椰Ye 2023-01-07 01:42 采纳率: 63.2%
浏览 140
已结题

C++ easyx 五子棋游戏

C++ easyx 五子棋游戏
受不了黑框框,想用easyx做一个界面
#include<iostream>
#include<easyx.h>
using namespace std;

struct state {
    int x;
    int y;
    int player;
};
class chess {
public:
    void buildboard(); //棋盘与棋子绘制
    void mouse_confirm(ExMessage *mess);  //鼠标确认
    int judge(int x, int y);
private:
    int chess[15][15]; //存储棋子状态,初始化为0-1为黑子,1为白子
    state Gstate; //默认黑子先行
};


void chess::buildboard() 
{
    setlinecolor(WHITE);   //设置线条颜色
    setlinestyle(PS_SOLID, 2);
    //线条间距为40
    for (int i = 1; i < 16; i++) {
        line(30, i * 40, 30 + 40 * 14, 40 * i);
    }
    for (int i = 0; i < 15; i++) {
        line(30 + i * 40, 40, 30 + i * 40, 40 * 15);
    }
    //绘制边框与棋盘定点
    setlinestyle(PS_SOLID, 4);
    rectangle(30, 40, 30 + 14 * 40, 40 * 15);
    setfillcolor(WHITE);
    solidcircle(30 + 7 * 40, 40 * 8, 5);  //中间
    solidcircle(30 + 3 * 40, 40 * 4, 5);  //左上
    solidcircle(30 + 3 * 40, 40 * 12, 5);  //左下
    solidcircle(30 + 10 * 40, 40 * 4, 5); //右上
    solidcircle(30 + 10 * 40, 40 * 12, 5);  //右下

    for (int i = 0; i < 15; i++) {
        for (int j = 0; j < 15; j++) {
            if (chess[i][j] == -1) {
                setfillcolor(BLACK);
                solidcircle(30 + i * 40, 40 * (j + 1), 10);
            }
            if (chess[i][j] == 1) {
                setfillcolor(WHITE);
                solidcircle(30 + i * 40, 40 * (j + 1), 10);
            }
        }
    }
}

void chess::mouse_confirm(ExMessage *mess) 
{
    Gstate.x = (int)((mess->x - 30) / 40);
    Gstate.y = (int)((mess->y - 40) / 40);
    if (mess->message == WM_LBUTTONDOWN) {
        chess[Gstate.x][Gstate.y] = Gstate.player;
        if (chess::judge(Gstate.x, Gstate.y) == 1) {
            MessageBox(GetHWnd(), "Game over", "提示", MB_OK);
            exit(1);
        }
        //切换棋手
        if (Gstate.player = -1) {
            Gstate.player = 1;
        }
        if (Gstate.player = 1) {
            Gstate.player = -1;
        }
    }
}

int chess::judge(int x, int y) 
{
    for (int i = 0; i < 15; i++) {
        for (int j = 0; j < 15; j++) {
            if (chess[i][j] == 1
                && chess[i ][j+1] == 1
                && chess[i][j + 2] == 1
                && chess[i ][j + 3] == 1
                && chess[i ][j + 4] == 1) {
                return 1;
            }
            if (chess[i][j] == -1
                && chess[i][j + 1] == -1
                && chess[i][j + 2] == -1
                && chess[i][j + 3] == -1
                && chess[i][j + 4] == -1) {
                return 1;
            }
            if (chess[i][j] == 1
                && chess[i+1][j] == 1
                && chess[i+2][j] == 1
                && chess[i+3][j] == 1
                && chess[i+4][j] == 1) {
                return 1;
            }
            if (chess[i][j] == -1
                && chess[i + 1][j] == -1
                && chess[i + 2][j] == -1
                && chess[i + 3][j] == -1
                && chess[i + 4][j] == -1) {
                return 1;
            }
            if (chess[i][j] == 1
                && chess[i + 1][j-1] == 1
                && chess[i + 2][j-2] == 1
                && chess[i + 3][j-3] == 1
                && chess[i + 4][j-4] == 1) {
                return 1;
            }
            if (chess[i][j] == -1
                && chess[i + 1][j - 1] == -1
                && chess[i + 2][j - 2] == -1
                && chess[i + 3][j - 3] == -1
                && chess[i + 4][j - 4] == -1) {
                return 1;
            }
        }
    }
}

int main() {
    chess wuziqi;
    initgraph(800, 720);
    IMAGE background;
    loadimage(&background, "beijin.jpg");  //加载图片
    putimage(0, 0, &background);   //输出图片
    wuziqi.buildboard();
    while (1) {
        ExMessage mess; //处理消息
        //鼠标消息获取
        if (peekmessage(&mess, EX_MOUSE)) //消息类型
        {
            switch (mess.message) {
            case WM_LBUTTONDOWN:  //左键
                wuziqi.mouse_confirm(&mess);
                break;
            default:
                break;
            }
        }

    }
    system("pause");
    return 0;
}

代码每一个部分都完成了,但是为什么不能下棋,是哪里出了问题
以为是坐标的问题,Gstate.x = (int)((mess->x - 30) / 40);
Gstate.y = (int)((mess->y - 40) / 40);

用了强制类型转换,还是不可以

  • 写回答

2条回答 默认 最新

  • 赵4老师 2023-01-07 12:06
    关注

    easyx官网上的例子代码很多,可以捡感兴趣的参考一下。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月7日
  • 创建了问题 1月7日

悬赏问题

  • ¥50 求一位精通京东相关开发的专家
  • ¥100 求懂行的大ge给小di解答下!
  • ¥15 pcl运行在qt msvc2019环境运行效率低于visual studio 2019
  • ¥15 MAUI,Zxing扫码,华为手机没反应。可提高悬赏
  • ¥15 python运行报错 ModuleNotFoundError: No module named 'torch'
  • ¥100 华为手机私有App后台保活
  • ¥15 sqlserver中加密的密码字段查询问题
  • ¥20 有谁能看看我coe文件到底哪儿有问题吗?
  • ¥20 我的这个coe文件到底哪儿出问题了
  • ¥15 matlab使用自定义函数时一直报错输入参数过多