我正在努力学习C ++并且我已经制作了小型的tictactoe游戏,但有些事情是错误的。我试图犯一个错误bool。但有些事情错了。我已经尝试了2个小时,我无法找到如何解决问题。怎么了?当我键入2 2,这应该工作时,弹出错误消息,它表示无效移动。但是X或O仍然会出现在电路板上 这是代码:
#include <iostream>
const int rows = 3;
const int elements = 3;
char SetPlayer;
char SetEnemyPlayer;
char chooseTurn = 0;
char board[rows][elements];
void Clear()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < elements; j++)
{
board[i][j] = ' ';
}
}
}
void Show()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < elements; j++)
{
std::cout << " " << board[i][j] << " |";
}
std::cout << std::endl;
std::cout << "------------" << std::endl;
}
}
void StartTurn()
{
std::cout << "Which character would you like to be? (X or O) "; std::cin >> chooseTurn;
switch (chooseTurn){
case 'O':
std::cout << "You have choosen O" << std::endl << std::endl;
chooseTurn = 'O';
break;
case 'X':
std::cout << "You have choosen X" << std::endl << std::endl;
chooseTurn = 'X';
break;
default:
std::cout << "Enter a valid character" << std::endl;
StartTurn();
}
}
bool PlayerAttack(int x, int y, char PlayerAttackChar)
{
if (board[x][y] == ' ')
{
board[x][y] = PlayerAttackChar;
return true;
}
return false;
}
bool EnemyAttack(int x, int y, char PlayerAttackChar)
{
if (board[x][y] == ' ')
{
board[x][y] = PlayerAttackChar;
return true;
}
return false;
}
bool OWinner()
{
if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' ||
board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' ||
board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' ||
board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' ||
board[0][2] == 'O' && board[1][2] == 'O' && board[2][0] == 'O' ||
board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' ||
board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' ||
board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O')
{
std::cout << "You won!" << std::endl;
return true;
}
return false;
}
bool XWinner()
{
if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' ||
board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' ||
board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' ||
board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' ||
board[0][2] == 'X' && board[1][2] == 'X' && board[2][0] == 'X' ||
board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' ||
board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' ||
board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X')
{
std::cout << "You won!" << std::endl;
return true;
}
return false;
}
bool error(int x, int y)
{
if (board[x][y] != ' ')
{
return true;
}
}
int main()
{
Clear();
StartTurn();
Show();
if (chooseTurn == 'X')
{
SetPlayer = 'X';
SetEnemyPlayer = 'O';
}
else
{
SetPlayer = 'O';
SetEnemyPlayer = 'X';
}
int pos1 = 0;
int pos2 = 0;
bool Xwinner = false;
bool Owinner = false;
int PlayerTurn = 0;
while (!Xwinner && !Owinner)
{
while (PlayerTurn == 0)
{
bool yourAttack = false;
PlayerTurn++;
std::cout << SetPlayer << " turn. Please input a coordinate: "; std::cin >> pos1 >> pos2; std::cout << std::endl;
pos1 -= 1;
pos2 -= 1;
if (error(pos1, pos2))
{
std::cout << "Please enter a valid position (1 to 3)" << std::endl;
PlayerTurn = 0;
}
PlayerAttack(pos1, pos2, SetPlayer);
Show();
}
while (PlayerTurn != 0)
{
PlayerTurn = 0;
std::cout<< SetEnemyPlayer << " turn. Please input a coordinate: "; std::cin >> pos1 >> pos2; std::cout << std::endl;
pos1 -= 1;
pos2 -= 1;
if (error(pos1, pos2))
{
std::cout << "Please enter a valid position (1 to 3)" << std::endl;
PlayerTurn = 1;
}
EnemyAttack(pos1, pos2, SetEnemyPlayer);
Show();
// AI
}
Xwinner = XWinner();
Owinner = OWinner();
}
}
答案 0 :(得分:1)
如果移动有效,您的错误函数将不会返回任何内容,并导致未定义的行为。你想添加一个返回false;在if语句之后。您还要确保x和y位于数组的边界内。
bool error(int x, int y)
{
if(x > 2 || y > 2 || board[x][y] != ' ')
{
return true;
}
return false;
}
另外,如果(错误)检查在循环中,您可能想要。因此,在玩家进入有效行动之前,游戏不会继续。
while (error(pos1, pos2))
{
std::cout << "Please enter a valid position (1 to 3)" << std::endl;
PlayerTurn = 0;
}