无法使函数正常工作

时间:2013-07-08 01:31:57

标签: c++ function tic-tac-toe

我正试图让这个tic tac toe程序工作,但我完全失去了。我在网上搜索过,但它只是让我更加困惑。我无法让改变播放器功能起作用(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;
}    

1 个答案:

答案 0 :(得分:2)

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

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