1、五子棋 C 语言程序代码#include #include #include void drawPanel();int isWin(int,int);int color = 1; /1-红色 2-白色 int chessman1515;/主函数void main()initgraph(620,620); /产生窗体/画棋盘drawPanel();/画棋子/1.定义鼠标事件MOUSEMSG m;HWND wnd = GetHWnd(); /定义当前窗体的句柄while(true)m = GetMouseMsg(); /获取鼠标事件对象if(m.uMsg = WM_LBUTTONDOWN)/获取点
2、击的坐标int x = m.x;int y = m.y;/换算成二维数组中的下标int i = x/40;int j = y/40;/显示点击的坐标/*char msg100;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);chessmanij = 1;else if(color=2)setfillstyle(RGB(255,255,255);fillcircle(40*i+20,40*j+
3、20,20);chessmanij = 2;/判断输赢 result = 1int 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;i15;i+)if(chessmaniy=color)count+;if(count=5)return 1;elsecount=0;/竖向for(i=0;i15;i+)if(chessmanxi=color)count+;if(count=5)return 1;elsecount=0;return 0; void drawPanel()int i;/画横线for(i=0;i15;i+)line(20,20+40*i,20+14*40,20+40*i);/画竖线for(i=0;i15;i+)line(20+40*i,20,20+40*i,20+14*40);