gpt4 book ai didi

c++ - 输入二维数组 - C++(第一行/列元素问题)

转载 作者:行者123 更新时间:2023-11-30 05:48:53 24 4
gpt4 key购买 nike

我正在使用 C++ 编写 Tic-Tac-Toe 游戏。我为电路板创建了一个二维数组。我遇到的问题是当用户选择一个位置来放置他们的作品(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;
}

最佳答案

你可能遇到了 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!";
}

这应该产生完全相同的 behaviou.r

关于c++ - 输入二维数组 - C++(第一行/列元素问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28017965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com