我正在尝试创建一个简单的井字游戏程序作为一些家庭作业的一部分,但难以检查游戏是否已获胜。一旦我启动程序并输入一个数字,它就会立即显示“玩家 2 获胜!” 我看不出我做错了什么。
#include "stdafx.h"
#include "tictactoe.h"
using namespace std;
void clearBoard(char gameBoard[])
{
for (int i = 0; i < board_size; i++)
{
gameBoard[i] = free_char;
}
}
void printBoard(char gameBoard[])
{
system("cls");
//for (int i = 0; i < board_width; i++)
//{
//cout << gameBoard[i] + 1 << " " << gameBoard[i] + 1 << " " << gameBoard[i] + 1 << endl;
//}
cout << gameBoard[0] << " " << gameBoard[1] << " " << gameBoard[2] << endl;
cout << gameBoard[3] << " " << gameBoard[4] << " " << gameBoard[5] << endl;
cout << gameBoard[6] << " " << gameBoard[7] << " " << gameBoard[8] << endl;
}
int askMove(int player)
{
cout << "Player " << player << ", Select a square [1-9]: ";
cin >> choice;
return 0;
}
bool verifyMakeMove(char gameBoard[], int move, int player)
{
if (player == 1)
{
mark = p1_char;
}
else
{
mark = p2_char;
}
if (move < 1 || move > 9)
{
cout << "You did not enter a number between 1 and 9. Try again." << endl;
}
if (move == 1 && gameBoard[0] == free_char)
{
gameBoard[0] = mark;
}
else if (move == 2 && gameBoard[1] == free_char)
{
gameBoard[1] = mark;
}
else if (move == 3 && gameBoard[2] == free_char)
{
gameBoard[2] = mark;
}
else if (move == 4 && gameBoard[3] == free_char)
{
gameBoard[3] = mark;
}
else if (move == 5 && gameBoard[4] == free_char)
{
gameBoard[4] = mark;
}
else if (move == 6 && gameBoard[5] == free_char)
{
gameBoard[5] = mark;
}
else if (move == 7 && gameBoard[6] == free_char)
{
gameBoard[6] = mark;
}
else if (move == 8 && gameBoard[7] == free_char)
{
gameBoard[7] = mark;
}
else if (move == 9 && gameBoard[8] == free_char)
{
gameBoard[8] = mark;
}
else
{
cout << "That move cannot be made. Try again." << endl;
}
return 0;
}
int checkEndGame(char gameBoard[])
{
if (gameBoard[0] == gameBoard[1] && gameBoard[1] == gameBoard[2])
{
return 1;
}
else if (gameBoard[3] == gameBoard[4] && gameBoard[4] == gameBoard[5])
{
return 1;
}
else if (gameBoard[6] == gameBoard[7] && gameBoard[7] == gameBoard[8])
{
return 1;
}
else if (gameBoard[0] == gameBoard[3] && gameBoard[3] == gameBoard[6])
{
return 1;
}
else if (gameBoard[1] == gameBoard[4] && gameBoard[4] == gameBoard[7])
{
return 1;
}
else if (gameBoard[2] == gameBoard[5] && gameBoard[5] == gameBoard[8])
{
return 1;
}
else if (gameBoard[0] == gameBoard[4] && gameBoard[4] == gameBoard[8])
{
return 1;
}
else if (gameBoard[2] == gameBoard[4] && gameBoard[4] == gameBoard[6])
{
return 1;
}
else if (gameBoard[0] != free_char && gameBoard[1] != free_char && gameBoard[2] != free_char && gameBoard[3] != free_char && gameBoard[4] != free_char && gameBoard[5] != free_char && gameBoard[6] != free_char && gameBoard[7] != free_char && gameBoard[8] != free_char)
{
return -1;
}
else
{
return 0;
}
}
int playGame()
{
char gameBoard[10];
int player = 1;
int winner;
clearBoard(gameBoard);
do
{
printBoard(gameBoard);
askMove(player);
verifyMakeMove(gameBoard, choice, player);
checkEndGame(gameBoard);
if (player == 1)
{
player = 2;
}
else
{
player = 1;
}
} while (checkEndGame(gameBoard) == 0);
if (checkEndGame(gameBoard) == 1)
{
cout << "Player " << player << " wins!" << endl;
}
else
{
cout << "It's a draw!" << endl;
}
return 0;
}
int main()
{
playGame();
return 0;
}