我正在做一个井字游戏。我已经能够在java中实现这一点,但我在使用C ++时遇到了一些困难。
我创建了2D数组:
char board[3][3] = { { 'A', 'B', 'C' }, { 'D', 'E', 'F' }, { 'G', 'H', 'I' } };
然后我有以下,这是实际的游戏玩法。我有其他功能可以检查游戏状态,另一个功能可以显示每个回合的棋盘。我遇到的问题是当任何玩家选择对应于第一行和第一列的单元格时。如果选择了这5个单元中的任何一个,程序就会结束。当用户选择其他4个单元格中的任何一个时,它会正确地将字母更改为玩家单元(X或O)。任何帮助,将不胜感激。谢谢!
while (checkStatus(player1) != true && checkStatus(player2) != true) {
//player 1
if (turn != false) {
std::cout << "Player 1 Turn. Choose a Letter" << endl;
}
//player2
else if(turn != true) {
std::cout << "Player 2 Turn. Choose a Letter" << endl;
}
displayBoard();
std::cin >> position;
position = toupper(position);
switch (position) {
case 'A':
row = 0;
column = 0;
break;
case 'B':
row = 0;
column = 1;
break;
case 'C':
row = 0;
column = 2;
break;
case 'D':
row = 1;
column = 0;
break;
case 'E':
row = 1;
column = 1;
break;
case 'F':
row = 1;
column = 2;
break;
case 'G':
row = 2;
column = 0;
break;
case 'H':
row = 2;
column = 1;
break;
case 'I':
row = 2;
column = 2;
break;
default:
cout << "You didn't enter a correct letter! Try again\n";
game(player1,player2,turn);
}
cin.clear();
//player X
if (turn != false && board[row][column] != 'X' && board[row][column] != 'O') {
board[row][column] = 'X';
turn = false;
checkStatus(player1);
checkStatus(player2);
}
//player O
else if (turn != true && board[row][column] != 'X' && board[row][column] != 'O') {
board[row][column] = 'O';
turn = true;
checkStatus(player1);
checkStatus(player2);
} else {
cout << "Choose a valid cell!";
game(player1, player2, turn);
}
}