吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 409|回复: 25
收起左侧

[求助] C语言,五子棋胜利判断有一个Bug

[复制链接]
xi0405 发表于 2025-3-14 23:17
2025-03-14 22-23-47.png
Bug就是在我没有点击左侧棋子的时候直接点击棋盘的任意位置就会直接退出程序

这是全局变量


[C] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
IMAGE imgQipan;//棋盘
IMAGE imgchess[CHESS_COUNT];//棋子
IMAGE imgf;
IMAGE imgB;
IMAGE imgW;
int curChess = -1;// 0 黑 3 白
int curX = -10;
int curY = -10;//棋子动态坐标
int row = -1;
int col = -1;
struct ch
{
    int type;//类型
    bool used; 
};
 
struct ch chess[19][19];


这是棋子的初始化


[C] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
void chessInit()
{
    memset(chess, -1, sizeof(chess));
    char name[64];
    for (int i = 0; i < CHESS_COUNT; i++)
    {
        sprintf_s(name, sizeof(name), "棋子/%d.png", i + 1);
        loadimage(&imgchess[i], name,22.5,22.5);
    }
    for (int i = 0; i < 19; i++)
    {
        for (int j = 0; j < 19; j++)
        {
            chess[i][j].used = false;
        }
    }
}


这是判断胜利的代码块


[C] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
void updategame()
{
    //对胜利进行判断
    if (judgChess())
    {
        mciSendString("close BGM", 0, 0, 0);
        mciSendString("open 征服2.mp3 alias bgm", 0, 0, 0);
        mciSendString("play bgm repeat", 0, 0, 0);
        printf("你赢啦!!!");
        MessageBox(NULL, "over", "over", 0);
        exit(0);
    }
}
 
bool judgChess()
{
    if (row != -1 && col != -1)
    {
        int m = row;
        int n = col;
        int i = 1;
         
        //横 左
        if (m - 3 > 0 && chess[m][n].used &&
            chess[m - i][n].type == chess[m][n].type &&
            chess[m - ++i][n].type == chess[m][n].type &&
            chess[m - ++i][n].type == chess[m][n].type &&
            chess[m - ++i][n].type == chess[m][n].type)
            return true;
        i = 1;
        //横 右
        if (m + 3 < 19 && chess[m][n].used &&
            chess[m + i][n].type == chess[m][n].type &&
            chess[m + ++i][n].type == chess[m][n].type &&
            chess[m + ++i][n].type == chess[m][n].type &&
            chess[m + ++i][n].type == chess[m][n].type)
            return true;
        i = 1;
        //竖 上
        if (n - 3 > 0 && chess[m][n].used &&
            chess[m][n - i].type == chess[m][n].type &&
            chess[m][n - ++i].type == chess[m][n].type &&
            chess[m][n - ++i].type == chess[m][n].type &&
            chess[m][n - ++i].type == chess[m][n].type)
            return true;
        i = 1;
        //竖 下
        if (n + 3 < 19 && chess[m][n].used &&
            chess[m][n + i].type == chess[m][n].type &&
            chess[m][n + ++i].type == chess[m][n].type &&
            chess[m][n + ++i].type == chess[m][n].type &&
            chess[m][n + ++i].type == chess[m][n].type)
            return true;
        i = 1;
        // '\' 下
        if (n + 3 < 19 && m + 3 < 19 && chess[m][n].used &&
            chess[m + i][n + i].type == chess[m][n].type &&
            chess[m + ++i][n + i].type == chess[m][n].type &&
            chess[m + ++i][n + i].type == chess[m][n].type &&
            chess[m + ++i][n + i].type == chess[m][n].type)
            return true;
        i = 1;
        // '\'上
        if (n - 3 > 0 && m - 3 > 0 && chess[m][n].used &&
            chess[m - i][n - i].type == chess[m][n].type &&
            chess[m - ++i][n - i].type == chess[m][n].type &&
            chess[m - ++i][n - i].type == chess[m][n].type &&
            chess[m - ++i][n - i].type == chess[m][n].type)
            return true;
        i = 1;
        // '/'下
        if (m - 3 > 0 && n + 3 < 19 && chess[m][n].used &&
            chess[m - i][n + i].type == chess[m][n].type &&
            chess[m - ++i][n + i].type == chess[m][n].type &&
            chess[m - ++i][n + i].type == chess[m][n].type &&
            chess[m - ++i][n + i].type == chess[m][n].type)
            return true;
        i = 1;
        // '/'上
        if (m + 3 < 19 && n - 3 > 0 && chess[m][n].used &&
            chess[m + i][n - i].type == chess[m][n].type &&
            chess[m + ++i][n - i].type == chess[m][n].type &&
            chess[m + ++i][n - i].type == chess[m][n].type &&
            chess[m + ++i][n - i].type == chess[m][n].type)
            return true;
 
    }
     
    return false;
}


这是鼠标状态的代码块


[C] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
void usedClick()
{
    ExMessage msg;
    static int status = 0;
    if (peekmessage(&msg))
    {
        if (msg.message == WM_LBUTTONDOWN)//按下鼠标状态
        {
            if (msg.x > 10 && msg.x < 40
                && msg.y >150 && msg.y < 180)
            {
                status = 1;
                printf("1");
                curChess = 0;
            }
            else if (msg.x > 10 && msg.x < 40
                && msg.y >450 && msg.y < 480)
            {
                status = 1;
                printf("2");
                curChess = 3;
            }
        }
 
        if (msg.message == WM_MOUSEMOVE && status == 1)//拖动
        {
            curX = msg.x;
            curY = msg.y;
        }
 
        if (msg.message == WM_LBUTTONUP)// 抬起
        {
            if (msg.x >= 74 && msg.x <= 592.94 + 28.83
                && msg.y >= 23 && msg.y <= 542 + 28.83)
            {
                row = (msg.x - 74) / 28.83;
                col = (msg.y - 23) / 28.83;
                if (chess[row][col].type == -1 && !chess[row][col].used)
                {
                    chess[row][col].type = curChess;
                    chess[row][col].used = true;
                }
                curX = -10;
                curY = -10;
                curChess = -1;
                status = 0;
            }
        }
    }
}

因为Bug就是出在没有使用棋子时鼠标按到棋盘的任意位置直接终止程序,所以我就开始从判断和鼠标状态开始排查。然后发现棋子的初始化,就是将棋盘上的每个位置都赋值为 -1,且都是未使用状态。那么鼠标在没有点击棋子后直接按下棋盘的一个位置再抬起,那么if(chess[row][col].type == -1一定为真,chess[row][col].used为false !后也就是真),因此更新此位置的chess[row][col].used=true。那么判断中一定有一个能达到胜利。现在我的问题就是咋解决这个问题。

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

冥界3大法王 发表于 2025-3-14 23:39
能自动走VCT  、VCF 吗?
懂得开局走棋型不?
没有限制禁手的这些都没有那就没意思了。
 楼主| xi0405 发表于 2025-3-14 23:44
冥界3大法王 发表于 2025-3-14 23:39
能自动走VCT  、VCF 吗?
懂得开局走棋型不?
没有限制禁手的这些都没有那就没意思了。

小白,拿简单的小项目练手的目的是学习编程,VCT、VCF啥的不懂
冥界3大法王 发表于 2025-3-15 08:48
xi0405 发表于 2025-3-14 23:44
小白,拿简单的小项目练手的目的是学习编程,VCT、VCF啥的不懂

网上找本那威 彭伟国写的五子棋入门书籍看吧。
之后你就有灵感了。
不然fc红白机那种简单的人机对战五子棋都干不过。
因为弱爆了。。。

点评

别人在沟通编程问题,你能别在这瞎扯吗?  详情 回复 发表于 2025-3-15 08:59
Hmily 发表于 2025-3-15 08:59
冥界3大法王 发表于 2025-3-15 08:48
网上找本那威 彭伟国写的五子棋入门书籍看吧。
之后你就有灵感了。
不然fc红白机那种简单的人机对战五 ...

别人在沟通编程问题,你能别在这瞎扯吗?
kezzyhu 发表于 2025-3-15 09:07
IDE不能下断点吗
LHR0510 发表于 2025-3-15 09:21
高手,高高手!
zhang0820277 发表于 2025-3-15 09:46
五子棋挺好玩
wcx101 发表于 2025-3-15 09:59
现在的我只会c和汇编这样的老语言,我要找时间学习
haikura 发表于 2025-3-15 10:11
楼主是专注考试还是面试啊,我也学C但我纯是应试的,实战项目一看就歇菜。。。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-3-15 20:28

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表