2013-07-08 50 views
-5

我正试图让这个tic tac脚趾程序能够正常工作,但我处于完全丧失状态。我在网上搜索过,但只会让我更困惑。我无法使更改播放器功能正常工作(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

更改播放器功能从1更改为2,但不会将2更改回1以使播放器1再次打开。检查联系功能应检查板阵列(减去0行和0列,因为它们是数字)'*',因为这是空白指示器。如果搜索没有发现'*',并且没有任何玩家被宣布为胜利者而不是领带并且会显示“这是一条领带”结束该程序。 – user2526556

回答

2

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

至于checkTie,您需要提供详细信息,以何种方式无效。或者你可以通过记录所做出的动作次数来避免需要;如果在第九步之后没有人获胜,那么这场比赛是平局。

+0

非常感谢你!我不能相信我忽视了我一直在研究这么长时间,并尝试了很多我现在完全盲目的代码。谢谢!!!!! – user2526556

+0

关于领带功能的任何想法?我非常感谢你的帮助。 – user2526556

+0

@ user2526556,打开编译器警告时,此答案将不必要。 – chris