C++Win32(DOS)五子棋代码(三字连珠).docVIP

  • 72
  • 0
  • 约4.49千字
  • 约 12页
  • 2017-05-12 发布于河南
  • 举报
CWin32(DOS)五子棋代码(三字连珠)

三字连珠 实验要求: 模仿五子棋的形式,编写有一方三子或五子连成一线即获胜的程序,具体要求: 玩家可以设置棋盘的大小,即在游戏开始时输入一变量n(n=3~9),使棋盘的大小为n×n。 本程序用if-else选择语句来判断获胜者,如果棋盘变大这将非常频繁,现要求将这一功能用循环语句实现,且适用于任意大小的棋盘。 加入可以悔棋的功能,要求任意方可以在对方下棋后悔改一步,即将对方当前步和本方上一步删除,本方重新下棋。 程序代码(本页不足请附页) 类的定义: class Game { public: Game(int M); void print(); //输出棋盘(1) void print2(); //输出棋盘(2) void two_people(); //双人对战 int judge1(int a,int b); //判断玩家1胜利 int judge2(int a,int b); //判断玩家2胜利 void regret(int a,int b); //悔棋 private: int N; char game[10][10]; }; 类的实现: #includeiostream.h #include1.h void Game::print() //输出棋盘(1) { int i,j; cout ; for(i=1;iN+1;i++) cout i; coutendl; for(i=0;iN;i++) { for(j=0;j=N;j++) { if(j==0) couti+1 ; else cout. ; } coutendl; } } void Game::print2() //输出棋盘(2) { int i,j; cout ; for(i=1;iN+1;i++) cout i; coutendl; for(i=1;iN+1;i++) { for(j=0;j=N;j++) { if(j==0) couti ; else if(game[i][j]==O) coutO ; else if(game[i][j]==X) coutX ; else cout. ; } coutendl; } } void Game::regret(int a,int b) //悔棋 { game[a][b]=0; } void Game::two_people() { int flag=0; //判断玩家 int p1,p2; //下棋位置 print(); for(;;) { if(flag==0) { cout请玩家一输入下棋位置(玩家二要悔棋请输入两个0)endl; cinp1p2; if(p1==0p2==0) { cout请输入要悔棋的坐标endl; cinp1p2; if(game[p1][p2]!=X) { cout输入有误endl; continue; } regret(p1,p2); } else if(game[p1][p2]!=0) { cout输入有误endl; continue; } else if(p1=1p1=9p2=1p2=9) game[p1][p2]=O; else { cout输入位置有误,请重输endl; continue; } if(judge1(p1,p2)==1) { print2(); cout玩家一胜利!endlendl; break; } flag=1; print2(); } else if(flag==1) { cout请玩家二输入下棋位置(玩家一要悔棋请输入两个0)endl; cinp1p2; if(p1==0p2==0) { cout请输入要悔棋的坐标endl; cinp1p2; if(game[p1][p2]!=O) { cout输入有误endl; continue; } regret(p1,p2); } else if(game[p1][p2]!=0) { cout输入有误endl; continue; } else if(

文档评论(0)

1亿VIP精品文档

相关文档