前言:关于五子棋小游戏的大概思路
1.五子棋的棋盘我选用的是15*15的规格,可以把棋盘看成对应的二维数组,然后由坐标位置进行转换得到二维数组下标位置,用1表示白色棋子,用-1表示黑色棋子,用0表示没有被占用的空格位。
2.对于黑棋和白棋,可以给他们附上一些游戏进行所需要的属性,比如,是否胜利、棋子的表示形式、当前是否轮到该棋子下棋、以及当前所下棋子的窗口坐标。
3.调用easyx.h里面的一些绘图函数实现一个绘图窗口五子棋游戏。
4.有人机模式(电脑作为棋手与人对战)、对战模式(一人分饰两角)、复盘(载入之前存过的存档)。
5.可以保存游戏。
1.实现页面的跳转(只是大概意思,具体可能有细微差别)
现在实现一个功能,用鼠标左键点击窗口中指定位置便能跳转页面从而进行到下一步操作。
首先了解怎么做到用鼠标点击的功能:
(补充:VK_LBUTTON是鼠标左键的虚拟键值,因为当你按过键盘上任一个按键时,GetAsyncKeyState函数的16位的short类型返回值的最低位为1,根据此特性,可以用GetAsyncKeyState(VK_LBUTTON)&1判断是否按过左键,若按过,GetAsyncKeyState(VK_LBUTTON)&1=1,;反之)
bool isIn(POINT* msg, int x, int y, int width, int height)//判断鼠标是否在给定的区域内
{
if (msg->x >= x && msg->x <= x + width && msg->y >= y && msg->y <= y + height)
{
return true;
}
return false;
}
HWND hadle = GetHWnd();//获取绘图窗口句柄
POINT msg;//鼠标结构体
GetCursorPos(&msg);//获取鼠标指针位置
ScreenToClient(hwnd,&msg);//将鼠标指针位置转换为窗口坐标
if(GetAsyncKeyState(VK_LBUTTON)&1)//判断是否按过左键
{
if(isIn(&msg, x, y, width, height))//这里的x、y、width、height可以自己改
{
//这里可以加左击鼠标后要进行的操作,以上就是判断鼠标是否左击的代码了
}
}
用a表示跳转页面次数,state表示模块,其中state有人机模式(AI)、对战模式(people)、复盘(redo),构建一个state枚举如下:
enum state
{
HOME,//初始模块
AI,//人机对战
PEOPLE,//真人对战
REDO,//复盘
}
a开始为0,state开始为HOME.
当跳转次数为0,即a=0、state为“HOME”,即state=HOME时,初始页面如下:
1.1.“人机模式”相关页面的跳转
当a=0,state=HOME时,用左键点击“人机模式”时,a=1,state=AI,所在页面如下:
当a=1,state=AI时,左键点击“返回”,则a=0,state=HOME
当a=1,state=AI时,用左键点击“开始游戏”时,a=2,state不变,所在页面如下:
当a=2,state=AI时,左键点击“返回”时,情况稍有不同:
(由于此时还在游戏中,电脑正在循环运行游戏部分的代码,需要确认是否是误触导致点到的“返回”,所以当左键点到“返回”时,a=3,state不变。当a=3并且state=AI时,进入循环,选择“是”或“否”,若一直不选则一直待在a=3,state=AI的界面。如果选了“是”,则直接跳转到a=1,state=AI的界面,且a=1,state不变;如果选了“否”,则退出循环,a=2,state不变,继续进行游戏。)
思维导图如下:
当a=3,state=AI时所示图如下:
1.2.对战模式相关页面的跳转
当a=0,state=HOME时,用左键点击“对战模式”时,a=1,state=PEOPLE,所在页面如下:
当a=1,state=PEOPLE时,左键点击“返回”,则a=0,state=HOME
在a=1,state=PEOPLE的基础下,用左键点击“开始游戏”时,a=2,state不变,所在页面如下:
当a=2,state=PEOPLE时,左键点击“返回”时,情况稍有不同:
(由于此时还在游戏中,电脑正在循环运行游戏部分的代码,需要确认是否是误触导致点到的“返回”,所以当左键点到“返回”时,a=3,state不变。当a=3并且state=PEOPLE时,进入循环,选择“是”或“否”,若一直不选则一直待在a=3,state=PEOPLE的界面。如果选了“是”,则直接跳转到a=1,state=PEOPLE的界面,且a=1,state不变;如果选了“否”,则退出循环,a=2,state不变,继续进行游戏。)
思维导图如下:
当a=3,state=PEOPLE时所示图如下:
1.3.“复盘”相关页面的跳转
当a=0,state=HOME时,当左键点击了“复盘”时,a=1,state=REDO,所在页面如下:
令state1初始为REDO,其负责在人机模式和对战模式的复盘页面中跳转。
当点击“人机模式”时,a=2,state为REDO不变,而state1=AI,所在页面如下:
当点击i=(1,2…6)存档时,从文件里对应的存档位置读取数据,然后a不变,state=AI,所以就顺理成章的进入了“人机模式”。
当点击“对战模式”时,a=2,state为REDO不变,而state1=PEOPLE,所在页面如下:
当点击i=(1,2…6)存档时,从文件里对应的存档位置读取数据,然后a不变,state=PEOPLE,所以就顺理成章的进入了“对战模式”。
2.游戏部分代码的大概思路
游戏部分可以看做是一步一步的下棋,
先定义一个包含棋子属性的结构体:
enum playerstate { OK;//正常状态 VICTORY;//胜利 } typedef struct player { double x;//棋子在绘图窗口的横坐标 double y;//棋子在绘图窗口的纵坐标 int state;//棋子的具体状态,初始状态都为OK int sequence;//当sequence=1时轮到此棋下,sequence=0时另外一棋下 int a;//区分黑棋和白棋,白棋的a=1,黑棋的a=-1; }player,*pplayer;
令白棋为player1,黑棋为player2。故先套一个循环,一次循环可以下一次棋,下一次棋可以移动多次棋子待确定为止。循环的终止条件为黑棋或白棋其中一个的state=VICTORY或棋盘已满,
如果是中途退出当前对局则跳转次数a=1,会跳转到a=1,state=PEOPLE\AI界面中;如果循环结束跳转次数a也为1,会跳转到a=1,state=PEOPLE\AI界面中。
2.1.“对战模式”的实现
在“对战模式”中,循环里面主要分为4步:下一步棋的具体过程、判断黑棋和白棋的胜负情况、交换下棋顺序(比如这次是白棋下,那下次轮到黑棋了)、检查棋盘是否已满。
思维导图如下:
2.1.1下一步棋的具体过程
把棋盘想像成一个15*15的二维数组,初始化黑白两方棋子状态和棋盘,然后由坐标位置进行转换得到二维数组下标位置
player player1;//攻方--白棋
player player2;//守方--黑棋
int boardAdd[15][15] = { 0 };//棋盘为空
player1.state = player2.state = OK;
player1.sequence = 1;
player2.sequence = 0;
player1.a = 1;//表示白棋
player2.a = -1;//表示黑棋
构想一个可以预设棋子的函数,每新下一步棋时,预设棋子的位置都重新刷在棋盘中央。通过“WASD”控制棋子的预设布局,可以多次进行移动,所以是一个循环结构,当按下ENTER时布下棋子,且该位置原来没有布下棋子时,则循环结束。
当按“W”键时,棋子向上,“A”向左,“S”向下,“D”向右。
由于背景图宽467像素,高609像素,为方便计算和大小适中,我设置绘图窗口宽4671.5,高6091.5。
所以预设初始位置(即棋子初始位置,无论黑棋白棋都一样):
pplayer1->x = pplayer2->x = 239 * 1.5 - 23;
pplayer1->y = pplayer2->y = 306 * 1.5 - 21;
这样就在中央位置附近了,随后进行预设棋子环节,其中棋子移动一格的宽度和高度大概都是48.5像素,就可以由坐标求得二维数组的对应下标位置,下面的b、c就是下标。每次移动预设棋子都要重新绘一遍图。同时若按ENTER键,且该位置原来为空,则在二维数组对应的位置上记录棋子属性(1或-1):
(注:依据下面一段代码所示,棋盘高度最低为98.5,最高777.5,棋盘宽度最低-4,最高675。当棋子向上时,98.5<y<=777.5;向下时,98.5=<y<777.5;向左时,-4<x<=675;向右时,-4=<x<777.5。这样就能保证棋子不会越界了)
void putMap(pplayer pplayer, int(*boardAdd)[15], IMAGE* image_start, IMAGE* image_piece, int h)
{
int i, j;
double b , c ;
BeginBatchDraw();
putimage(0, 0, image_start + h);//先绘制背景图
putimage(b, c, image_piece + 4);//再在背景图上绘制预设棋子图案
for (i = 0; i < 15; i++)
{
for (j = 0; j < 15; j++)
{
if (boardAdd[i][j] == 1)
{
b = j * 48.5 - 4;//二维数组中为1的下标对应的横坐标
c = 98.5 + i * 48.5;//二维数组中为1的下标对应的纵坐标
if (b != pplayer->x || c != pplayer->y)//若二维数组中为1的坐标位置与预设棋子位置不同时可以绘制,相同时要被预设棋子覆盖
{
putimage(b, c, image_piece + 2);//在背景图上绘制白色棋子
}
}
if (boardAdd[i][j] == -1)
{
b = j * 48.5 - 4;//二维数组中为-1的下标对应的横坐标
c = 98.5 + i * 48.5;//二维数组中为-1的下标对应的纵坐标
if (b != pplayer->x || c != pplayer->y)//若二维数组中为-1的坐标位置与预设棋子位置不同时可以绘制,相同时要被预设棋子覆盖
{
putimage(b, c, image_piece + 3);//在背景图上绘制黑色棋子
}
}
}
}
EndBatchDraw();
}
//BeginBatchDraw()和EndBatchDraw()为双缓冲绘图,可以解决绘图闪烁的问题。
//下一颗棋的流程
int n=1;
while (n)
{
b = (int)((pplayer->x + 4) / 48.5);
c = (int)((pplayer->y- 98.5) / 48.5);
putMap(pplayer, boardAdd, image_start, image_piece, 5);
if (KEY_PRESS(0x57) && pplayer->y > 98.5 && pplayer->y <= 777.5)//'W'键
{
pplayer->y -= 48.5;
}
if (KEY_PRESS(0x53) && pplayer->y < 777.5 && pplayer->y >= 98.5)//'S'键
{
pplayer->y += 48.5;
}
if (KEY_PRESS(0x41) && pplayer->x > -4 && pplayer->x <= 675)//'A'键
{
pplayer->x -= 48.5;
}
if (KEY_PRESS(0x44) && pplayer->x >= -4 && pplayer->x < 675)//'D'键
{
pplayer->x += 48.5;
}
if (KEY_PRESS(VK_RETURN) && boardAdd[c][b] == 0)
{
n = 0;
}
}
//棋下好了就记录下来到二维数组里
if (boardAdd[c][b] == 0)
{
boardAdd[c][b] = pplayer->a;//记录当前所下的棋子
}
这一部分的思维导图如下:
2.1.2判断黑棋和白棋的胜负情况
当黑棋或白棋有一方的棋子在横向、纵向、左斜向、右斜向四个方向有五子一线时,则判定为这个颜色的棋获胜。可以通过二维数组来判断,如果在横向、纵向、左斜向、右斜向其中一个方向有5个或超过5个“1”成一条线时,则白棋获胜;如果在横向、纵向、左斜向、右斜向其中一个方向有5个或超过5个“-1”成一条线时,则黑棋获胜。以下四种情况都是以白棋为例。
2.1.2.1.如何判断棋子横向的胜负情况
从当前棋子位置开始判定:先将当前棋子坐标位置转换成数组下标位置,
int j = (int)((pplayer->x + 23 + 4) / 48.5);//列标
i = (int)((pplayer->y + 21 - 98.5) / 48.5);//横标
count = 0//记录白棋出现的个数
分别从此下标开始往左、往右判断,就有
int b = 1;
int c = 1;
while (boardAdd[i - b][j] == 1||boardAdd[i + c][j] == 1)//向左、右判断
{
if (boardAdd[i - b][j] == 1)
{
b++;
count++;
}
if (boardAdd[i + c][j] == 1)
{
c++;
count++;
}
if (count+1==5)
{
pplayer->state = VICTORY;//如果一个方向有5个或超过5个“1”成一条线时,则白棋获胜
}
}
黑棋同理;
2.1.2.2.如何判断棋子纵向的胜负情况
从当前棋子位置开始判定:先将当前棋子坐标位置转换成数组下标位置,
int j = (int)((pplayer->x + 23 + 4) / 48.5);//列标
i = (int)((pplayer->y + 21 - 98.5) / 48.5);//横标
count = 0//记录白棋出现的个数
分别从此下标开始往上、往下判断,就有
int b = 1;
int c = 1;
while (boardAdd[i][j - b] ==1|| boardAdd[i][j + c] == 1)//向下判断、向上判断
{
if (boardAdd[i][j - b] == 1)
{
b++;
count++;
}
if (boardAdd[i][j + c] == 1)
{
c++;
count++;
}
if (count + 1 == 5)
{
pplayer->state = VICTORY;//如果一个方向有5个或超过5个“1”成一条线时,则白棋获胜
}
}
黑棋同理;
2.1.2.3.如何判断棋子左斜向的胜负情况
从当前棋子位置开始判定:先将当前棋子坐标位置转换成数组下标位置,
int j = (int)((pplayer->x + 23 + 4) / 48.5);//列标
i = (int)((pplayer->y + 21 - 98.5) / 48.5);//横标
count = 0//记录白棋出现的个数
分别从此下标开始往左上方、往右下方判断,就有
int b = 1, c = 1;
int e = 1, f = 1;
while (boardAdd[i - b][j - c] == 1|| boardAdd[i + e][j + f] == 1)//向左上、右下判断
{
if (boardAdd[i - b][j - c] == pplayer->a)
{
b++;
c++;
count++;
}
if (boardAdd[i + e][j + f] == pplayer->a)
{
e++;
f++;
count++;
}
if (count + 1 == 5)
{
pplayer->state = VICTORY;//如果一个方向有5个或超过5个“1”成一条线时,则白棋获胜
}
}
黑棋同理;
2.1.2.4.如何判断棋子右斜向的胜负情况
从当前棋子位置开始判定:先将当前棋子坐标位置转换成数组下标位置,
int j = (int)((pplayer->x + 23 + 4) / 48.5);//列标
i = (int)((pplayer->y + 21 - 98.5) / 48.5);//横标
count = 0//记录白棋出现的个数
分别从此下标开始往左下方、往右上方判断,就有
int b = 1, c = 1;
int e = 1, f = 1;
while (boardAdd[i - b][j + c] == 1|| boardAdd[i + e][j - f] == 1)//向左下、右上判断
{
if (boardAdd[i - b][j + c] == 1)
{
b++;
c++;
count++;
}
if (boardAdd[i + e][j - f] ==1)
{
e++;
f++;
count++;
}
if (count + 1 == 5)
{
pplayer->state = VICTORY;//如果一个方向有5个或超过5个“1”成一条线时,则白棋获胜
}
}
黑棋同理;
2.1.3.交换下棋顺序
默认先是白棋下,下一颗棋交换一次顺序。所以player1(白棋)的a=1,player2(黑棋)的a=0,交换palyer1.a和player2.a的值,就交换了白棋和黑棋的下棋顺序。故有:
void swap(pplayer pplayer1,pplayer pplayer2)
{
(pplayer1->a)^=(pplayer2->a);
(pplayer2->a)^=(pplayer1->a);
(pplayer1->a)^=(pplayer2->a);
}
2.1.4.检查棋盘是否已满
每下一次棋后,根据现有的二维数组来判断棋盘是否已满。则有:
int countpiece(int (*boardAdd)[15])
{
int i,j;
int count=0;
for(i=0;i<15;i++)
{
for(j=0;j<15;j++)
{
if(boardAdd[i][j]!=0)
{
count+=1;
}
}
}
return count;
}
2.2.“人机模式”的实现
人机模式和对战模式相像,主要包括下一步棋的过程、判断玩家和电脑的胜负情况、检查棋盘是否已满。
2.2.1.下一步棋的过程
令ret=0,当ret%2=,0时,轮到玩家下棋,在下完一步棋后,ret++,如果ret%2=1时,轮到电脑下棋,根据上一步玩家下的棋,电脑将在其四周八个方向随机一个位置下棋,
具体实现过程:x,y是上一步玩家下的棋的横坐标和纵坐标,b,c是电脑棋子的数组的纵向下标、横向下标。如果随机生成的下标位置超出棋盘了,比如当b<0或b>15或c<0或c>15或随机生成的棋子位置上已有棋子发布时,有这5种情况的就要重新生成电脑棋子位置
srand((unsigned)time(NULL));
do
{
b = ((x + 4) / 48.5);//列
c = ((y - 98.5) / 48.5);//行
int a = rand() % 8;
if (a == 0)
{
c -= 1;//生成在正上
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 1)
{
c += 1;//生成在正下
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 2)
{
b -= 1;//生成在正左
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 3)
{
b += 1;//生成在正右
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 4)
{
b-=1,c-= 1;//生成在左上
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 5)
{
b -= 1, c += 1;//生成在左下
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 6)
{
b+=1,c -= 1;//生成在右上
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 7)
{
b+=1,c += 1;//生成在右下
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
i = (int)c;
j = (int)b;
} while (b < 0 || b>14 || c < 0 || c>14||boardAdd[i][j]!=0);
2.2.2.判断玩家和电脑的胜负情况
同理和对战模式的判定一样
2.2.3.检查棋盘是否已满
同理和对战模式的判定一样
3.保存存档,读取存档
要建两个文件夹,一个存对战模式的数据,一个存人机模式的数据,两个文件操作一模一样,因此下面只以对战模式为例。
总共有6个存档,创建一个顺序表:
typedef struct SequenceList
{
int* a;//存放存档
int size;//存档里的有效存档
int content;//总存档空间,共6个
} SL,*pSL;
a=(int*)malloc(6*15*15*sizeof(int));//动态分配6个存档的空间,一个存档有225个int类型
size=0;//有效个数初始为0,存一个档增加一个有效个数
content=6;//总存档空间,共6个
当游戏进行时保存文档,即在a=2,state=PEOPLE/AI的界面上点击了“保存游戏”后,动态内存里6个档位的对应位置的数据就被最新保存的二维数组数据覆盖。例如当点击保存游戏的次数为x%6,就把第x%6个档位的数据给覆盖了。然后进行文件操作把动态内存里的数据写入到文件中:
while(1)
{
int i,j;
FILE* pf=fopen("redo.txt","wb");
if(pf==NULL)
{
perror("fopen--wb:");
return;
}
for(i=0;i<25;i++)
{
for(j=0;j<15;j++)
{
*((SL.a+(size%6)*225)+i*15+j)=boardAdd[i][j];//覆盖数据
}
}
fwrite(SL.a,4,6*225,pf);//存入文档,
fclose(pf);
SL.size++;
}
注:空文档(即刚开始没有被覆盖数据的文档)在程序运行读取文档数据时,根据读取到多少个文档来将后面没有数据的档位的全赋值0,例如:
int i,j,k; FILE* pf=fopen("redo.txt","rb"); if(pf==NULL) { perror("fopen:--rb"); return; } int ret=fread(SL.a,4,6*225,pf);//fread函数的返回值是根据给定的每个元素的大小在文件中读取到的元素个数 SL.size=ret/6; if(size<0)//如果文档里没有数据,全赋值0 { for(k=0;k<6;k++) { for(i=0;i<25;i++) { for(j=0;j<15;j++) { *((SL.a+k*225)+i*15+j)=0;//覆盖数据 } } } } else if(size<5)//如果有存档无数据,则为其赋值0从而置为空文档 { for(k=size;k<6;k++) { for(i=0;i<25;i++) { for(j=0;j<15;j++) { *((SL.a+k*225)+i*15+j)=0;//覆盖数据 } } } }
当读取文档时,根据点击的文档编号来从文件中对应位置读取15*15个元素到二维数组中,然后进入游戏部分。读取文档操作如下:
//其中x时存档编码
void ReadData(int x, int(*boardAdd)[15])
{
FILE* pf = fopen("redo.txt", "rb");
if (pf == NULL)
{
perror("malloc::rb");
return;
}
fseek(pf, (x-1) * 225 * 4, SEEK_SET);//把文件指针调整到指定位置读取文件,把文件指针调整到从SEEK_SET位置开始往后走 (x-1) * 225 * 4个字节的位置处。
fread((int*)boardAdd, 4, 225, pf);//读取一个文档的数据
fclose(pf);
printf("读取存档%d成功!\n",x);
}
4.代码的具体实现
wuziqi.h
#pragma once
#define WINVER 0x0A00
#define _WIN32_WINNT 0x0A00
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <easyx.h>
#include <graphics.h>
#include <windows.h>
#include <ShellScalingApi.h>
#include <stdbool.h>
#include <time.h>
#pragma comment(lib,"Shcore.lib")
#define KEY_PRESS(vk) (GetAsyncKeyState(vk)&0x1?1:0)
typedef struct SequeList
{
int(*a)[15 * 15];
int num;
int content;
}SL, * pSL;
typedef struct player
{
int a;
double x;
double y;
int state;
int sequence;
}player, * pplayer;
enum state
{
HOME,
AI,
PEOPLE,
REDO,
};
enum playerstate
{
OK,
FAIL,
VICTORY,
};
int game(int* a,int* state,int* num, pplayer pplayer1, pplayer pplayer2, int(*boardAdd)[15], IMAGE* start, IMAGE* image_piece, pSL psl,pSL psl2);
int game_begain(int* a,int* state,pplayer pplayer1, pplayer pplayer2, int(*boardAdd)[15], IMAGE* image_sart, IMAGE* image_piece, pSL psl);
void swap(pplayer pplayer1, pplayer pplayer2);
bool isIn(POINT* msg, int x, int y, int width, int height);
void ReadDataPEOPLE(int x,int (*boardAdd)[15], pSL psl);
void ReadDataAI(int x,int (*boardAdd)[15], pSL psl);
int redotest(int* a,int* state,int(*boardAdd)[15], IMAGE* image_start, IMAGE* image_piece, pSL psl,pSL psl2);
wuzuqi.cpp
#include "wuziqi.h"
void swap(pplayer pplayer1, pplayer pplayer2)
{
int tmp = pplayer1->sequence;
pplayer1->sequence = pplayer2->sequence;
pplayer2->sequence = tmp;
}
int countpiece(int(*boardAdd)[15])
{
int i, j, count = 0;
for (i = 0; i < 15; i++)
{
for (j = 0; j < 15; j++)
{
if (boardAdd[i][j] != 0)
{
count++;
}
}
}
return count;
}
bool isIn(POINT* msg, int x, int y, int width, int height)
{
if (msg->x >= x && msg->x <= x + width && msg->y >= y && msg->y <= y + height)
{
return true;
}
return false;
}
void putMap(pplayer pplayer, int(*boardAdd)[15], IMAGE* image_start, IMAGE* image_piece, int h)
{
int i, j;
double b ,c ;
BeginBatchDraw();
putimage(0, 0, image_start + h);
putimage(pplayer->x, pplayer->y, image_piece + 4);
for (i = 0; i < 15; i++)
{
for (j = 0; j < 15; j++)
{
if (boardAdd[i][j] == 1)
{
b = j * 48.5 - 4;
c = 98.5 + i * 48.5;
if (b != pplayer->x || c != pplayer->y)
{
putimage(b, c, image_piece + 2);
}
}
if (boardAdd[i][j] == -1)
{
b = j * 48.5 - 4;
c = 98.5 + i * 48.5;
if (b != pplayer->x || c != pplayer->y)
{
putimage(b, c, image_piece + 3);
}
}
}
}
EndBatchDraw();
}
void putMapAI(int(*boardAdd)[15], IMAGE* image_start, IMAGE* image_piece)
{
int i, j;
double b, c;
BeginBatchDraw();
putimage(0, 0, image_start + 4);
for (i = 0; i < 15; i++)
{
for (j = 0; j < 15; j++)
{
if (boardAdd[i][j] == 1)
{
b = j * 48.5 - 4;
c = 98.5 + i * 48.5;
putimage(b, c, image_piece + 2);
}
if (boardAdd[i][j] == -1)
{
b = j * 48.5 - 4;
c = 98.5 + i * 48.5;
putimage(b, c, image_piece + 3);
}
}
}
EndBatchDraw();
}
int readypiece(int* a,int* state,pplayer pplayer, int(*boardAdd)[15], IMAGE* image_start, IMAGE* image_piece, pSL psl)
{
HWND handle = GetHWnd();
POINT msg;
int n = 1;
int b, c;
while (n)
{
b = (int)((pplayer->x + 4) / 48.5);
c = (int)((pplayer->y - 98.5) / 48.5);
if (*state == PEOPLE)
{
putMap(pplayer, boardAdd, image_start, image_piece, 5);
}
if (*state == AI)
{
putMap(pplayer, boardAdd, image_start, image_piece, 4);
}
if (KEY_PRESS(0x57) && pplayer->y > 98.5 && pplayer->y <= 777.5)//'W'键
{
pplayer->y -= 48.5;
}
if (KEY_PRESS(0x53) && pplayer->y < 777.5 && pplayer->y >= 98.5)//'S'键
{
pplayer->y += 48.5;
}
if (KEY_PRESS(0x41) && pplayer->x > -4 && pplayer->x <= 675)//'A'键
{
pplayer->x -= 48.5;
}
if (KEY_PRESS(0x44) && pplayer->x >= -4 && pplayer->x < 675)//'D'键
{
pplayer->x += 48.5;
}
if (KEY_PRESS(VK_RETURN) && boardAdd[c][b] == 0)
{
n = 0;
}
if (KEY_PRESS(VK_LBUTTON))
{
int num1;
GetCursorPos(&msg);
ScreenToClient(handle, &msg);
if (*state == PEOPLE)
{
if (isIn(&msg, 155, 830, 400, 80))
{
FILE* pf = fopen("redo.txt", "wb");
if (pf == NULL)
{
perror("malloc::wb");
return 1;
}
num1 = (psl->num % 6);
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
*((int*)(psl->a + num1) + i * 15 + j) = boardAdd[i][j];
}
}
psl->num++;
fwrite((int*)(psl->a), 4, 6 * 15 * 15, pf);
fclose(pf);
printf("保存完成!\n");
printf("已存放到PEOPLE存档%d!\n", num1 + 1);
}
}
if (*state == AI)
{
if (isIn(&msg, 155, 830, 400, 80))
{
FILE* pf = fopen("redo1.txt", "wb");
if (pf == NULL)
{
perror("malloc::wb");
return 1;
}
num1 = (psl->num % 6);
for (int i = 0; i < 15; i++)
{
for (int j = 0; j < 15; j++)
{
*((int*)(psl->a + num1) + i * 15 + j) = boardAdd[i][j];
}
}
psl->num++;
fwrite((int*)(psl->a), 4, 6 * 15 * 15, pf);
fclose(pf);
printf("保存完成!\n");
printf("已存放到AI存档%d!\n", num1 + 1);
}
}
if (isIn(&msg, 0, 0, 200, 150))
{
*a = 3;
}
if ((*a==3)&&(*state==AI))
{
while (1)
{
putimage(0, 0, image_start + 7);
if (KEY_PRESS(VK_LBUTTON))
{
GetCursorPos(&msg);
ScreenToClient(handle, &msg);
if (isIn(&msg, 120, 565, 90, 95))
{
return 0;
}
if (isIn(&msg, 530, 575, 90, 95))
{
putMap(pplayer, boardAdd, image_start, image_piece,4 );
break;
}
}
}
*a = 2;
}
if (*a==3&&*state==PEOPLE)
{
while (1)
{
putimage(0, 0, image_start + 6);
if (KEY_PRESS(VK_LBUTTON))
{
GetCursorPos(&msg);
ScreenToClient(handle, &msg);
if (isIn(&msg, 120, 565, 90, 95))
{
return 0;
}
if (isIn(&msg, 530, 575, 90, 95))
{
putMap(pplayer, boardAdd, image_start, image_piece, 5);
break;
}
}
}
*a = 2;
}
}
}
if (boardAdd[c][b] == 0)
{
boardAdd[c][b] = pplayer->a;//记录当前所下的棋子
}
return 1;
}
int game_begain(int* a,int* state,pplayer pplayer1, pplayer pplayer2, int(*boardAdd)[15], IMAGE* image_start, IMAGE* image_piece, pSL psl)
{
pplayer1->x = pplayer2->x = 239 * 1.5 - 23;
pplayer1->y = pplayer2->y = 306 * 1.5 - 21;
if (*state == PEOPLE)
{
if (pplayer1->sequence)
{
printf("轮到白棋下了:\n");
if (readypiece(a,state,pplayer1, boardAdd, image_start, image_piece, psl) == 0)
{
return 0;
}
}
if (pplayer2->sequence)
{
printf("轮到黑棋下了:\n");
if (readypiece(a,state,pplayer2, boardAdd, image_start, image_piece, psl) == 0)
{
return 0;
}
}
}
else
{
if (pplayer1->sequence)
{
printf("轮到您下了:\n");
if (readypiece(a, state, pplayer1, boardAdd, image_start, image_piece, psl) == 0)
{
return 0;
}
}
if (pplayer2->sequence)
{
printf("轮到您下了:\n");
if (readypiece(a, state, pplayer2, boardAdd, image_start, image_piece, psl) == 0)
{
return 0;
}
}
}
return 1;
}
void readypiece_AI(double x,double y,pplayer pplayer, int(*boardAdd)[15], IMAGE* image_start, IMAGE* image_piece)
{
double b, c;
int i, j;
do
{
b = ((x + 4) / 48.5);//列
c = ((y - 98.5) / 48.5);//行
int a = rand() % 8;
if (a == 0)
{
c -= 1;//生成在正上
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 1)
{
c += 1;//生成在正下
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 2)
{
b -= 1;//生成在正左
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 3)
{
b += 1;//生成在正右
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 4)
{
b-=1,c-= 1;//生成在左上
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 5)
{
b -= 1, c += 1;//生成在左下
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 6)
{
b+=1,c -= 1;//生成在右上
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
if (a == 7)
{
b+=1,c += 1;//生成在右下
pplayer->x = b * 48.5 - 4;
pplayer->y = c * 48.5 + 98.5;
}
i = (int)c;
j = (int)b;
} while (b < 0 || b>14 || c < 0 || c>14||boardAdd[i][j]!=0);
boardAdd[i][j] = pplayer->a;
putMapAI(boardAdd, image_start, image_piece);
}
void game_begain_AI(pplayer pplayer1, pplayer pplayer2, int(*boardAdd)[15], IMAGE* image_start, IMAGE* image_piece)
{
if (pplayer1->sequence==0)
{
readypiece_AI(pplayer2->x,pplayer2->y,pplayer1, boardAdd, image_start, image_piece);
}
if (pplayer2->sequence==0)
{
readypiece_AI(pplayer1->x, pplayer1->y,pplayer2, boardAdd, image_start, image_piece);
}
}
void Isleft_Incline(pplayer pplayer, int(*boardAdd)[15])//左斜判断
{
int j = (int)((pplayer->x + 23 + 4) / 48.5), i = (int)((pplayer->y + 21 - 98.5) / 48.5), count = 0;
int b = 1, c = 1;//向左
int e = 1, f = 1;//向右
while (boardAdd[i - b][j - c] == pplayer->a|| boardAdd[i + e][j + f] == pplayer->a)//向左、右判断
{
if (boardAdd[i - b][j - c] == pplayer->a)
{
b++;
c++;
count++;
}
if (boardAdd[i + e][j + f] == pplayer->a)
{
e++;
f++;
count++;
}
if (count + 1 == 5)
{
pplayer->state = VICTORY;
}
}
}
void Isright_Incline(pplayer pplayer, int(*boardAdd)[15])//右斜判断
{
int j = (int)((pplayer->x + 23 + 4) / 48.5), i = (int)((pplayer->y + 21 - 98.5) / 48.5), count = 0;
int b = 1, c = 1;//向左
int e = 1, f = 1;//向右
while (boardAdd[i - b][j + c] == pplayer->a|| boardAdd[i + e][j - f] == pplayer->a)//向左、右判断
{
if (boardAdd[i - b][j + c] == pplayer->a)
{
b++;
c++;
count++;
}
if (boardAdd[i + e][j - f] == pplayer->a)
{
e++;
f++;
count++;
}
if (count + 1 == 5)
{
pplayer->state = VICTORY;
}
}
}
void Is_parallel(pplayer pplayer, int(*boardAdd)[15])//平行判断
{
int j = (int)((pplayer->x + 23 + 4) / 48.5), i = (int)((pplayer->y + 21 - 98.5) / 48.5), count = 0;
int b = 1;//向左
int c = 1;//向右
while (boardAdd[i - b][j] == pplayer->a||boardAdd[i + c][j] == pplayer->a)//向左、右判断
{
if (boardAdd[i - b][j] == pplayer->a)
{
b++;
count++;
}
if (boardAdd[i + c][j] == pplayer->a)
{
c++;
count++;
}
if (count+1==5)
{
pplayer->state = VICTORY;
}
}
}
void Is_vertical(pplayer pplayer, int(*boardAdd)[15])//垂直判断
{
int j = (int)((pplayer->x + 23 + 4) / 48.5), i = (int)((pplayer->y + 21 - 98.5) / 48.5), count = 0;
int b = 1;//向下
int c = 1;//向上
while (boardAdd[i][j - b] == pplayer->a|| boardAdd[i][j + c] == pplayer->a)
{
if (boardAdd[i][j - b] == pplayer->a)
{
b++;
count++;
}
if (boardAdd[i][j + c] == pplayer->a)
{
c++;
count++;
}
if (count + 1 == 5)
{
pplayer->state = VICTORY;
}
}
}
void game_end(pplayer pplayer1, pplayer pplayer2, int(*boardAdd)[15])
{
if (pplayer1->sequence == 1)
{
Isleft_Incline(pplayer1, boardAdd);//左斜判断
Isright_Incline(pplayer1, boardAdd);//右斜判断
Is_parallel(pplayer1, boardAdd);//平行判断
Is_vertical(pplayer1, boardAdd);//垂直判断
}
else
{
Isleft_Incline(pplayer2, boardAdd);//左斜判断
Isright_Incline(pplayer2, boardAdd);//右斜判断
Is_parallel(pplayer2, boardAdd);//平行判断
Is_vertical(pplayer2, boardAdd);//垂直判断
}
if (pplayer1->state == VICTORY)
{
printf("*******白棋胜利*******\n");
}
else if (pplayer2->state == VICTORY)
{
printf("*******黑棋胜利*******\n");
}
}
void game_endAI(pplayer pplayer1,pplayer pplayer2,int (*boardAdd)[15])
{
Isleft_Incline(pplayer1, boardAdd);//左斜判断
Isright_Incline(pplayer1, boardAdd);//右斜判断
Is_parallel(pplayer1, boardAdd);//平行判断
Is_vertical(pplayer1, boardAdd);//垂直判断
Isleft_Incline(pplayer2, boardAdd);//左斜判断
Isright_Incline(pplayer2, boardAdd);//右斜判断
Is_parallel(pplayer2, boardAdd);//平行判断
Is_vertical(pplayer2, boardAdd);//垂直判断
if (pplayer1->state == VICTORY)
{
if (pplayer1->a == 1)
{
printf("*******您胜利*******\n");
}
else
{
printf("*******人机胜利*******\n");
}
}
else if (pplayer2->state == VICTORY)
{
if (pplayer1->a == 1)
{
printf("*******您胜利*******\n");
}
else
{
printf("*******人机胜利*******\n");
}
}
}
int game(int* a,int* state,int* num, pplayer pplayer1, pplayer pplayer2, int(*boardAdd)[15], IMAGE* image_start, IMAGE* image_piece,pSL psl,pSL psl2)
{
int haha = 0;
if ((*a == 2 && *state == AI))
{
srand((unsigned)time(NULL));
do
{
if (haha % 2 == 0)
{
if (game_begain(a, state, pplayer1, pplayer2, boardAdd, image_start, image_piece, psl2) == 0)
{
return 1;
}
}
else if(haha%2==1)
{
game_begain_AI(pplayer1, pplayer2, boardAdd, image_start, image_piece);
}
game_endAI(pplayer1,pplayer2,boardAdd);
(*num) = countpiece(boardAdd);//检查棋盘是否已满
haha++;
} while (pplayer1->state == OK && pplayer2->state == OK && (*num) != 225);
Sleep(3000);
return 1;
}
else
{
do
{
if (game_begain(a,state,pplayer1, pplayer2, boardAdd, image_start, image_piece, psl) == 0)
{
return 1;
}
game_end(pplayer1, pplayer2, boardAdd);//判断游戏是否结束
swap(pplayer1, pplayer2);
(*num) = countpiece(boardAdd);//检查棋盘是否已满
} while (pplayer1->state == OK && pplayer2->state == OK && (*num) != 225);
Sleep(3000);
return 1;
}
}
int count_Add(int(*boardAdd)[15])
{
int i, j;
int count = 0;
for (i = 0; i < 15; i++)
{
for (j = 0; j < 15; j++)
{
count += boardAdd[i][j];
}
}
if (count == 0)
{
return 0;
}
if (count < 0)
{
return -1;
}
if (count > 0)
{
return 1;
}
}
int redotest(int* a,int* state,int (*boardAdd)[15],IMAGE* image_start,IMAGE* image_piece,pSL psl,pSL psl2)
{
player player1, player2;
player1.state = player2.state = OK;
if (count_Add(boardAdd)==0)
{
player1.sequence = 1;
player2.sequence = 0;
}
if(count_Add(boardAdd)==1)
{
player1.sequence = 0;
player2.sequence = 1;
}
if (count_Add(boardAdd) == -1)
{
player1.sequence = 1;
player2.sequence = 0;
}
player1.a = 1;
player2.a = -1;
int num = 0;
return game(a,state,&num, &player1, &player2, boardAdd, image_start, image_piece, psl,psl2);
}
void ReadDataPEOPLE(int x, int(*boardAdd)[15], pSL psl)
{
FILE* pf = fopen("redo.txt", "rb");
if (pf == NULL)
{
perror("malloc::rb");
return;
}
fseek(pf, (x - 1) * 225 * 4, SEEK_SET);
fread((int*)boardAdd, 4, 225, pf);
fclose(pf);
printf("读取PEOPLE存档%d成功!\n", x);
}
void ReadDataAI(int x, int(*boardAdd)[15], pSL psl)
{
FILE* pf = fopen("redo1.txt", "rb");
if (pf == NULL)
{
perror("malloc::rb");
return;
}
fseek(pf, (x - 1) * 225 * 4, SEEK_SET);
fread((int*)boardAdd, 4, 225, pf);
fclose(pf);
printf("读取AI存档%d成功!\n", x);
}
test.cpp
#include "wuziqi.h"
void initimage(IMAGE* image_start,IMAGE* image_piece)
{
int i;
for (i = 0; i < 9; i++)
{
char str[15] = { 0 };
sprintf(str,"./hehe%d.png",i);
loadimage((image_start + i), str, getwidth(), getheight());
}
loadimage((image_start + 9), "./haihai1.png", getwidth(), getheight());
loadimage((image_start + 10), "./haihai1.png", getwidth(), getheight());
for (i = 0; i < 5;i++)
{
char str[15] = { 0 };
sprintf(str, "./piece%d.png", i + 1);
loadimage((image_piece + i), str);
}
}
void gamestart(int* a,int* state, pSL psl,pSL psl2, IMAGE* image_start, IMAGE* image_piece)
{
player player1;//攻方--白棋
player player2;//守方--黑棋
int boardAdd[15][15] = { 0 };
player1.state = player2.state = OK;
player1.sequence = 1;
player2.sequence = 0;
player1.a = 1;
player2.a = -1;
while (1)
{
HWND handle = GetHWnd();
POINT msg;
if (KEY_PRESS(VK_LBUTTON))
{
GetCursorPos(&msg);
ScreenToClient(handle, &msg);
if (isIn(&msg, 580, 13, 125, 80))
{
player1.sequence = 0;
player2.sequence = 1;
break;
}
}
if (KEY_PRESS(VK_RBUTTON))
{
GetCursorPos(&msg);
ScreenToClient(handle, &msg);
if (isIn(&msg, 580, 13, 125, 80))
{
player1.sequence = 1;
player2.sequence = 0;
break;
}
}
}
int num = 0;
*a = game(a,state,&num, &player1, &player2, boardAdd, image_start, image_piece,psl,psl2);
}
void start(pSL sl,pSL sl2,int (*boardAdd)[15],int* a,int* state,int* state1,POINT* msg, IMAGE* image_start, IMAGE* image_piece)
{
HWND handle = GetHWnd();
if (KEY_PRESS(VK_LBUTTON))
{
GetCursorPos(msg);
ScreenToClient(handle, msg);
if (*a == 0)
{
if (isIn(msg, 220, 107, 360, 120))
{
*state = AI;
*a = 1;
}
if (isIn(msg, 220, 300, 360, 120))
{
*state = PEOPLE;
*a = 1;
}
if (isIn(msg, 220, 480, 360, 120))
{
*state = REDO;
*a = 1;
}
}
else
{
switch (*state)
{
case AI:
if (isIn(msg, 260, 310, 170, 200))
{
*a = 2;
}
if (isIn(msg, 0, 0, 130, 90))
{
*state = HOME;
*a = 0;
}
break;
case PEOPLE:
if (isIn(msg, 260, 310, 170, 200))
{
*a = 2;
}
if (isIn(msg, 0, 0, 130, 90))
{
*state = HOME;
*a = 0;
}
break;
case REDO:
if (isIn(msg, 0, 0, 130, 75)&&*a==1)
{
*state = HOME;
*a = 0;
}
if (*a == 1 && *state1 == REDO && isIn(msg, 183, 175, 370, 216))
{
*state1 = AI;
*a = 2;
}
if (*a == 1 && *state1 == REDO && isIn(msg, 200, 476, 350, 210))
{
*state1 = PEOPLE;
*a = 2;
}
else if (*a == 2 && isIn(msg, 0, 0, 130, 75) )
{
*a = 1;
*state1 = REDO;
}
else if (*state1 == PEOPLE && *a == 2)
{
if (isIn(msg, 30, 320, 225, 195))
{
*state = PEOPLE;
ReadDataPEOPLE(1, boardAdd, sl);
*state1 = REDO;
*a = redotest(a, state, boardAdd, image_start, image_piece, sl,sl2);
}
if (isIn(msg, 250, 320, 225, 195))
{
*state = PEOPLE;
ReadDataPEOPLE(2, boardAdd, sl);
*state1 = REDO;
*a = redotest(a, state, boardAdd, image_start, image_piece, sl, sl2);
}
if (isIn(msg, 467, 330, 225, 195))
{
*state = PEOPLE;
ReadDataPEOPLE(3, boardAdd, sl);
*state1 = REDO;
*a = redotest(a, state, boardAdd, image_start, image_piece, sl, sl2);
}
if (isIn(msg, 33, 518, 225, 195))
{
*state = PEOPLE;
ReadDataPEOPLE(4, boardAdd, sl);
*state1 = REDO;
*a = redotest(a, state, boardAdd, image_start, image_piece, sl, sl2);
}
if (isIn(msg, 250, 519, 225, 195))
{
*state = PEOPLE;
ReadDataPEOPLE(5, boardAdd, sl);
*state1 = REDO;
*a = redotest(a, state, boardAdd, image_start, image_piece, sl, sl2);
}
if (isIn(msg, 465, 515, 225, 195))
{
*state = PEOPLE;
ReadDataPEOPLE(6, boardAdd, sl);
*state1 = REDO;
*a = redotest(a, state, boardAdd, image_start, image_piece, sl, sl2);
}
}
else if (*state1 == AI && *a == 2)
{
if (isIn(msg, 30, 320, 225, 195))
{
*state = AI;
ReadDataAI(1, boardAdd, sl);
*state1 = REDO;
*a = redotest(a, state, boardAdd, image_start, image_piece, sl, sl2);
}
if (isIn(msg, 250, 320, 225, 195))
{
*state = AI;
ReadDataAI(2, boardAdd, sl);
*state1 = REDO;
*a = redotest(a, state, boardAdd, image_start, image_piece, sl, sl2);
}
if (isIn(msg, 467, 330, 225, 195))
{
*state =AI;
ReadDataAI(3, boardAdd, sl);
*state1 = REDO;
*a = redotest(a, state, boardAdd, image_start, image_piece, sl, sl2);
}
if (isIn(msg, 33, 518, 225, 195))
{
*state =AI;
ReadDataAI(4, boardAdd, sl);
*state1 = REDO;
*a = redotest(a, state, boardAdd, image_start, image_piece, sl, sl2);
}
if (isIn(msg, 250, 519, 225, 195))
{
*state = AI;
ReadDataAI(5, boardAdd, sl);
*state1 = REDO;
*a = redotest(a, state, boardAdd, image_start, image_piece, sl, sl2);
}
if (isIn(msg, 465, 515, 225, 195))
{
*state = AI;
ReadDataAI(6, boardAdd, sl);
*state1 = REDO;
*a = redotest(a, state, boardAdd, image_start, image_piece, sl, sl2);
}
}
break;
}
}
}
if (*a == 2)
{
if (*state == PEOPLE)
{
printf("点击开始游戏后,左键\"切换\"表示黑棋先手,右键\"切换\"表示白棋先手\n");
gamestart(a,state, sl,sl2,image_start, image_piece);
}
if (*state == AI)
{
printf("点击开始游戏后,左键\"切换\"表示黑棋先手,右键\"切换\"表示白棋先手\n");
gamestart(a,state, sl,sl2, image_start, image_piece);
}
if (*state == REDO)
{
if (*state1 == AI)
{
putimage(0, 0, image_start + 9);
}
if (*state1 == PEOPLE)
{
putimage(0, 0, image_start + 10);
}
}
}
if (*a == 1)
{
if (*state == AI)
{
putimage(0, 0, image_start + *state);
}
if (*state == PEOPLE)
{
putimage(0, 0, image_start + *state);
}
if (*state == REDO)
{
putimage(0, 0, image_start + *state);
}
}
if (*a == 0)
{
if (*state == HOME)
{
putimage(0, 0, image_start + *state);
}
}
}
void initsl(pSL sl)
{
sl->a = (int(*)[15 * 15])malloc(6 * 225*4);
sl->num = 0;
sl->content = 6;
}
void InitRedo(pSL sl)
{
FILE* pf = fopen("redo.txt", "rb");
if (pf == NULL)
{
perror("fopen--r::");
printf("读取文件redo.txt出错,重新启动程序\n");
return ;
}
int ret = fread((int*)(sl->a), 4, 6 * 225, pf);
if (ret == 0)
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 15; j++)
{
for (int k = 0; k < 15; k++)
{
*((int*)((sl->a) + i) + j * 15 + k) = 0;
}
}
}
}
else if (ret != 6 * 225)
{
for (int i = ret / 225; i < 6; i++)
{
for (int j = 0; j < 15; j++)
{
for (int k = 0; k < 15; k++)
{
*((int*)((sl->a) + i) + j * 15 + k) = 0;
}
}
}
}
fclose(pf);
sl->num = ret / 225;
}void InitRedo1(pSL sl2)
{
FILE* pf = fopen("redo1.txt", "rb");
if (pf == NULL)
{
perror("fopen--r::");
printf("读取文件redo1.txt出错,重新启动程序\n");
return;
}
int ret = fread((int*)(sl2->a), 4, 6 * 225, pf);
if (ret == 0)
{
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 15; j++)
{
for (int k = 0; k < 15; k++)
{
*((int*)((sl2->a) + i) + j * 15 + k) = 0;
}
}
}
}
else if (ret != 6 * 225)
{
for (int i = ret / 225; i < 6; i++)
{
for (int j = 0; j < 15; j++)
{
for (int k = 0; k < 15; k++)
{
*((int*)((sl2->a) + i) + j * 15 + k) = 0;
}
}
}
}
fclose(pf);
sl2->num = ret / 225;
}
int main()
{
IMAGE image_start[11];
IMAGE image_piece[5];
int boardAdd[15][15] = { 0 };
SL sl,sl2;
int state = HOME;
int state1 = REDO;
int a = 0;
SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
initgraph(467 * 1.5, 609 * 1.5, EW_SHOWCONSOLE);
initimage(image_start,image_piece);
initsl(&sl);
initsl(&sl2);
InitRedo(&sl);
InitRedo1(&sl2);
printf("退回到主页,再点击ESC键离开游戏\n");
while (1)
{
if (KEY_PRESS(VK_ESCAPE))
{
break;
}
POINT msg;
start(&sl,&sl2,boardAdd,&a,&state,&state1,&msg, image_start, image_piece);
}
printf("已退出游戏\n");
}