Tic Tac Toe C ++ Check&打印优胜者

时间:2016-01-31 05:11:46

标签: c++

这个编码有问题,如何检查并显示获胜者?我试图添加它但由于函数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;
}

这是我现在面临的问题,如何让它成为胜利者?

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);
}

问题是游戏没有结束,但它仍然要求玩家恢复并继续更新价值。 点击下方查看我面临的问题。谢谢!

Click to view my output, it doesn't end the game

2 个答案:

答案 0 :(得分:0)

在代码中的两个位置,您在=语句中使用了 if 。这会导致初始化,而不是等式检查。您应该使用 == 而不是 =

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];
}

答案 1 :(得分:0)

您的代码存在严重的递归问题。 checkWinner()中的函数main永远不会被调用,因为函数nextPlayer(1);是无限递归的,因为它缺少基本情况

递归基本情况,是导致递归函数停止递归的原因。

请注意,在您的代码中:

void nextPlayer(int player)
{
 playerInput(player);
 showBoard();

 if(player==1)
  player=2;
 else
  player=1;
 nextPlayer(player);
}

nextPlayer(1);调用main后,它永远不会返回,因为它总是在函数末尾进入另一个自己的nextPlayer(player);调用。为此,你需要一个基本情况来阻止这种递归发生。

现在,逻辑上你的基本情况应该是,在继续游戏之前检查胜利者,这是由checkWinner()函数处理的。

但是,问题是checkWinner()函数返回void,这意味着没有所以,在当前状态下,我们不能将它用作基本情况。

但是,如果我们重新设计它以返回如下内容:

  • 0 for play on
  • 1或2表示玩家赢了
  • 最后-1表示董事会已满,没有人赢(领带)。

所以,将checkWinner()函数改为(我还纠正了一些条件,所以仔细查看代码):

int  checkWinner()
{
 int winner;

 // any of the rows is same
 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];
    return winner;
  }
 }

 // any of the columns is same
 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];
    return winner;
  }
 }

 // 1st diagonal is same
 if(board[0][0]==board[1][1] && board[1][1]==board[2][2] && board[0][0]!=0)
 {
  winner = board[0][0];
  return winner;
 }

 // 2nd diagonal is same
 if(board[0][2]==board[1][1] && board[1][1]==board[2][0] && board [0][2]!=0)
 {
  winner = board[0][2];
  return winner;
 }

 // if we reached here nobody has won yet

 // if any empty box on board then play on
 for(int i=0; i<=2; i++)
 {
  for(int j=0; j<=2; j++)
  {
   if(board[i][j]==0)
   {
    winner = 0;
    return winner;
   }
  }
 }

 winner = -1; // all boxes full and nobody won so A tie has occurred
 return winner;
}

现在,可以使用它,我们将在void nextPlayer(int player)函数中使用它:

void nextPlayer(int player)
{ 
 int winner = checkWinner();

 if( winner == 0) // play on
 {
  playerInput(player);
  showBoard();

  if(player==1)
   player=2;
  else
   player=1;
  nextPlayer(player);
 }
 else if(winner == -1)
 {
  cout<<"\nGame drawn!\n";
 }
 else
 {
  cout<<"\nPlayer "<<winner<<" wins!\n"<<endl;
 }
}

请注意,在我们开始执行任何操作之前,会检查checkWinner()条件。

现在,checkWinner()函数中main的调用无论如何都无法访问,现在将在void nextPlayer(int player)的每个步骤继续执行之前完成:

#include <iostream>
using namespace std;

void showBoard(void);
void playerInput(int p);
int 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);

 return 0;
}

其余功能正确且保持不变。