#include//调用图形库函数
#include//getchar()头文件
#include//标准输入输出头文件
void game();
void play();
int judge(int a,int b);
int flag=0;//用于记录和区分棋手1或2
int board[20][20]={0};//用于表示棋盘上落子点的坐标
int main()
{
int a,b;
game();
play();
judge(a,b);
getchar();//防止闪退
return 0;
}
void game()
{
initgraph(740, 570);//设置绘图窗口大小
setbkcolor(BROWN);//设置棋盘背景颜色
setlinecolor(BLACK);
cleardevice();
for(int i=0;i<19;i++)//绘制棋盘界面
{
setlinestyle(PS_SOLID,1);//设置线条粗细
line(50+i*25,50,50+i*25,525);
line(50,50+i*25,525,50+i*25);
}
setfillcolor(BLACK);//设置填充颜色
solidcircle(625,150,25);
setlinestyle(PS_SOLID,2);
line(597,147,597,200);
line(597,200,650,200);
settextcolor(BLACK);//标志,字体颜色
setbkcolor(WHITE);//标志内字体颜色
settextstyle(47, 0, _T("华文行楷"));//设置字体
outtextxy(625,150,"奕");//在(625,150)处写字
settextcolor(WHITE);
setlinestyle(PS_SOLID,3);
line(570,205,720,205);
line(570,205,570,265);
line(570,265,720,265);
line(720,205,720,265);
settextstyle(26, 0, _T("楷体"));
setbkcolor(BROWN);
settextcolor(BLACK);
outtextxy(575,210,"棋手 1:黑子");//棋手显示
settextcolor(WHITE);
outtextxy(575,235,"棋手 2:白子");
setlinestyle(PS_SOLID,2);
line(50,49,50,525);//边界线条加粗
line(50,50,525,50);
line(50,525,525,525);
line(525,50,525,525);
settextcolor(RED);
settextstyle(15, 0, _T("楷体"));
outtextxy(605,554,"ZZD原创制作(^v^)");
}
void play()//落子操作
{
MOUSEMSG m;//鼠标消息
int a=0,b=0,x=0,y=0;
HWND hwnd;//窗口句柄
hwnd=GetHWnd();//窗口置前
while(1)//死循环作游戏主体
{
m=GetMouseMsg();//获取鼠标消息
for(int i=0;i<20;i++)//得到鼠标点击处坐标,并计算出落子位置
{
for(int j=0;j<20;j++)
{
if(abs(m.x-50-i*25)<12.5&&abs(m.y-50-j*25)<12.5)//用绝对值判断具体落子位置
{
a=i;
b=j;
x=i*25+50;
y=j*25+50;
}
}
}
if(m.uMsg==WM_LBUTTONDOWN)//判断当前消息为鼠标左键
{
if(m.x<537.5&&m.y<37.5)
{
MessageBox(hwnd,"该处不可落子,请重新选取落子位置!!!","#警告!!!",MB_OK);
continue;
}//提示不可落子
if(m.x>537.5&&m.y<537.5)
{
MessageBox(hwnd,"该处不可落子,请重新选取落子位置!!!","#警告!!!",MB_OK);
continue;
}
if(m.x>37.5&&m.y>537.5)
{
MessageBox(hwnd,"该处不可落子,请重新选取落子位置!!!","#警告!!!",MB_OK);
continue;
}
if(m.x<37.5&&m.y>37.5)
{
MessageBox(hwnd,"该处不可落子,请重新选取落子位置!!!","#警告!!!",MB_OK);
continue;
}
if(board[a][b]!=0)
{
MessageBox(hwnd,"该处已有棋子,请重新落子!!!","#警告!!!",MB_OK);//制作窗口确定键以及窗口内容
continue;
}
if(flag%2==0)
{
if(m.x<537.5&&m.y<37.5)
if(m.x>537.5&&m.y<537.5)
if(m.x>37.5&&m.y>537.5)
if(m.x<37.5&&m.y>37.5)
if(board[a][b]!=0)
continue;//不可落子处不落子
setfillcolor(BLACK);
solidcircle(x,y,10);//黑棋子
board[a][b]=1;
flag++;
}
else
{
if(m.x<537.5&&m.y<37.5)
if(m.x>537.5&&m.y<537.5)
if(m.x>37.5&&m.y>537.5)
if(m.x<37.5&&m.y>37.5)
if(board[a][b]!=0)
continue;
setfillcolor(WHITE);
solidcircle(x,y,10);//白棋子
board[a][b]=2;
flag++;
}
}
if(judge(a,b))
{
if(flag%2==1&&flag<=400)
{
MessageBox(hwnd,"游戏结束,恭喜:棋手1(执黑子)胜利(^v^); 胜败乃兵家常事,棋手2(执白子)也不要气馁哦!","恭喜(^v^)",MB_OK );
exit(0);//游戏结束,黑子胜
}
else if(flag%2==0&&flag<=400)
{
MessageBox(hwnd,"游戏结束,恭喜:棋手2(执白子)胜利(^v^); 胜败乃兵家常事,棋手1(执黑子)也不要气馁哦!","恭喜(^v^)",MB_OK );
exit(0);//游戏结束,白子胜
}
}
else if(flag>400)
{
MessageBox(hwnd,"游戏结束:平局。双方势均力敌呢","(^v^)",MB_OK );
exit(0);//游戏结束,平局
}
}
}
int judge(int a,int b)//判断输赢家
{
int i,j;
int t=2-flag%2;//用于判断和区分棋手
for(i=a-4,j=b;i<=a;i++)//横向比较
{
if(i>=0&&i<=16&&t==board[i][j]&&t==board[i+1][j]&&t==board[i+2][j]&&t==board[i+3][j]&&t==board[i+4][j])
return 1;
}
for(i=a,j=b-4;j<=b;j++)//纵向比较
{
if(j>=0&&j<=16&&t==board[i][j]&&t==board[i][j+1]&&t==board[i][j+2]&&t==board[i][j+3]&&t==board[i][j+4])
return 1;
}
for(i=a-4,j=b-4;i<=a,j<=b;i++,j++)//斜向下比较
{
if(j>=0&&j<=16&&i>=0&&i<=16&&t==board[i][j]&&t==board[i+1][j+1]&&t==board[i+2][j+2]&&t==board[i+3][j+3]&&t==board[i+4][j+4])
return 1;
}
for(i=a-4,j=b+4;i<=a,j>=1;i++,j--)//斜向上比较
{
if(j>=0&&j<=16&&i>=0&&i<=16&&t==board[i][j]&&t==board[i+1][j-1]&&t==board[i+2][j-2]&&t==board[i+3][j-3]&&t==board[i+4][j-4])
return 1;
}
return 0;
}