ItIsEnderman
本帖最后由 ItIsEnderman 于 2019-11-9 20:08 编辑

C++写的24点,源码地址:
https://github.com/Endimyon/Twenty_four_points


刚睡醒脑子糊了,把24打成22,我去……算了懒得改了。


前几天老师布置了个不是作业的作业:写一个24点程序,然后我就肝了几个晚上写了个这。目前也刚讲完函数,没讲字符串,只能自己摸爬滚打;没有讲类到底该怎么用,当然基本没讲面向对象,因此所有函数全部……扔在外面,也因此不知道到底还有啥办法比全局变量更好。
另外测试时发现如果输入无效,有时候居然会数组下标越界……也不知道咋回事。

而且这也是第一次开Repository,有些东西翻译成中文也是一脸懵逼的。不过我这个专业到后面要整天玩GitHub的,早一天学就是早一天学,哈哈

所以谁能帮忙调试调试或者提点建议啥的,或者闲聊几句也行呗。

自己的调试:






来自群组: Bone Studio

chenan
我啥也看不懂

飞羽_fly
这个是干什么的

ItIsEnderman

二十四点游戏

袋里的鲨
我跑了一下, 目测是 cord 和 poke 那里出锅了

不过没仔细看 = = 其实是口胡乱讲



g++ -fsanitize=address 就是好啊

ItIsEnderman
本帖最后由 ItIsEnderman 于 2019-11-9 21:06 编辑
袋里的鲨 发表于 2019-11-9 20:41
我跑了一下, 目测是 cord 和 poke 那里出锅了

不过没仔细看 = = 其实是口胡乱讲

这行吗?咋越界的……

FloatingBlocks
本帖最后由 GXHurry 于 2019-11-9 21:28 编辑

之前也是想做一个的,既然你做出来了我就直接拿来用好了

还有我的习惯是先写原型,再把函数写后面,不过反正都一样


ItIsEnderman
GXHurry 发表于 2019-11-9 21:26
之前也是想做一个的,既然你做出来了我就直接拿来用好了

有Bug,5楼说了

FloatingBlocks

那就算了,

我自己的思路是写一个穷举的程序
然后生成一个包含所有数字的计算方法的文件,
需要的时候直接查找
不知道可不可行

袋里的鲨
ItIsEnderman 发表于 2019-11-9 21:05
这行吗?咋越界的……

if (str[length] == '1') {
            if (str[length + 1] == '0') {
                // 可能存在 Runtime Error 的情况!
                // length = str.length() - 1, length + 1 就越界了
                card[cardio] = 10;
                cardio++;
                cout << "#1 cardio: " << cardio << endl;
            }
            else {
                cout << "Invalid input." << endl;
                return false;
            }
        }
        else if (str[length] == 'j' || str[length] == 'J') {
            card[cardio] = 11;
            cardio++;
            cout << "#2 cardio: " << cardio << endl;
        }
        else if (str[length] == 'q' || str[length] == 'Q') {
            card[cardio] = 12;
            cardio++;
            cout << "#3 cardio: " << cardio << endl;
        }
        else if (str[length] == 'k' || str[length] == 'K') {
            card[cardio] = 13;
            cardio++;
            cout << "#4 cardio: " << cardio << endl;
        }
        else if (str[length] == 'a' || str[length] == 'A') {
            card[cardio] = 1;
            cardio++;
            cout << "#5 cardio: " << cardio << endl;
        }
        else if (str[length] == ' ' || str[length] == '0') {
            //Empty space will be ignored. And if the number is ten, the zero will have been used, so it will be ignored as well.
            //After all, 08 == 8. [doge]
        }
        else if (int(str[length] - '0') < 10) {
            card[cardio] = int(str[length] - '0');
            cardio++;
            cout << "#6 cardio: " << cardio << endl;
        }
        else {
            cout << "Invalid input." << endl;
            return false;
        }
    }

输出


This is a mathematic game "24 Points".
Input number 2~10 or A,J,Q,K in one line and then enter.
A,J,K,Q
#5 cardio: 1
#6 cardio: 2
#2 cardio: 3
#6 cardio: 4

然后就越界了...


ItIsEnderman
本帖最后由 ItIsEnderman 于 2019-11-9 22:00 编辑

完了,逗号被读进int了,看半天才反应过来,逗号是44,同余后成-4,,是个有效的值,然后后面还有有效的输入于是就……爆了
https://github.com/Endimyon/Twen ... c897fb2ffa7bfe57ccb
这样改行吗


袋里的鲨
ItIsEnderman 发表于 2019-11-9 21:54
完了,逗号被读进int了,看半天才反应过来,逗号是44,同余后成-4,,是个有效的值,然后后面还有有效的输 ...

并不想仔细看了, 今天好多事没做.jpg

但是我可以继续打进去一些奇奇怪怪的东西啊, 感觉加了个特判之后鲁棒性还是不够强...

myrymy
在这个游戏是在哪运行的

ItIsEnderman
myrymy 发表于 2019-11-9 23:36
在这个游戏是在哪运行的

控制台窗口

不负韶华
为什么做24点?你行做个Python版mc。(bushi)

ItIsEnderman
草五叶 发表于 2019-11-10 08:50
为什么做24点?你行做个Python版mc。(bushi)

你以为基础课教Py啊?看帖子内容啊,都说了这个是上面发下来的可选任务

1052701012
我啥也看不懂

不负韶华
ItIsEnderman 发表于 2019-11-10 09:26
你以为基础课教Py啊?看帖子内容啊,都说了这个是上面发下来的可选任务

好吧,你把24点做完再做Python版mc。

1707056307
我都看不懂

伏羲大大
24点程序?  赌博的?

第一页 上一页 下一页 最后一页