无法使函数正常工作

Can not get functions to work correctly

本文关键字:工作 常工作 函数      更新时间:2023-10-16

我正试图让这个井字游戏程序发挥作用,但我完全不知所措。我在网上找遍了,但这只会让我更加困惑。我无法让change player函数工作(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;
}    

playerTurn()工作正常;但是,在Main Game循环的顶部,在if语句的测试中使用=而不是==,从而在每次迭代中将playerNum重置为1。

至于checkTie,您需要详细说明它在哪些方面不起作用。或者,你可以通过记录移动次数来避免需要它;如果在第9次移动后没有人获胜,那么这场比赛就是平局。