首页 > 学院 > 开发设计 > 正文

五子棋

2019-11-08 02:28:20
字体:
来源:转载
供稿:网友
/*1、画一个19*19的棋盘2、定义一个棋盘数组3、光标移动的函数4、落子的函数5、判定是否落子的函数6、判定输赢的函数7、执行函数8、重新开始函数 */#include<stdio.h>#include<conio.h>#include<windows.h>#include<string.h>#include<stdlib.h>void  exe();void drawBoard();void gotoxy(int x,int y); void move();void init();  void border();void  luozi();void blance();void  record();void inittow(); void after();#define  width  19#define  height 19int k,l,m,n;int count;//棋盘 int chessboard[height][width];//设置一个flag判定是否已经落子int flag=1;char PRess; //记录当前光标位置的结构体typedef struct position{int x;int y; }position; position chess;  //胜利之后的动作 void after()  {  char a;  if(count==5){gotoxy(0,20);if(!flag==0)printf("玩家%d胜利/n",!flag+2);elseprintf("玩家%d胜利/n",!flag);printf("是否重新开始一局?Y/N/n");scanf("%s",&a);switch(a){case 'Y':exe();break;case 'N':exit(0);break;}} }//画棋盘 void drawBoard(){int i,j;system("cls");for(i=0;i<height;i++){for(j=0;j<width;j++){printf("十");chessboard[i][j]=10;}printf("/n");}}// 光标移动的函数void gotoxy(int x,int y) {COORD c;c.X=2*x;c.Y=y;SetConsoleCursorPosition (GetStdHandle(STD_OUTPUT_HANDLE), c); //修改当前光标的位置}//判定是否出界void border(){if(chess.x<19&&chess.y<19&&chess.x>=0&&chess.y>=0){gotoxy(chess.x,chess.y);} } //落子的函数void  luozi(){if(flag){printf("●"); chessboard[chess.y][chess.x]=1;flag=!flag;}else{printf("○"); chessboard[chess.y][chess.x]=0;flag=!flag;} }//判定初始化void inittow(){count=0;k=chess.y;m=chess.y;l=chess.x;n=chess.x;  } //记录 void  record(){//竖直方向inittow();while(chessboard[k][l]==(!flag)||chessboard[m][n]==(!flag)){if(chessboard[k][l]==(!flag)){k--;if(chessboard[k][l]==(!flag));count++;}if(chessboard[m][n]==(!flag)){m++;if(chessboard[m][n]==(!flag))count++; }}after(); //水平方向inittow();while(chessboard[k][l]==!flag||chessboard[m][n]==!flag){if(chessboard[k][l]==(!flag)){l--;if(chessboard[k][l]==(!flag));count++;}if(chessboard[m][n]==(!flag)){n++;if(chessboard[m][n]==(!flag))count++; }}after(); //左上到右下inittow();while(chessboard[k][l]==!flag||chessboard[m][n]==!flag){if(chessboard[k][l]==(!flag)){k--;l--;if(chessboard[k][l]==(!flag));count++;}if(chessboard[m][n]==(!flag)){m++;n++;if(chessboard[m][n]==(!flag))count++; }}after();//右上到左下 inittow();while(chessboard[k][l]==!flag||chessboard[m][n]==!flag){if(chessboard[k][l]==(!flag)){k++;l--;if(chessboard[k][l]==(!flag));count++;}if(chessboard[m][n]==(!flag)){m--;n++;if(chessboard[m][n]==(!flag))count++; }}after();}//判定输赢的函数void blance(){record();}//控制光标移动以及落子的函数 void move(){if(press){switch(press){case 'w':chess.y-=1;border();break;case 's':chess.y+=1;border(); break;case 'a':chess.x-=1;border(); break;case 'd':chess.x+=1;border(); break; case 'j':luozi();blance();break; }}} //初始化函数void init(){drawBoard();gotoxy(0,0);chess.x=0;chess.y=0; } //执行函数void  exe(){init();while(1){press=getch();move();}} int main(){system("color 2d");exe();gotoxy(19,19);}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表