简易版五子棋~~
简单说明~鼠标控制单击左键执行黑子~单击右键执行白子~由于初学绘图~这代码是参考网上课程的~还有很多不完善的地方~仅供参考~
注意~文件后缀是.cpp格式~~~~
程序代码:
#include<graphics.h> #include<stdio.h> #include<conio.h> #include<windows.h> #define N 28 void PlayGame(); int GameOver1(int chess[N][N],int flag); void BackGround() { IMAGE GameBackground; /* loadimage(&GameBackground,"GameBackground.jpg"); int width=GameBackground.getwidth(); int height=GameBackground.getheight(); initgraph(width,height); putimage(0,0,&GameBackground);*/ setlinecolor(BLACK); for (int i=20;i<=540;i+=20) { line(20,i,540,i); line(i,20,i,540); } setfillcolor(BLACK); fillcircle(100,280,2); fillcircle(280,100,2); fillcircle(460,100,2); fillcircle(100,460,2); fillcircle(460,460,2); fillcircle(100,100,2); fillcircle(460,280,2); fillcircle(280,460,2); fillcircle(280,280,2); } void messagebox() { setlinecolor(BLACK); setfillcolor(RGB(255,201,14)); fillrectangle(540,20,720,190);//左上角和右下角参数,确定一个矩形 settextstyle(50,28,"黑体"); //画提示框里面的内容 setbkmode(TRANSPARENT);//字体不背景透明处理 setlinecolor(BLACK); settextcolor(BLACK); outtextxy(550,50,"黑棋"); setfillcolor(BLACK); fillcircle(680,75,15); setlinecolor(WHITE); settextcolor(WHITE); outtextxy(550,110,"白棋"); setfillcolor(WHITE); fillcircle(680,135,15); PlayGame(); getchar(); getchar(); } void PlayGame() { //鼠标 int x,y,i,j; int chess[N][N]={0}; MOUSEMSG m; while (1) { m=GetMouseMsg(); switch(m.uMsg) { case WM_LBUTTONDOWN: if (m.x>=20&&m.x<=540&&m.y>=20&&m.y<=540) { x=m.x%20; if (x<=10) m.x=m.x-x; else m.x=m.x-x+20; y=m.y%20; if (y<=10) m.y=m.y-y; else m.y=m.y-y+20; i=m.x/20; j=m.y/20; if (chess[i][j]!=0) continue; setlinecolor(BLACK); setfillcolor(BLACK); fillcircle(m.x,m.y,8); chess[i][j]=1; if (GameOver1(chess,1)==1) { settextcolor(RED); settextstyle(50,50,"黑体"); outtextxy(100,200,"这局黑棋胜利"); } } break; case WM_RBUTTONDOWN: if (m.x>=20&&m.x<=550&&m.y>=20&&m.y<=550) { x=m.x%20; if (x<=10) m.x=m.x-x; else m.x=m.x-x+20; y=m.y%20; if (y<=10) m.y=m.y-y; else m.y=m.y-y+20; i=m.x/20; j=m.y/20; if (chess[i][j]!=0) continue; setlinecolor(WHITE); setfillcolor(WHITE); fillcircle(m.x,m.y,8); chess[i][j]=2; if (GameOver1(chess,2)==1) { settextcolor(WHITE); settextstyle(50,50,"黑体"); outtextxy(100,200,"这局白棋胜利"); } } break; } } } int GameOver1(int chess[N][N],int flag) { int i,j,k,low,row,tilt1,tilt2; for (i=0;i<N;i++) { for (k=0;k<N-4;k++) { low=row=tilt1=tilt2=0; for (j=k;j<k+5;j++) { if (chess[i][j]==flag) low++; if (chess[j][i]==flag) row++; if (chess[i+(j-k)][j]==flag) tilt1++; if (chess[i+4-(j-k)][j]==flag) tilt2++; } if (low==5||row==5||tilt1==5||tilt2==5) return 1; } } return 0; } int main() { initgraph(750,560); setbkcolor(RGB(255,100,0)); cleardevice(); BackGround(); messagebox(); getch(); closegraph(); return 0; }