-6

我正试图让这个井字游戏程序正常工作,但我完全不知所措。我已经搜索了整个网络,但它只会让我更加困惑。我无法使更改播放器功能正常工作(playerTurn),也无法使检查平局的功能正常工作(checkTie)。请,任何帮助将不胜感激。这是我所拥有的:

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <math.h>
#include <cstdlib>
#include <string>

using namespace std;



char checkWinner(char[][4]);
bool checkTie(char[][4]);
int playerTurn(int);

const char PLAYER1='X';
const char PLAYER2='O';


int main()
{
    //variable for player
    int playerNum=1;

    //Game over variable
    bool bGameOver=false;

    //Variable to hold winner status
    char winner;
    bool tie;

    //Variable player
    string player;

    //Player mark
    char mark;

    //Constants for board size
    const int ROWS=4;
    const int COLS=4;

    char board[ROWS][COLS] = {{' ', '1', '2', '3'},
                             { '1', '*', '*', '*'},
                             {'2', '*', '*', '*'},
                             {'3', '*', '*', '*'}};


    //counter variable
    int i;
    //display the board               
    for (int i = 0; i <ROWS; i++)
          {
              cout << board[i][0] << "\t" << board[i][1] << "\t" <<
              board[i][2] << "\t" << board[i][3]<< endl;
          }//end for                  


    //Variables to hold selection
     int rowx, colx;

while(!bGameOver)//Main game loop
{

     if(playerNum=1)
     {
        player="Player 1";
        mark=PLAYER1;
     }//end if
     else if(playerNum==2)
     {  
        player="Player 2";  
        mark=PLAYER2;
     }//end else

     //Get player selection
     cout << player << " - enter row:"; cin >> rowx;
     cout << player << " - enter column:"; cin >> colx;
     cout << "" << endl;

     if (board[rowx][colx] != '*')
     {
    cout << "This space is taken...try again" << endl;
    cout << "row:";  cin >> rowx;
    cout << "column:"; cin >> colx; 
    }//end if

     board[rowx][colx]=mark;



    for (int i = 0; i <ROWS; i++)
          {
              cout << board[i][0] << "\t" << board[i][1] << "\t" <<
              board[i][2] << "\t" << board[i][3]<< endl;

          }//end for 

          //call checkwinner function
          winner=checkWinner(board);

          playerNum=playerTurn(playerNum);


          cout << "the player number is: " << playerNum << endl;


          if(winner=='X')
          {
            cout << "Player 1 wins!" << endl;
            bGameOver=true;
          }//end if


          if(winner=='O')
          {
             cout << "Player 2 wins!" << endl;
             bGameOver=true;
          }//end if  

          //call checkTie function
          tie=checkTie(board);

          if(tie)
          {
            cout << "It's a tie!" << endl;
            bGameOver=true;
          }//end if 


}     

         if (bGameOver)
         {
            cout << "Game over!" << endl;
         }//end if




    system ("PAUSE");
    return 0;

} //end main



//Declare function checkWinner
char checkWinner(char arr[][4])
{
     //variable to hold result
     char result='B';

     //variable to hold counter
     int i=1;

    //Check player 1 status
    for(i=1; i<4; i++)  /* check rows */
    {
        if(arr[i][1]=='X' && arr[i][2]== 'X' && arr[i][3]=='X')
        {
            result='X';
        }
    }

    for(i=1; i<4; i++)  /* check columns */
    {
        if(arr[1][i]=='X' && arr[2][i]== 'X' && arr[3][i]=='X')
        {
            result='X';
        }
    }
       /* test diagonals */
    if(arr[1][1]=='X' && arr[2][2]== 'X' && arr[3][3]=='X')
    {
        result='X';
    }

    if(arr[1][3]=='X' && arr[2][2]=='X' && arr[3][1]=='X')
    {
       result='X';
    }   

    //Check player 2 status
    for(i=1; i<4; i++)  /* check rows */
    {
        if(arr[i][1]=='O' && arr[i][2]== 'O' && arr[i][3]=='O')
        {
            result='O';
        }
    }

    for(i=1; i<4; i++)  /* check columns */
    {
        if(arr[1][i]=='O' && arr[2][i]=='O' && arr[3][i]=='O')
        {
            result='O';
        }
    }
       /* test diagonals */
    if(arr[1][1]=='O' && arr[2][2]== 'O' && arr[3][3]=='O')
    {
        result='O';
    }

    if(arr[1][3]=='O' && arr[2][2]=='O' && arr[3][1]=='O')
    {
       result='O';
    }   



       return result;

}//end function    

//Define checkTie function
bool checkTie(char arr[][4])
{
     //constant for size
     const int SIZE=4;


     //variable to hold result
     bool result=false;


     //variable loop counter
     int i=0;


    if (arr[1][1]!='*'&&arr[1][2]!='*'&&arr[1][3]!='*'&&
        arr[2][1]!='*'&&arr[2][2]!='*'&&arr[2][3]!='*'&&
        arr[3][1]!='*'&&arr[3][2]!='*'&&arr[3][3]!='*')
        {
           result=true;
        }//end if  

    return result;
}//end function    

int playerTurn(int num)
{
    //variable for result
    int result;

    if(num==1)
    {
       result=2;
    }   
    else
    {
       result=1;
    }   


    return result;
}    
4

1 回答 1

2

playerTurn() 工作正常;但是,在主游戏循环的顶部,您在 if 语句的测试中使用 = 而不是 ==,因此每次迭代都将 playerNum 重置为 1。

至于 checkTie,您需要详细说明它以何种方式不起作用。或者您可以通过计算所进行的移动次数来避免需要它;如果在第 9 步之后没有人获胜,则游戏为平局。

于 2013-07-08T01:45:29.147 回答