所以我一直在建立一个Tic-Tac-Toe计划,目前它仍在进行中。到目前为止我没有遇到任何构建错误,但我现在遇到的主要问题是当玩家1输入他们的第一个号码时程序才会关闭。我认为这是因为Input()函数,但是在查看了大约3次代码后,我发现了一个空白,并且不知道是什么导致了它。这是相关的代码:
#include <iostream>
using namespace std;
char acBoard[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
char cPlayerCharacter = '0';
int iChoice = iChoice + 1;
void DisplayBoard()
{
system("cls");
cout << ' ' << acBoard[0][0] << " | " << acBoard[0][1] << " | " << acBoard[0][2] << endl;
cout << "---|---|---" << endl;
cout << ' ' << acBoard[1][0] << " | " << acBoard[1][1] << " | " << acBoard[1][2] << endl;
cout << "---|---|---" << endl;;
cout << ' ' << acBoard[2][0] << " | " << acBoard[2][1] << " | " << acBoard[2][2] << endl;
}
char win()
{
if (acBoard[0][0] && acBoard[0][1] && acBoard[0][2] == 'X' ||
acBoard[1][0] && acBoard[1][1] && acBoard[1][2] == 'X' ||
acBoard[2][0] && acBoard[2][1] && acBoard[2][2] == 'X' ||
acBoard[0][0] && acBoard[1][0] && acBoard[2][0] == 'X' ||
acBoard[0][1] && acBoard[1][1] && acBoard[2][1] == 'X' ||
acBoard[0][2] && acBoard[1][2] && acBoard[2][2] == 'X' ||
acBoard[0][0] && acBoard[1][1] && acBoard[2][2] == 'X' ||
acBoard[2][0] && acBoard[1][1] && acBoard[2][0] == 'X')
return 'X';
if (acBoard[0][0] && acBoard[0][1] && acBoard[0][2] == '0' ||
acBoard[1][0] && acBoard[1][1] && acBoard[1][2] == '0' ||
acBoard[2][0] && acBoard[2][1] && acBoard[2][2] == '0' ||
acBoard[0][0] && acBoard[1][0] && acBoard[2][0] == '0' ||
acBoard[0][1] && acBoard[1][1] && acBoard[2][1] == '0' ||
acBoard[0][2] && acBoard[1][2] && acBoard[2][2] == '0' ||
acBoard[0][0] && acBoard[1][1] && acBoard[2][2] == '0' ||
acBoard[2][0] && acBoard[1][1] && acBoard[2][0] == '0')
return '0';
return 'd';
}
void Input()
{
cin >> iChoice;
switch (iChoice)
{
case 1:
acBoard[0][0] = cPlayerCharacter;
break;
case 2:
acBoard[0][1] = cPlayerCharacter;
break;
case 3:
acBoard[0][2] = cPlayerCharacter;
break;
case 4:
acBoard[1][0] = cPlayerCharacter;
break;
case 5:
acBoard[1][1] = cPlayerCharacter;
break;
case 6:
acBoard[1][2] = cPlayerCharacter;
break;
case 7:
acBoard[2][0] = cPlayerCharacter;
break;
case 8:
acBoard[2][1] = cPlayerCharacter;
break;
case 9:
acBoard[2][2] = cPlayerCharacter;
break;
}
}
int main()
{
do
{
DisplayBoard();
cout << "Enter a Number Player " << cPlayerCharacter << ": ";
Input();
if (cPlayerCharacter = '0')
{
cPlayerCharacter = 'X';
}
else cPlayerCharacter = '0';
} while (!win);
if (win() == 'd')
{
cout << "It's a draw";
}
if (win() == '0')
{
cout << "Nought wins";
}
if (win() == 'X')
{
cout << "Cross wins";
}
}
我觉得解决方案就在我面前,但我却看不到它。任何帮助,将不胜感激。感谢。
答案 0 :(得分:1)
while (!win);
应while (!win());
实际调用该函数。否则你基本上只是在评估函数是否存在。