代码采用C语言编写,结合图形库,实现五子棋小游戏,代码如下:
#include
#include
#include
#include
#include
#include
#define MAXROW 1000
#define MAXCOL 500
int visb[MAXROW][MAXCOL];//记录黑棋
#define VISBF "visb.dat"
int visw[MAXROW][MAXCOL];//记录白棋
#define VISWF "visw.dat"
int bwin;//标记黑棋获胜
int wwin;//标记白棋子获胜
int step;
int tmpb[MAXROW][MAXCOL];
int tmpw[MAXROW][MAXCOL];
mouse_msg msg = {0};
void caozuo();
void saveWPositionToFile();
void saveBPositionToFile();
void ZhuYe()//主页
{
wwin=0;
bwin=0;
memset(visb,0,sizeof(visb));
memset(visw,0,sizeof(visw));//退出后仍可以再来一盘 (内存初始化)
memset(tmpb, 0, sizeof(tmpb));
memset(tmpw, 0, sizeof(tmpw));
saveWPositionToFile();
saveBPositionToFile();
initgraph(1000, 500);
setcaption("高艾祺_1192904001_五子棋游戏");
setbkcolor(EGERGB(0x69, 0x69, 0x69)); //设置背景颜色
setfillcolor(EGERGB(0xB0,0xD8,0xE6)) ;
bar(350,350,650,500);
bar(350,100,650,250);
setbkmode(TRANSPARENT);//文字填充透明化
setcolor(EGERGB(0x0, 0x0, 0x0));//字体颜色
setfont(40, 15, "楷体");
outtextxy(450,150,"双人对战");
setfont(40,15,"楷体");
outtextxy(450,400,"开始游戏");
}
void saveWPositionToFile()
{
FILE* fpw = fopen(VISWF, "wb");
int rowIndex = 0;
visw[0][0] = 123;
for (; rowIndex < MAXROW; rowIndex++)
{
fwrite(visw[rowIndex], MAXCOL * sizeof(int), 1, fpw);
}
fclose(fpw);
}
void saveBPositionToFile()
{
FILE* fpb = fopen(VISBF, "wb");
int rowIndex = 0;
for (; rowIndex < MAXROW; rowIndex++)
{
fwrite(visb[rowIndex], MAXCOL * sizeof(int), 1, fpb);
}
fclose(fpb);
}
void readWPositionFromFile()
{
FILE* fpw = fopen(VISWF, "rb");
if (fpw)
{
int rowIndex = 0;
for (; rowIndex < MAXROW && !feof(fpw); rowIndex++)
{
fread(tmpw[rowIndex], MAXCOL * sizeof(int), 1, fpw);
}
fclose(fpw);
}
}
void readBPositionFromFile()
{
FILE* fpb = fopen(VISBF, "rb");
if (fpb)
{
int rowIndex = 0;
for (; rowIndex < MAXROW; rowIndex++)
{
fread(tmpb[rowIndex], MAXCOL * sizeof(int), 1, fpb);
}
fclose(fpb);
}
}
int jud()
{
readWPositionFromFile();
readBPositionFromFile();
for(int i=60;i<=480;i++)
{
for(int j=60;j<=480;j++)
{
if (tmpb[i][j] && tmpb[i + 30][j] && tmpb[i + 60][j] && tmpb[i - 30][j] && tmpb[i - 60][j])
return 1;//横向获胜
else if (tmpb[i][j] && tmpb[i][j + 30] && tmpb[i][j + 60] && tmpb[i][j - 30] && tmpb[i][j - 60])
return 1;//纵向获胜
else if (tmpb[i][j] && tmpb[i + 30][j + 30] && tmpb[i + 60][j + 60] && tmpb[i - 30][j - 30] && tmpb[i - 60][j - 60])
return 1;//斜向获胜
else if (tmpb[i][j] && tmpb[i - 30][j + 30] && tmpb[i - 60][j + 60] && tmpb[i + 30][j - 30] && tmpb[i + 60][j - 60])
return 1;
if (tmpw[i][j] && tmpw[i + 30][j] && tmpw[i + 60][j] && tmpw[i - 30][j] && tmpw[i - 60][j])
return 2;
else if (tmpw[i][j] && tmpw[i][j + 30] && tmpw[i][j + 60] && tmpw[i][j - 30] && tmpw[i][j - 60])
return 2;
else if (tmpw[i][j] && tmpw[i + 30][j + 30] && tmpw[i + 60][j + 60] && tmpw[i - 30][j - 30] && tmpw[i - 60][j - 60])
return 2;
else if (tmpw[i][j] && tmpw[i - 30][j + 30] && tmpw[i - 60][j + 60] && tmpw[i + 30][j - 30] && tmpw[i + 60][j - 60])
return 2;
}
}
}
void QiPan()//棋盘
{
setbkcolor(EGERGB(173, 92, 45));//设置背景颜色
setcolor(BLACK);
for(int i=60;i<510;i+=30)
{
line(i,60,i,480);
line(60,i,480,i);
}
setcolor(EGERGB(255, 255, 255));
setfont(40, 15, "楷体");
xyprintf(700,150,"白棋先行");
setcolor(EGERGB(0,0,0));
setfont(40, 15, "楷体");
xyprintf(700,200,"黑棋后行");
}
void zhankuang()//显示当前棋盘状况
{
setbkcolor(EGERGB(173, 92, 45));//设置背景颜色
setcolor(BLACK);
for(int i=60;i<510;i+=30)//显示棋盘
{
line(i,60,i,480);
line(60,i,480,i);
}
for(int i=60;i<=480;i++)
{
for(int j=60;j<=480;j++)
{
if(visw[i][j])//显示白棋
{
setcolor(EGERGB(255, 255, 255));
setfillcolor(EGERGB(255,255,255));
fillellipse(i,j,15,15);
}
else if(visb[i][j])//显示黑棋
{
setcolor(EGERGB(0, 0, 0));
setfillcolor(EGERGB(0,0,0));
fillellipse(i,j,15,15);
}
}
}
setcolor(EGERGB(255, 255, 255));
setfont(40, 15, "楷体");
xyprintf(700,150,"白棋:%d",(step-1)/2+(step-1)%2);
setcolor(EGERGB(0,0,0));
setfont(40, 15, "楷体");
xyprintf(700,200,"黑棋:%d",(step-1)/2);
setfillcolor(EGERGB(0x87,0xCE,0x0FF));
bar(700,400,850,450);
setcolor(EGERGB(0x0, 0x0, 0x0));
outtextxy(750,400,"退出");
if(bwin)
{
setcolor(EGERGB(0, 0, 0));
setbkmode(TRANSPARENT);
setfont(20, 10, "楷体");
outtextxy(630,350,"黑棋获胜,点击下方回到主菜单");
}
else if(wwin)
{
setcolor(EGERGB(255, 255, 255));
setbkmode(TRANSPARENT);
setfont(20, 10, "楷体");
outtextxy(650,350,"白棋获胜,点击下方退出键回到主菜单");
}
}
void choose()
{
for ( ; is_run(); delay_fps(60))//鼠标操作
{
while (mousemsg())
{
msg = getmouse();
}
if((int)msg.is_down()&&msg.x>=350&&msg.x<=650&&msg.y>=350&&msg.y<=500)
{
cleardevice();
caozuo();
break;
}
}
}
void fanhui()//退出至主页
{
for(; is_run(); )
{
msg = getmouse();
if((int)msg.is_down()&&(int)msg.x>=700&&msg.x<=850&&msg.y>=400&&msg.y<=450)//鼠标操作,“退出”所在文本框范围
{
cleardevice();
ZhuYe();
choose();
}
}
}
void caozuo()//鼠标操作
{
bwin=0;
wwin=0;
QiPan();//
step=1;//步数初始化
for ( ; is_run(); )
{
msg = getmouse();
if((int)msg.is_down()&&msg.x&&msg.y&&visw[msg.x/30*30][msg.y/30*30]==0&&visb[msg.x/30*30][msg.y/30*30]==0&&msg.x>=50&&msg.x<=485&&msg.y>=55&&msg.y<=485)
{
step++;//每走一步,步数+1
if((step)&1)
{
visb[msg.x/30*30][msg.y/30*30]=1;//奇数下黑棋
saveBPositionToFile();
}
if((step)%2==0)
{
visw[msg.x/30*30][msg.y/30*30]=1;//偶数下白棋
saveWPositionToFile();
}
if(jud()==1)
{
bwin=1;
}
else if(jud()==2)
{
wwin=1;
}
cleardevice();
zhankuang();
if(bwin||wwin)
break;
}
else if((int)msg.is_down()&&(int)msg.x>=700&&msg.x<=850&&msg.y>=400&&msg.y<=450)
{
cleardevice();
ZhuYe();
choose();
}
}
}
int main()
{
HWND hwndDOS = GetForegroundWindow();
ShowWindow(hwndDOS, SW_HIDE);
ZhuYe();//主菜单
choose();//菜单栏选择
fanhui();
closegraph();
return 0;
}