我正在使用C ++编写Tic-Tac-Toe游戏。我为电路板创建了一个2D阵列。我遇到的问题是当用户选择放置他们的作品(X或O)的位置时。当用户选择第一行或第一列中的任何字母时,程序将结束。例如,如果用户选择中间单元格,则会正确放置用户的作品。任何帮助,将不胜感激。谢谢!
#include <iostream>
using namespace std;
char board[3][3] = { { 'A', 'B', 'C' }, { 'D', 'E', 'F' }, { 'G', 'H', 'I' } };
char player1 = 'X', player2 = 'O';
//displays board
void displayBoard() {
cout << "_________________" << endl;
cout << "| | | |" << endl;
cout << "| " << board[0][0] << " | " << board[0][1] << " | "
<< board[0][2] << " |" << endl;
cout << "|____|_____|____|" << endl;
cout << "| | | |" << endl;
cout << "| " << board[1][0] << " | " << board[1][1] << " | "
<< board[1][2] << " |" << endl;
cout << "|____|_____|____|" << endl;
cout << "| | | |" << endl;
cout << "| " << board[2][0] << " | " << board[2][1] << " | "
<< board[2][2] << " |" << endl;
cout << "| | | |" << endl;
cout << "|_______________|" << endl;
}
//check if game is over
bool checkStatus(char piece) {
bool over = false;
if ((board[0][0] == piece) && (board[0][1] = piece)
&& (board[0][2] = piece)) {
over = true;
} else if ((board[1][0] == piece) && (board[1][1] = piece) && (board[1][2] =
piece)) {
over = true;
} else if ((board[2][0] == piece) && (board[2][1] = piece) && (board[2][2] =
piece)) {
over = true;
} else if ((board[0][0] == piece) && (board[1][0] = piece) && (board[2][0] =
piece)) {
over = true;
} else if ((board[0][1] == piece) && (board[1][1] = piece) && (board[2][1] =
piece)) {
over = true;
} else if ((board[0][2] == piece) && (board[1][2] = piece) && (board[2][2] =
piece)) {
over = true;
} else if ((board[0][0] == piece) && (board[1][1] = piece) && (board[2][2] =
piece)) {
over = true;
} else if ((board[0][2] == piece) && (board[1][1] = piece) && (board[2][0] =
piece)) {
over = true;
} else {
over = false;
}
return over;
}
//gameplay
void game(char player1, char player2, bool current) {
bool turn = current; //true:player 1; false:player2
int row = 0, column = 0;
char position;
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 = (position - 'A') / 3;
column = (position - 'A') % 3;
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);
}
}
}
int main() {
int choice;
std::cout << "Welcome to Tic-Tac-Toe\n"
"1 player game, enter 1\n"
"2 player game, enter 2" << endl;
while (!(cin >> choice) || choice < 1 || choice > 2) {
cout
<< "Bad input. Try again!\nfor a 1 player game, enter 1\nfor a 2 plater game, enter 2"
<< endl;
cin.clear();
cin.ignore(INT_MAX, '\n');
}
//two player game
if (choice != 1) {
cout << "Tic-Tac-Toe: 2 Players" << endl;
game(player1, player2, true);
} else {
}
return 0;
}
答案 0 :(得分:1)
你可能会对c中最古老,最常见的编程错误之一祈祷:if (a==b)
vs if (a=b)
checkStatus
中的所有检查都是这样的:
if ((board[0][0] == piece) && (board[0][1] = piece) && (board[0][2] = piece))
当他们看起来像这样:
if ((board[0][0] == piece) && (board[0][1] == piece) && (board[0][2] == piece))
另外,就游戏逻辑而言,您可以在switch语句之后压缩代码
if (board[row][column] != 'X' && board[row][column] != 'O') {
if (turn) board[row][column] = 'X';
else board[row][column] = 'O';
turn = !turn;
} else {
cout << "Choose a valid cell!";
}
这应该产生完全相同的行为。