c++圈圈叉叉游戏

c++圈圈叉叉游戏c 圈圈叉叉游戏 include iostream include string define SIZE 4 格子大小为 4 4 using std cout using std endl using std cin using std string char BLANK string iostream

大家好,我是讯享网,很高兴认识大家。

 

c++圈圈叉叉游戏 

 
讯享网

 

#include <iostream> #include<string> #define SIZE 4//格子大小为4*4 using std::cout; using std::endl; using std::cin; using std::string; char BLANK = '.'; char pointA = 'x'; char pointB = 'o'; static char board[SIZE][SIZE];//用来存格子里面的内容 static int firstMoveFlag = 0;//由于第一步不能走中心,这里做一个是否是第一步的标志判断 int * input() { string temp; int *n = new int[2]; cin >> temp;//将键盘输入存为string,temp相当于数组,里面存入的是char,你可以单步调试看变量里面的内容 n[0] = temp[1]-48;//输入(1,2)坐标,则序号为1的是横坐标,3是纵坐标 n[1] = temp[3]-48;//-48是因为数字0对应的ASCII码是48,什么是ASCII码可以google return n;//无法直接返回数组,这里返回的是数组的地址指针 } void displayBoards()//这个就是将格子里面的内容一个一个打印出来 { for (int i = 0;i < SIZE;i++) { for (int j = 0;j < SIZE;j++) { cout << board[i][j]; if (j >= 3) { cout << "\n"; } } } } class pointGame { public: void Amove()//A first move { int Row; int Column; int *p; do { cout << "playerA: Which coordinate would you like to place?\n"; p = input(); Row = *p + 1;//用指针来指向返回的内容,由于格子里面有第一行和第一列是序号,所以实际行数要加1 Column = *(p + 1) + 1; delete p; if (Row<1||Row>3||Column<1||Column>3)//如果返回的坐标数字不在1到3之间就说明输入有错, { cout << "Input error!Please try again!\n"; } if (firstMoveFlag == 0 && Row == 2 && Column == 2)//判断第一步是不是走的中心 { cout << "The first point cannot be the CENTER!\n"; } if (firstMoveFlag == 0 && (Row != 2 || Column != 2))//如果成功的走了第一步,就将标志firstMoveFlag变换掉 { firstMoveFlag = 1; } } while ((Row < 1 || Row > 3) || (Column < 1 || Column > 3)||firstMoveFlag==0||board[Row][Column]==pointB);//如果坐标不在1到3之间或者第一步没有走成功,或者A走的是B已经走过的了,就要重新输入坐标,知道走成功 board[Row][Column] = pointA;//走成功后将格子里的内容替换掉(原来是.) cout << "\n\n\n\n\n\n\n\n"; displayBoards();//将格子打印出来 } void Bmove()//这也是和A一样的,只是没了第一步的内容 { int Row; int Column; int *p; do { cout << "playerB: Which coordinate would you like to place?\n"; p = input(); Row = *p + 1; Column = *(p + 1) + 1; delete p; if (Row < 1 || Row>3 || Column < 1 || Column>3) { cout << "Input error!Please try again!\n"; } } while ((Row < 1 || Row > 3) || (Column < 1 || Column > 3) || board[Row][Column] == pointA); board[Row][Column] = pointB; cout << "\n\n\n\n\n\n\n\n"; displayBoards(); } int gameOver()//游戏是否结束 { int result = 0;//这个是用来判断相同符号能连起来的个数 //下面就是判断横纵还有斜方向上是否有三个相同的符号,全都是循环查询 //check the column for (int j = 1; j < SIZE; j++) { for (int i = 1;i < SIZE;i++) { if (board[i][j] == board[1][j]) { result++; } } if (result == 3 && board[1][j] == pointA)//如果连成三个相同的,并且是A走的 { return 1;//AӮ返回1代表A赢 } else if (result == 3 && board[1][j] == pointB) { return 2;//BӮ代表B赢 } else { result = 0;//代表没有谁赢 } } //check the row for (int j = 1; j < SIZE; j++) { for (int i = 1;i < SIZE;i++) { if (board[j][i] == board[j][1]) { result++; } } if (result == 3&& board[j][1]==pointA) { return 1; } else if (result == 3 && board[j][1] == pointB) { return 2; } else { result = 0; } } //check the X if (board[1][1] == board[2][2] && board[1][1] == board[3][3] && board[1][1] == pointA) return 1; if (board[1][3] == board[2][2] && board[1][3] == board[3][1] && board[1][3] == pointA) return 1; if (board[1][1] == board[2][2] && board[1][1] == board[3][3] && board[1][1] == pointB) return 2; if (board[1][3] == board[2][2] && board[1][3] == board[3][1] && board[1][3] == pointB) return 2; return 0; } }; int main() { int step = 0; cout << "Welcome to play the game!\n"; //下面这一段就是初始化格子,并把它打印出来 for (int i = 0;i < SIZE;i++) { for (int j = 0;j < SIZE;j++) { board[i][j] = '.'; } } board[0][0] = ' '; board[0][1] = '0'; board[0][2] = '1'; board[0][3] = '2'; board[1][0] = '0'; board[2][0] = '1'; board[3][0] = '2'; displayBoards();//打印格子 pointGame game; do { //第一步是A走 game.Amove(); step++;//这个是用来记录一共走了多少步的,如果是9步,就说明格子填满了,并且没有人赢 if (game.gameOver() == 1)//是不是A赢了 { cout << "PlayerA won! Have a cookie.\n"; break;//如果有人赢了,就跳出while循环 } game.Bmove(); step++; if (game.gameOver() == 2) { cout << "PlayerB won! Have a cookie.\n"; break; } } while (1);//一直循环,直到有人赢或者将格子填满 if (step == 9) { cout << "It's a draw!!\n";//平局 } } 

讯享网

 

小讯
上一篇 2025-04-05 10:17
下一篇 2025-03-09 07:42

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/53604.html