当前位置:文档之家› 五子棋C语言程序代码

五子棋C语言程序代码

五子棋C语言程序代码
五子棋C语言程序代码

五子棋C语言程序代码#include

#include

#include

void drawPanel();

int isWin(int,int);

int color = 1; //1-红色2-白色

int chessman[15][15];

//主函数

void main()

{

initgraph(620,620); //产生窗体

//画棋盘

drawPanel();

//画棋子

//1.定义鼠标事件

MOUSEMSG m;

HWND wnd = GetHWnd(); //定义当前窗体的句柄while(true)

{

m = GetMouseMsg(); //获取鼠标事件对象

if(m.uMsg == WM_LBUTTONDOWN)

{

//获取点击的坐标

int x = m.x;

int y = m.y;

//换算成二维数组中的下标

int i = x/40;

int j = y/40;

//显示点击的坐标

/*char msg[100];

sprintf(msg,"%d,%d",i,j);

MessageBoxA(wnd,msg,"消息",MB_OK);*/

if(color==1)

{

setfillstyle(RGB(255,0,0));

fillcircle(40*i+20,40*j+20,20);

chessman[i][j] = 1;

}

else if(color==2)

{

setfillstyle(RGB(255,255,255));

fillcircle(40*i+20,40*j+20,20);

chessman[i][j] = 2;

}

//判断输赢result => 1

int result = isWin(i,j);

if(result ==1)

{

if(color==1)

{

MessageBoxA(wnd,_T("恭喜,红方获胜!"),"消息",MB_OK);

}

else if(color==2)

{

MessageBoxA(wnd,_T("恭喜,白方获胜!"),"消息",MB_OK);

}

break;

}

//切换对方下子

color = color == 1 ? 2 : 1 ;

}

}

getch();

closegraph(); //关闭窗体

}

int isWin(int x,int y)

{

int count=0;//计数器

int i;

//横向

for(i=0;i<15;i++)

{

if(chessman[i][y]==color)

{

count++;

if(count==5)

return 1;

}else

{

count=0;

}

}

//竖向

for(i=0;i<15;i++)

{

if(chessman[x][i]==color)

{

count++;

if(count==5)

return 1;

}else

{

count=0;

}

}

return 0;

}

void drawPanel()

{

int i;

//画横线

for(i=0;i<15;i++)

{

line(20,20+40*i,20+14*40,20+40*i);

}

//画竖线

for(i=0;i<15;i++)

{

line(20+40*i,20,20+40*i,20+14*40);

}

}

相关主题
文本预览
相关文档 最新文档