资源描述:
《五子棋C语言程序代码》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、五子棋C语言程序代码#include#include#includevoiddrawPanel();intisWin(int,int);intcolor=1;//1-红色2-白色intchessman[15][15];//主函数voidmain(){initgraph(620,620);//产生窗体//画棋盘drawPanel();//画棋子//1.定义鼠标事件MOUSEMSGm;HWNDwnd=GetHWnd();//定义当前窗体的句柄while(true){m=GetMouseMsg();//获取鼠标事
2、件对象if(m.uMsg==WM_LBUTTONDOWN){//获取点击的坐标intx=m.x;inty=m.y;//换算成二维数组中的下标inti=x/40;intj=y/40;//显示点击的坐标/*charmsg[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;}elseif(color==2){setfill
3、style(RGB(255,255,255));fillcircle(40*i+20,40*j+20,20);chessman[i][j]=2;}//判断输赢result=>1intresult=isWin(i,j);if(result==1){if(color==1){MessageBoxA(wnd,_T("恭喜,红方获胜!"),"消息",MB_OK);}elseif(color==2){MessageBoxA(wnd,_T("恭喜,白方获胜!"),"消息",MB_OK);}break;}//切换对方下子color=color==1?2:1;}}getch();c
4、losegraph();//关闭窗体}intisWin(intx,inty){intcount=0;//计数器inti;//横向for(i=0;i<15;i++){if(chessman[i][y]==color){count++;if(count==5)return1;}else{count=0;}}//竖向for(i=0;i<15;i++){if(chessman[x][i]==color){count++;if(count==5)return1;}else{count=0;}}return0;}voiddrawPanel(){inti;//画横线for(i=0
5、;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);}}