我做了一个tic tac toe程序,我使用了一个函数来检查win或draw方案,代码工作正常,但我不允许在这个任务中使用函数。
现在这是我的代码,现在可以在添加函数gameover()
时正常工作:
#include <iostream>
using namespace std;
bool gameover();
char turn = 'X';
bool draw = false;
char board[3][3] = { { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' } };
int main()
{
while (!gameover())
{
cout << "\nPlayer 1 [X] --- Player 2 [O]\n\n\n";
cout << "\t" << board[0][0] << " " << board[0][1] << " " << board[0][2] << endl<< endl;
cout << "\t" << board[1][0] << " " << board[1][1] << " " << board[1][2] << endl<< endl;
cout << "\t" << board[2][0] << " " << board[2][1] << " " << board[2][2] << endl<< endl;
int choice;
int row = 0, column = 0;
if (turn == 'X')
cout << "Player 1 turn [X]: ";
else if (turn == 'O')
cout << "Player 2 turn [O]: ";
cin >> choice;
switch (choice)
{
case 1: row = 0; column = 0; break;
case 2: row = 0; column = 1; break;
case 3: row = 0; column = 2; break;
case 4: row = 1; column = 0; break;
case 5: row = 1; column = 1; break;
case 6: row = 1; column = 2; break;
case 7: row = 2; column = 0; break;
case 8: row = 2; column = 1; break;
case 9: row = 2; column = 2; break;
default:
cout << "\nYou didn't enter a correct number! Try again\n";
continue;
}
if (turn == 'X' && board[row][column] != 'X' && board[row][column] != 'O')
{
board[row][column] = 'X';
turn = 'O';
}
else if (turn == 'O' && board[row][column] != 'X' && board[row][column] != 'O')
{
board[row][column] = 'O';
turn = 'X';
}
else
{
cout << "\nThe cell you chose is used! Try again\n";
continue;
}
gameover();
}
if (turn == 'O' && !draw)
cout << endl << endl << "Player 1 [X] Wins! Game Over!\n";
else if (turn == 'X' && !draw)
cout << endl << endl << "Player 2 [O] Wins! Game Over!\n";
else
cout << endl << endl << "It's a draw! Game Over!";
}
bool gameover()
{
for (int i = 0; i < 3; i++)//Check for a win
if ((board[i][0] == board[i][1] && board[i][1] == board[i][2]) || (board[0][i] == board[1][i] && board[1][i] == board[2][i]) || (board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
return true;
for (int i = 0; i < 3; i++)//Check for draw
for (int j = 0; j < 3; j++)
if (board[i][j] != 'X' && board[i][j] != 'O')
return false;
draw = true;
return true;
}
但当我删除gameover()
功能并将其添加到int main()
时,它不会检查是赢还是抽奖,它会继续显示显示板,程序不会显示;结束。
如何在int main中添加gameover功能并使其检查win或draw?
编辑:这是我到目前为止实施的代码,即使有3个直接&#39; X&#39;,它仍然要求输出:#include <iostream>
using namespace std;
bool gameover();
char turn = 'X';
bool draw = false;
char board[3][3] = { { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' } };
int main()
{
while (draw != true)
{
cout << "\nPlayer 1 [X] --- Player 2 [O]\n\n\n";
cout << "\t" << board[0][0] << " " << board[0][1] << " " << board[0][2] << endl<< endl;
cout << "\t" << board[1][0] << " " << board[1][1] << " " << board[1][2] << endl<< endl;
cout << "\t" << board[2][0] << " " << board[2][1] << " " << board[2][2] << endl<< endl;
int choice;
int row = 0, column = 0;
if (turn == 'X')
cout << "Player 1 turn [X]: ";
else if (turn == 'O')
cout << "Player 2 turn [O]: ";
cin >> choice;
switch (choice)
{
case 1: row = 0; column = 0; break;
case 2: row = 0; column = 1; break;
case 3: row = 0; column = 2; break;
case 4: row = 1; column = 0; break;
case 5: row = 1; column = 1; break;
case 6: row = 1; column = 2; break;
case 7: row = 2; column = 0; break;
case 8: row = 2; column = 1; break;
case 9: row = 2; column = 2; break;
default:
cout << "\nYou didn't enter a correct number! Try again\n";
continue;
}
if (turn == 'X' && board[row][column] != 'X' && board[row][column] != 'O')
{
board[row][column] = 'X';
turn = 'O';
}
else if (turn == 'O' && board[row][column] != 'X' && board[row][column] != 'O')
{
board[row][column] = 'O';
turn = 'X';
}
else
{
cout << "\nThe cell you chose is used! Try again\n";
continue;
}
for (int i = 0; i < 3; i++)//Check for a win
if ((board[i][0] == board[i][1] && board[i][1] == board[i][2]) || (board[0][i] == board[1][i] && board[1][i] == board[2][i]) || (board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
draw = true;
for (int i = 0; i < 3; i++)//Check for draw
for (int j = 0; j < 3; j++)
if (board[i][j] != 'X' && board[i][j] != 'O')
draw = false;
}
cout << "The Final Result\n\n\n";
cout << "\t" << board[0][0] << " " << board[0][1] << " " << board[0][2] << endl<< endl;
cout << "\t" << board[1][0] << " " << board[1][1] << " " << board[1][2] << endl<< endl;
cout << "\t" << board[2][0] << " " << board[2][1] << " " << board[2][2] << endl<< endl;
if (turn == 'O' && !draw)
cout << endl << endl << "Player 1 [X] Wins! Game Over!\n";
else if (turn == 'X' && !draw)
cout << endl << endl << "Player 2 [O] Wins! Game Over!\n";
else
cout << endl << endl << "It's a draw! Game Over!";
}
bool gameover()
{
for (int i = 0; i < 3; i++)//Check for a win
if ((board[i][0] == board[i][1] && board[i][1] == board[i][2]) || (board[0][i] == board[1][i] && board[1][i] == board[2][i]) || (board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
return true;
for (int i = 0; i < 3; i++)//Check for draw
for (int j = 0; j < 3; j++)
if (board[i][j] != 'X' && board[i][j] != 'O')
return false;
draw = true;
return true;
}
答案 0 :(得分:2)
#include <iostream>
using namespace std;
char turn = 'X';
bool draw = false;
bool gameover = false;
char board[3][3] = { { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' } };
int main()
{
while (!gameover)
{
cout << "\nPlayer 1 [X] --- Player 2 [O]\n\n\n";
cout << "\t" << board[0][0] << " " << board[0][1] << " " << board[0][2] << endl<< endl;
cout << "\t" << board[1][0] << " " << board[1][1] << " " << board[1][2] << endl<< endl;
cout << "\t" << board[2][0] << " " << board[2][1] << " " << board[2][2] << endl<< endl;
int choice;
int row = 0, column = 0;
if (turn == 'X')
cout << "Player 1 turn [X]: ";
else if (turn == 'O')
cout << "Player 2 turn [O]: ";
cin >> choice;
switch (choice)
{
case 1: row = 0; column = 0; break;
case 2: row = 0; column = 1; break;
case 3: row = 0; column = 2; break;
case 4: row = 1; column = 0; break;
case 5: row = 1; column = 1; break;
case 6: row = 1; column = 2; break;
case 7: row = 2; column = 0; break;
case 8: row = 2; column = 1; break;
case 9: row = 2; column = 2; break;
default:
cout << "\nYou didn't enter a correct number! Try again\n";
continue;
}
if (turn == 'X' && board[row][column] != 'X' && board[row][column] != 'O')
{
board[row][column] = 'X';
turn = 'O';
}
else if (turn == 'O' && board[row][column] != 'X' && board[row][column] != 'O')
{
board[row][column] = 'O';
turn = 'X';
}
else
{
cout << "\nThe cell you chose is used! Try again\n";
continue;
}
draw = true; //assume a draw
for (int i = 0; i < 3; i++)//Check for a win
if ((board[i][0] == board[i][1] && board[i][1] == board[i][2]) || (board[0][i] == board[1][i] && board[1][i] == board[2][i]) || (board[0][0] == board[1][1] && board[1][1] == board[2][2]) || (board[0][2] == board[1][1] && board[1][1] == board[2][0]))
{
gameover = true;
draw = false;
break;
}
if (!gameover)
for (int i = 0; i < 3; i++)//Check for draw
for (int j = 0; j < 3; j++)
if (board[i][j] != 'X' && board[i][j] != 'O')
{
draw = false;
break;
}
if (draw)
gameover = true;
}
if (turn == 'O' && !draw)
cout << endl << endl << "Player 1 [X] Wins! Game Over!\n";
else if (turn == 'X' && !draw)
cout << endl << endl << "Player 2 [O] Wins! Game Over!\n";
else
cout << endl << endl << "It's a draw! Game Over!";
}
不太漂亮,但创建结构和可读性是函数的用途。
答案 1 :(得分:2)
修改后的代码有两个问题,一个算法和一个域。
算法错误是return
语句终止函数的执行。这意味着不会对该功能体进行进一步处理。
在修改过的代码中,没有发生这样的控制流中断 - 两个for
循环都是完整的。这意味着,在电路板满了之前,第二个for
循环会将draw
重置为false
,即使前一个循环将其设置为true
。
第二个域名问题是您错误地重新定位了draw
变量。以前,这意味着“游戏以平局结束吗?”现在,你正在使用它作为“游戏结束了吗?”你需要两个独立的变量。
完成这些更改后,代码看起来非常像@user1320881's answer。