问题描述
这个编码有问题,如何检查和显示赢家?我试图添加它,但变成错误由于函数checkWinner()。
have a problem with this coding, how to check and display the winner? I have tried to add it but turns into error due to the function checkWinner().
#include <iostream>
using namespace std;
void showBoard(void);
void playerInput(int p);
void checkWinner();
void nextPlayer(int);
int board[3][3]={{0,0,0},{0,0,0},{0,0,0}};
int main()
{
int r;
int c;
int player;
int winner;
int turns;
cout << "******* Tic Tac Toe Game *******" << endl;
showBoard();
nextPlayer(1);
checkWinner();
return 0;
}
输出板功能:
void showBoard(void)
{
int r;
int c;
for(r=0; r<=2; r++)
{
for(c=0; c<=2; c++)
{
if( board [r][c]==0)
cout << "0 ";
else if (board [r][c]==1)
cout << "1 ";
else
cout << "2 ";
}
cout << endl;
}
}
这是播放器输入功能:
void playerInput(int p)
{
int row;
int col;
if(p==1)
cout <<"You are player number 1 \n\n";
else
cout <<"You are player number 2 \n\n";
cout<<"Please enter your coordinate:";
cin>>row;
cout<<"\n";
cin>>col;
if(p==1)
board[--row][--col]=1;
if(p==2)
board[--row][--col]=2;
}
遇到我现在面临的问题, ?
Heres the problem I'm facing now, how to make it display the winner?
void checkWinner()
{
int winner;
for (int i=0; i<=2; i++)
{
if(board[i][0]==board[i][1] && board[i][1]==board[i][2] && board[i][0]!=0)
{
winner=board[i][0];
}
}
for(int i=0; i<=2; i++)
{
if (board[0][i]==board[1][i] && board[1][i]==board[2][i] && board[0][i]!=0)
{
winner=board[0][i];
}
}
if(board[0][0]=board[1][1] && board[1][1]==board[2][2] && board[0][0]!=0)
{
winner=board[0][0];
}
if(board[0][2]=board[1][1] && board[1][1]==board[2][0] && board [0][2]!=0)
{
winner=board[0][2];
}
if(board[0][0]==board[0][1] && board[0][1]==board[0][2]&& board[0][2]==board[0][1]&& board[1][0]==board [1][1]&& board[1][1]==board [1][2]&& board[1][2]==board[2][0]&&board[2][0]==board [2][1]&& board[2][1]==board [2][2] && board [0][0]!=0)
{
winner=0;
}
}
void nextPlayer(int player)
{
playerInput(player);
showBoard();
if(player==1)
player=2;
else
player=1;
nextPlayer(player);
}
问题是游戏没有结束,但它仍然要求玩家恢复并继续更新值。
点击下面看我面临的问题。谢谢!
The problem is the game is not ended but it still asking for the player to resume and it keep updating the value.click below to see the problem I'm facing. Thanks!
推荐答案
在代码中的两点,您已使用 <$ c在 if
语句中的$ c> = 这会导致初始化,而不是等同检查。您应该使用 ==
而不是 =
。
At two points in your code, you have used =
in your if
statement. This leads to initialization instead of equality checking. You should use ==
instead of the =
.
if (board[0][0] = board[1][1] && /** This should be a == for checking equality */
board[1][1] == board[2][2] &&
board[0][0] != 0)
{
winner=board[0][0];
}
if (board[0][2] = board[1][1] && /** This should be a == for checking equality */
board[1][1] == board[2][0] &&
board[0][2] != 0)
{
winner = board[0][2];
}
这篇关于Tic Tac Toe C ++检查&打印赢家的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!