当前位置:文档之家› 五子棋游戏开发总结(C语言版)

五子棋游戏开发总结(C语言版)

五子棋游戏开发总结一.五子棋游戏概述略二.游戏功能对弈游戏,自动判断游戏胜负。

采用人机对话模式,界面设计美观友好。

具体玩法:采用任意键开始游戏,此处采用键盘值W、A、S、D控制棋子移动方向,空格键(SPACE)落子,ESC退出游戏。

三.系统开发平台1)Visual C++ 单纯的Visual C++ 不行,需下载一个EasyX小插件对其进行配置好才可(网上有下的,也有配置方法)。

2)程序代码必须以.cpp结尾。

3)最佳分辨率:最佳效果1024*768。

四.游戏开发详细过程:五.游戏的完整详细代码:#include <stdio.h>#include <graphics.h> //图形库#include <conio.h>#include <bios.h>#include <stdlib.h>//宏定义#define ESC 27 //退出#define SPACE 32#define LEFT 65 //向左键#define RIGHT 68#define UP 87#define DOWN 83int key;int chess[20][20]; //棋盘坐标位置int flag=1;//标识要画的棋子的颜色flag=1,棋子为蓝色;其他为红色。

int chessx,chessy;void start();void draw_chessboard();void draw_circle(int x,int y,int color);void play();int result(int x,int y);//开始游戏void start(){outtextxy(200,240,"GAME START!");outtextxy(200,380,"ESC-exit/press any key to continue ");}//画棋盘void draw_chessboard(){int i,j;setbkcolor(GREEN);//设置背景颜色为绿色cleardevice();//清屏for(i=40;i<=440;i+=20)for(j=40;j<=440;j++){putpixel(i,j,4);putpixel(j,i,4);}setcolor(8);setlinestyle(1,0,1);rectangle(32,32,448,448);outtextxy(10,10,"ESC-exit/SPACE-put a piece"); }//画棋子void draw_circle(int x,int y,int color){setcolor(color);setlinestyle(1,0,10);x=(x+2)*20;y=(y+2)*20;circle(x,y,4);}//清除棋子void draw_pixel(int x,int y,int color){x=(x+2)*20;y=(y+2)*20;{int a,b,c,d;for(a=1;a<=8;a++)putpixel(x+a,y,color);for(b=8;b>=1;b--)putpixel(x,y-b,color);for(c=1;c<=8;c++)putpixel(x,y+c,color);for(d=8;d>=1;d--)putpixel(x-d,y,color);putpixel(x+9,y,color);putpixel(x,y-9,color);putpixel(x,y+9,color);putpixel(x-9,y,color);}}//游戏过程void play(){int i,j;switch(key){case LEFT://棋子左移if(chessx-1<0)break;{for(i=chessx-1,j=chessy;i>=1;i--)if(chess[i][j]==0){draw_circle(chessx,chessy,GREEN);draw_pixel(chessx,chessy,8);break;}if(i<1)break;chessx=i;if(flag==1)draw_circle(chessx,chessy,BLUE);elsedraw_circle(chessx,chessy,RED);}break;case RIGHT://棋子右移if((chessx+1)>19)break;else{for(i=chessx+1,j=chessy;i<=19;i++)if(chess[i][j]==0){draw_circle(chessx,chessy,GREEN);draw_pixel(chessx,chessy,8);break;}if(i>19)break;chessx=i;if(flag==1)draw_circle(chessx,chessy,BLUE);elsedraw_circle(chessx,chessy,RED);}break;case DOWN://棋子下移if((chessy+1)>19)break;{for(i=chessx,j=chessy+1;j<=19;j++)if(chess[i][j]==0){draw_circle(chessx,chessy++,GREEN);draw_pixel(chessx,chessy,8);break;}if(j>19)break;chessx=i;if(flag==1)draw_circle(chessx,chessy,BLUE);elsedraw_circle(chessx,chessy,RED);}break;case UP://棋子上移if((chessy-1)<0)break;else{for(i=chessx,j=chessy-1;j>=1;j--)if(chess[i][j]==0){draw_circle(chessx,chessy,GREEN);draw_pixel(chessx,chessy,8);break;}if(j<1)break;chessy=j;if(flag==1)draw_circle(chessx,chessy,BLUE);elsedraw_circle(chessx,chessy,RED);}break;case ESC://退出游戏break;case SPACE://落子if(chessx>=1&&chessy<=19&&chessx<19&&chessy>=1){if(chess[chessx][chessy]==0){chess[chessx][chessy]=flag;if(result(chessx,chessy)==1){cleardevice();if(flag==1){cleardevice();outtextxy(80,200,"CONGRATIONA TIONS,BLUE WIN !");getch();closegraph();exit(0);}if(flag==2){cleardevice();outtextxy(80,200,"CONGRATIONA TIONS,RED WIN !");getch();closegraph();exit(0);}}if(flag==1)flag=2;elseflag=1;break;}}elsebreak;}}//判断胜负int result(int x,int y){int j,k,n1,n2;while(1){n1=0;n2=0;for(j=x,k=y;j>=1&&k>=1;j--,k--) {if(chess[j][k]==flag)n1++;elsebreak;}for(j=x,k=y;j<=19&&k<=19;j++,k++) {if(chess[j][k]==flag)n2++;elsebreak;}if(n1+n2-1>=5)return(1);n1=0;n2=0;for(j=x,k=y;j<=19&&k>=1;j++,k--) {if(chess[j][k]==flag)n1++;elsebreak;}for(j=x,k=y;j>=1&&k<=19;j--,k++) {if(chess[j][k]==flag)n2++;elsebreak;}if(n1+n2-1>=5)return(1);n1=0;n2=0;for(j=x,k=y;j>=1;j--){if(chess[j][k]==flag)n1++;elsebreak;}for(j=x,k=y;j<=19;j++){if(chess[j][k]==flag)n2++;elsebreak;}if(n1+n2-1>=5)return(1);n1=0;n2=0;for(j=x,k=y;k>=1;k--){if(chess[j][k]==flag)n1++;elsebreak;}for(j=x,k=y;k<=19;k++){if(chess[j][k]==flag)n1++;elsebreak;}if(n1+n2-1>=5)return(1);return (0);}}//主函数int main(void){initgraph(620,480);start();key=getch();if(key==ESC)exit(0);else{cleardevice();flag=1;draw_chessboard();do{chessx=10;chessy=10;if(flag==1)draw_circle(chessx,chessy,BLUE);elsedraw_circle(chessx,chessy,RED);do{while(getch()==0);key=getch();play();}while(key!=ESC&&key!=SPACE);}while(key!=ESC);closegraph();}return 0;}作者:flybird时间:2012/08/18。

相关主题