我正在为一个tic tac toe游戏编写一个c ++程序。 游戏必须有一个胜利数量的游戏计数器 每个球员都有多少关系。问题 我的意思是我似乎无法让董事会明白, 所以当它到达一场比赛结束时,董事会会清除 X&O和X。我能得到任何帮助,我将非常感激!
#include <iostream>
using namespace std;
void display_board();
void player_turn();
bool gameover();
int xwins, owins, ties;
char turn;
bool draw = false;
char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
int main()
{
cout << "Tic Tac Toe Game\n";
cout << "Player 1 [X] --- Player 2 [O]\n";
turn = 'X';
char board[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
while (!gameover())
{
display_board();
player_turn();
gameover();
}
if (turn == 'O' && !draw)
{
display_board();
cout << endl << endl << "Player 1 [X] Wins! Game Over!\n";
xwins++;
}
else if (turn == 'X' && !draw)
{
display_board();
cout << endl << endl << "Player 2 [O] Wins! Game Over!\n";
owins++;
}
else
{
display_board();
cout << endl << endl << "It's a draw! Game Over!\n";
ties++;
}
}
void display_board()
{
cout << "---------------------" << endl << endl;
cout << " | | " << endl;
cout << " " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] <<" Player 1 wins "<<xwins<< endl;
cout << "_____|_____|_____" <<endl;
cout << " | | " << endl;
cout << " " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] <<" Player 2 wins "<<owins<< endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] <<" Ties "<<ties<< endl;
cout << " | | " << endl;
}
void player_turn()
{
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 << "You didn't enter a correct number! Try again\n";
player_turn();
}
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 << "The cell you chose is used! Try again\n";
player_turn();
}
}
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 :(得分:0)
我可以看到您的代码存在一些问题:
while (!gameover())
{
display_board();
player_turn();
gameover();
}
你不需要循环内的gameover()。你已经在条件下检查了它。
此外,您不会在每个游戏实例后重置全局变量。添加另一个while / do-while循环,该循环一直运行到特定条件。在此循环中,添加您的主要功能。并为每个循环实例重置全局变量(绘图,板和转)。
答案 1 :(得分:0)
试试这个
do{
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 << "You didn't enter a correct number! Try again\n";
}
}
while(choice<1 || choice>9);
因为如果用户输入其再次调用player_turn();
的任何其他值,那么它将工作recursively
(在函数内调用相同的函数)。
在检查gameover();
时,无需再次在循环内调用while(!gameover())
。
在绘制匹配后,您需要设置值,如果数组为1,2,3 ...并调用函数display_board();
。
答案 2 :(得分:0)
如果我已正确理解您的问题,那么有两个问题:
1)你需要找到一个胜利和领带计数器的地方。
bool gameover()
{
for (int i = 0; i < 3; i++)//Check for a win
{
bool row_match = (board[i][0] == board[i][1] && board[i][1] == board[i][2]);
bool column_match = (board[0][i] == board[1][i] && board[1][i] == board[2][i]);
bool diagonal_match = (board[0][2] == board[1][1] && board[1][1] == board[2][0]) || (board[0][0] == board[1][1] && board[1][1] == board[2][2]);
if (row_match || column_match || diagonal_match)
{
bool x_wins = (row_match && board[i][0] == "X") || (column_match && board[0][i] == "X") || (diagonal_match && board[1][1] == "X")
if (x_wins) xwins++;
else owins++;
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;
}
}
}
ties++;
draw = true;
return true;
}
2)你需要清理电路板。
do
{
display_board();
player_turn();
} while (!gameover);
clearboard();
void clearboard()
{
for (int row = 0; row < 3; row++)
{
for (int col = 0; col < 3; col++)
{
int val = 3 ** row + col + 1;
board[row][col] = itoa (val);
}
}
}
如果不能解决您的问题,或者我误解了您的问题,请告诉我。