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);
用了强制类型转换,还是不可以