前言:前几天刚写完一个爬虫,觉得无所事事,便想着去看一下,可视化easyx,刚好听了一节象棋课,其中原理大概是清楚了,然后想着搞一个五子棋,才有了这篇文章。
1:绘制地图
1.1九乃极数格子数:9x9
1.2线条:全都黑色的吧 宽度:2个像素点应该够了
1.3图片背景:棕黄色 记得刷新
1. void drawMap()
2. {
3. wuzhi=initgraph(540+5,580+5,1);
4. setbkcolor(BROWN); //设置窗口颜色为白色
5. cleardevice();//刷新窗口
6. for (int i = 0;i < COW+1;i++)
7. {
8. setlinecolor(BLACK);
9. line(0, i * SIZE+40, 540, i * SIZE+40);
10. }
11. for (int i = 0;i < COL+1;i++)
12. {
13. setlinecolor(BLACK);
14. line(i * SIZE, 40, i * SIZE, 580);
15. }
16. settextstyle(40, 16, "微软雅黑");
17. settextcolor(RED);
18. outtextxy(230, 0, "五子棋");
19.
20. }
2:初始化数组,开始画图
2.1我们在这里初始化数组值为0
2.2之后下棋,白子先手
2.3下过的地方不能再下棋,这里遍历一下
void initmoveChess()
{
int x=0, y=0;
MOUSEMSG m;
m = GetMouseMsg();
int X = m.x - 5;
int Y = m.y - 45;
switch ( X/30)
{
case 0:
x = 0;
break;
case 1:
case 2:
x = 1;
break;
case 3:
case 4:
x = 2;
break;
case 5:
case 6:
x = 3;
break;
case 7:
case 8:
x = 4;
break;
case 9:
case 10:
x = 5;
break;
case 11:
case 12:
x = 6;
break;
case 13:
case 14:
x = 7;
break;
case 15:
case 16:
x = 8;
break;
case 17:
case 18:
x = 9;
break;
}
switch ( Y/30)
{
case 0:
y = 0;
break;
case 1:
case 2:
y = 1;
break;
case 3:
case 4:
y = 2;
break;
case 5:
case 6:
y = 3;
break;
case 7:
case 8:
y = 4;
break;
case 9:
case 10:
y = 5;
break;
case 11:
case 12:
y = 6;
break;
case 13:
case 14:
y = 7;
break;
case 15:
case 16:
y = 8;
break;
case 17:
case 18:
y = 9;
break;
}
if (array[x][y] == 0)
{
if (m.uMsg == WM_LBUTTONDOWN)
{
if (white == black)
{
CHEES[k].id = 1;
CHEES[k].collor = 白;
CHEES[k].x = x * SIZE + 5;
CHEES[k].y = y * SIZE + 5;
array[x][y] = 1;
setfillcolor(WHITE);
fillcircle(x * SIZE, y * SIZE + 40, 20);//下白棋
white++;
}
else if (black < white)
{
CHEES[k].id = -1;
CHEES[k].collor = 黑;
CHEES[k].x = x * SIZE + 5;
CHEES[k].y = y * SIZE + 5;
array[x][y] = -1;
setfillcolor(BLACK);
fillcircle(x * SIZE, y * SIZE + 40, 20);//下黑棋
black++;
}
k++;
}
else if (m.uMsg == WM_RBUTTONDOWN)
{
int i, j;
for (i = 0;i < COW + 1;i++)
{
for (j = 0;j < COL + 1;j++)
{
printf("%d", array[j][i]);
}
printf("\n\n\n");
}
}//查看数组排列,需设置控制台
}//只有没下过的能下,默认没下过的数组值为0
else
{
initmoveChess();
}//如果鼠标给的参数不为0,那就重新给,递归
}
3:判断游戏是否结束
3.1每下一次就遍历一次
3.2白子设定值为1,黑子为-1
3.3当五个除零之外的,相同数相连时,代表游戏结束
void gameOver()
{
for (int i = 0; i < COW + 1 - 4; i++)
{
for (int j = 0;j < COL + 1 - 4;j++)
{
if ((array[j][i] == 1 && array[j + 1][i] == 1 && array[j + 2][i] == 1 && array[j + 3][i] == 1 && array[j + 4][i] == 1) ||
(array[j][i] == -1 && array[j + 1][i] == -1 && array[j + 2][i] == -1 && array[j + 3][i] == -1 && array[j + 4][i] == -1) ||
(array[i][j] == 1 && array[i][j + 1] == 1 && array[i][j + 2] == 1 && array[i][j + 3] == 1 && array[i][j + 4] == 1) ||
(array[i][j] == -1 && array[i][j + 1] == -1 && array[i][j + 2] == -1 && array[i][j + 3] == -1 && array[i][j + 4] == -1) ||
(array[i][j] == 1 && array[i + 1][j + 1] == 1 && array[i + 2][j + 2] == 1 && array[i + 3][j + 3] == 1 && array[i + 4][j + 4] == 1) ||
(array[i][j] == -1 && array[i + 1][j + 1] == -1 && array[i + 2][j + 2] == -1 && array[i + 3][j + 3] == -1 && array[i + 4][j + 4] == -1))
{
MessageBox(wuzhi, "gameover", "游戏结束了年轻人", MB_YESNO);
exit(0);//宣告游戏结束,咱们整一个弹窗
}
}
}
}
4:除此之外,我设定了一个自动下棋的函数
4.1配合随机函数
4.2遍历数组
4.3循环加递归,但注意进行循环递归操作时,最好加个sleep函数,将程序运行速度带慢,不然容易爆栈
void GUA1()//如若先手执子,吾亦能绝杀神明一桌
{
int x, y;
while (1)
{
srand(time(0));
x = rand() % 10;
y = rand() % 10;
if (array[x][y] == 0)
{
if (white == black)
{
CHEES[k].id = 1;
CHEES[k].collor = 白;
CHEES[k].x = x * SIZE + 5;
CHEES[k].y = y * SIZE + 5;
array[x][y] = 1;
setfillcolor(WHITE);
fillcircle(x * SIZE, y * SIZE + 40, 20);//下白棋
white++;
}
else if (black < white)
{
CHEES[k].id = -1;
CHEES[k].collor = 黑;
CHEES[k].x = x * SIZE + 5;
CHEES[k].y = y * SIZE + 5;
array[x][y] = -1;
setfillcolor(BLACK);
fillcircle(x * SIZE, y * SIZE + 40, 20);//下黑棋
black++;
}
k++;
Sleep(1200);
}
else
{
GUA1();
}//如果鼠标给的参数不为0,那就重新给,递归
gameOver();
}
}
笔者的地图绘制是有些问题的,初始地图画小了一点,导致边界棋子,只能显示一部分。
因为才接触easyx 3天,有些细节上的问题还是处理不够到位。
希望鄙人,浅薄的知识,能对你有所帮助。
以下是源代码:
#include<graphics.h>
#include<stdio.h>
#pragma warning(disable : 4996);
#include<windows.h>
#include<time.h>
#define COW 9
#define COL 9
#define SIZE 60
HWND wuzhi;
int array[COW + 1][COL + 1] = {NULL};
int k = 0;
int white = 0, black = 0;
enum chess_collor
{
白, 黑
};
struct chees
{
chess_collor collor;
int x;
int y;
int id;
}CHEES[100];
void gameOver()
{
for (int i = 0; i < COW + 1 - 4; i++)
{
for (int j = 0;j < COL + 1 - 4;j++)
{
if ((array[j][i] == 1 && array[j + 1][i] == 1 && array[j + 2][i] == 1 && array[j + 3][i] == 1 && array[j + 4][i] == 1) ||
(array[j][i] == -1 && array[j + 1][i] == -1 && array[j + 2][i] == -1 && array[j + 3][i] == -1 && array[j + 4][i] == -1) ||
(array[i][j] == 1 && array[i][j + 1] == 1 && array[i][j + 2] == 1 && array[i][j + 3] == 1 && array[i][j + 4] == 1) ||
(array[i][j] == -1 && array[i][j + 1] == -1 && array[i][j + 2] == -1 && array[i][j + 3] == -1 && array[i][j + 4] == -1) ||
(array[i][j] == 1 && array[i + 1][j + 1] == 1 && array[i + 2][j + 2] == 1 && array[i + 3][j + 3] == 1 && array[i + 4][j + 4] == 1) ||
(array[i][j] == -1 && array[i + 1][j + 1] == -1 && array[i + 2][j + 2] == -1 && array[i + 3][j + 3] == -1 && array[i + 4][j + 4] == -1))
{
MessageBox(wuzhi, "gameover", "游戏结束了年轻人", MB_YESNO);
exit(0);//宣告游戏结束,咱们整一个弹窗
}
}
}
}
void GUA1()//如若先手执子,吾亦能绝杀神明一桌
{
int x, y;
while (1)
{
srand(time(0));
x = rand() % 10;
y = rand() % 10;
if (array[x][y] == 0)
{
if (white == black)
{
CHEES[k].id = 1;
CHEES[k].collor = 白;
CHEES[k].x = x * SIZE + 5;
CHEES[k].y = y * SIZE + 5;
array[x][y] = 1;
setfillcolor(WHITE);
fillcircle(x * SIZE, y * SIZE + 40, 20);//下白棋
white++;
}
else if (black < white)
{
CHEES[k].id = -1;
CHEES[k].collor = 黑;
CHEES[k].x = x * SIZE + 5;
CHEES[k].y = y * SIZE + 5;
array[x][y] = -1;
setfillcolor(BLACK);
fillcircle(x * SIZE, y * SIZE + 40, 20);//下黑棋
black++;
}
k++;
Sleep(1200);
}
else
{
GUA1();
}//如果鼠标给的参数不为0,那就重新给,递归
gameOver();
}
}
void initmoveChess()
{
int x=0, y=0;
MOUSEMSG m;
m = GetMouseMsg();
int X = m.x - 5;
int Y = m.y - 45;
switch ( X/30)
{
case 0:
x = 0;
break;
case 1:
case 2:
x = 1;
break;
case 3:
case 4:
x = 2;
break;
case 5:
case 6:
x = 3;
break;
case 7:
case 8:
x = 4;
break;
case 9:
case 10:
x = 5;
break;
case 11:
case 12:
x = 6;
break;
case 13:
case 14:
x = 7;
break;
case 15:
case 16:
x = 8;
break;
case 17:
case 18:
x = 9;
break;
}
switch ( Y/30)
{
case 0:
y = 0;
break;
case 1:
case 2:
y = 1;
break;
case 3:
case 4:
y = 2;
break;
case 5:
case 6:
y = 3;
break;
case 7:
case 8:
y = 4;
break;
case 9:
case 10:
y = 5;
break;
case 11:
case 12:
y = 6;
break;
case 13:
case 14:
y = 7;
break;
case 15:
case 16:
y = 8;
break;
case 17:
case 18:
y = 9;
break;
}
if (array[x][y] == 0)
{
if (m.uMsg == WM_LBUTTONDOWN)
{
if (white == black)
{
CHEES[k].id = 1;
CHEES[k].collor = 白;
CHEES[k].x = x * SIZE + 5;
CHEES[k].y = y * SIZE + 5;
array[x][y] = 1;
setfillcolor(WHITE);
fillcircle(x * SIZE, y * SIZE + 40, 20);//下白棋
white++;
}
else if (black < white)
{
CHEES[k].id = -1;
CHEES[k].collor = 黑;
CHEES[k].x = x * SIZE + 5;
CHEES[k].y = y * SIZE + 5;
array[x][y] = -1;
setfillcolor(BLACK);
fillcircle(x * SIZE, y * SIZE + 40, 20);//下黑棋
black++;
}
k++;
}
else if (m.uMsg == WM_RBUTTONDOWN)
{
int i, j;
for (i = 0;i < COW + 1;i++)
{
for (j = 0;j < COL + 1;j++)
{
printf("%d", array[j][i]);
}
printf("\n\n\n");
}
}//查看数组排列,需设置控制台
}//只有没下过的能下,默认没下过的数组值为0
else
{
initmoveChess();
}//如果鼠标给的参数不为0,那就重新给,递归
}
void drawMap()
{
wuzhi=initgraph(540+5,580+5,1);
setbkcolor(BROWN); //设置窗口颜色为白色
cleardevice();//刷新窗口
for (int i = 0;i < COW+1;i++)
{
setlinecolor(BLACK);
line(0, i * SIZE+40, 540, i * SIZE+40);
}
for (int i = 0;i < COL+1;i++)
{
setlinecolor(BLACK);
line(i * SIZE, 40, i * SIZE, 580);
}
settextstyle(40, 16, "微软雅黑");
settextcolor(RED);
outtextxy(230, 0, "五子棋");
}
int main()
{
drawMap();
while (1)
{
GUA1();//这里设置,卡顿行不通,因为他是个递归,我们从函数本身下手
gameOver();
}
}