2010-07-30 127 views
0

void checkboard函數不起作用。這是一個tic tac腳趾遊戲。因此,如果有人贏得比賽或者是平局,那麼每次移動後該功能都會被檢查。在有人贏了或者有平局之後,董事會將被重新設置,並且遊戲將會開始。我無法讓無效的checkboard函數工作。請幫我解決這個問題。使用IF語句檢查數組輸入的問題

檢查板應檢查每個用戶的輸入,看看該比賽是否贏了。所以checkboard必須檢查playerOne是否有一行或對角線的X,或者如果playerTwo有這種方式,或者有一條平手。如果玩家獲勝或者有抽籤,它會告訴誰贏了或者有一個平局,然後詢問用戶他們是否想再玩一次或退出該程序。從他們的迴應中,棋盤重置或遊戲退出。

我認爲問題在於無論如何,bGameOver一直保持真實。在gameover之前,我把代碼放在bGameOver = false之後,並且程序按照它的設想運行。所以一些if語句中的一些地方,在1次移動後,bGameOver = true。

解決問題後,我想提出一些建議,只有在問題得到解決後才能改進。

#include <iostream> 
#include <string> 




void checkboard(char** CurrentPlayer, char (&squares)[3][3], char &playerMarker, bool bGameOver,char playerOne[]); 


using namespace std; 

void main() 
{ 
char board[3][3]; 
    board[0][0] = ' '; 
    board[0][1] = ' '; 
    board[0][2] = ' '; 
    board[1][0] = ' '; 
    board[1][1] = ' '; 
    board[1][2] = ' '; 
    board[2][0] = ' '; 
    board[2][1] = ' '; 
    board[2][2] = ' '; 
bool bGameOver(true); 
char playerMarker; 


    // Main game loop 
    do 
    { 

     showboard(board); 

     boardMarker(playerTurn, playerOne, playerTwo, playerMarker, &currentPlayer); 

     playerMove(currentPlayer, board, playerMarker); 

     checkboard(&currentPlayer, board, playerMarker, bGameOver, playerOne); 

     alternatePlayers(playerTurn); 

    } 
    while (!bGameOver); 
    } 



    void showboard(char (&board)[3][3]) 
    { 
     for(int i = 0; i < 3; ++i) 
     { 
      for(int j = 0; j < 3; ++j) 
      { 
       cout << board[i][j]; 
       if(j < 2) cout << "|"; 
      } 
      cout << "\n"; 
      if(i < 2) cout << "-+-+-" << endl;; 
     } 
    } 


    void playerMove(char* CurrentPlayer, char (&board)[3][3], char &playerMarker) 
    { 
    bool bValidMove; 
    int x,y; 
    // Prompt the player for a move 
    cout << "Player " << CurrentPlayer << "'s move:" << endl; 



    // Loop until we get a valid move 
    do 
    { 
     cout << "Please enter the row number for the place you wish to mark: " << endl; 
     cin >> x; 
     cout << "Please enter the column number for the place you wish to mark" << endl; 
     cin >> y; 
     bValidMove = true; 

     // Check for a valid move 
     if (x == 1 && y == 1) 
     { 
     board[0][0] = playerMarker; 
     } 
     else if (x == 1 && y == 2) 
     { 
     board[0][1] = playerMarker; 
     } 
     else if (x == 1 && y == 3) 
     { 
     board[0][2] = playerMarker; 
     } 
     else if (x == 2 && y == 1) 
     { 
     board[1][0] = playerMarker; 
     } 
     else if (x == 2 && y == 2) 
     { 
     board[1][1] = playerMarker; 
     } 
     else if (x == 2 && y == 3) 
     { 
     board[1][2] = playerMarker; 
     } 
     else if (x == 3 && y == 1) 
     { 
     board[2][0] = playerMarker; 
     } 
     else if (x == 3 && y == 2) 
     { 
     board[2][1] = playerMarker; 
     } 
     else if (x == 3 && y == 3) 
     { 
     board[2][2] = playerMarker; 
     } 
     else 
     { 
     cout << "Invalid Move. Try again." << endl; 
     bValidMove = false; 
     } 

    } 
    while (!bValidMove); 

    } 

    void checkboard(char** CurrentPlayer, char (&board)[3][3], char &playerMarker, bool bGameOver, char playerOne[100]) 
{ 

     bGameOver  = false; 
     bool bWinGame = true; 
     // Check for end of game conditions 
    do 
    { 
     if (board[0][0] = playerMarker) 
     { 
      if (board[0][1] == board[0][0] && board[0][2] == board[0][0]) { 
       bGameOver = true; 
      } 
      if (board[1][0] == board[0][0] && board[2][0] == board[0][0]) { 
       bGameOver = true; 
      } 
     } 
     if (board[1][1] != playerMarker) 
     { 
      if (board[0][0] == board[1][1] && board[2][2] == board[1][1]) 
      { 
       bGameOver = true; 
      } 
      if (board[0][1] == board[1][1] && board[2][1] == board[1][1]) 
      { 
       bGameOver = true; 
      } 
      if (board[1][0] == board[1][1] && board[1][2] == board[1][1]) 
      { 
       bGameOver = true; 
      } 
      if (board[0][2] == board[1][1] && board[2][0] == board[1][1]) 
      { 
       bGameOver = true; 
      } 
     } 
     if (board[2][2] != playerMarker) 
     { 
      if (board[0][2] == board[2][2] && board[1][2] == board[2][2]) 
      { 
       bGameOver = true; 
      } 
      if (board[2][0] == board[2][2] && board[2][1] == board[2][2]) 
      { 
       bGameOver = true; 
      } 
     } 
     // Need to check the board full (no-win condition) 
     if (board[0][0] != playerMarker && board[0][1] != playerMarker && board[0][2] != playerMarker && 
      board[1][0] != playerMarker && board[1][1] != playerMarker && board[1][2] != playerMarker && 
      board[2][0] != playerMarker && board[2][1] != playerMarker && board[2][2] != playerMarker && !bGameOver) 
     { 
      bGameOver = true; 
      bWinGame = false; 
     } 

     system("cls"); 

     if (bGameOver) 
     { 
      if (bWinGame) 
      { 
       cout << "Player " << CurrentPlayer << " wins!" << endl; 
      } 
      // Print ending board 

     for(int i = 0; i < 3; ++i) 
     { 
      for(int j = 0; j < 3; ++j) 
      { 
      cout << board[i][j]; 
      if(j < 2) cout << "|"; 
      } 
      cout << "\n"; 
      if(i < 2) cout << "-+-+-" << endl;; 
     } 

      cout << "Game Over!" << endl; 
      cout << "Play again (y/n)?" << endl; 
      char cPlayAgain; 
      cin >> cPlayAgain; 

      if (cPlayAgain == 'y') 
      { 
       bGameOver = false; 
       // Clear the board 
       board[0][0] = ' '; 
       board[0][1] = ' '; 
       board[0][2] = ' '; 
       board[1][0] = ' '; 
       board[1][1] = ' '; 
       board[1][2] = ' '; 
       board[2][0] = ' '; 
       board[2][1] = ' '; 
       board[2][2] = ' '; 
      } 
     *CurrentPlayer = playerOne; 
     } 

} while (!bGameOver); 
} 
+1

由同一用戶關閉http://stackoverflow.com/questions/3369478/checking-an-array-for-results-using-a-function-關閉。 – 2010-07-30 16:59:11

+0

這個問題被關閉了,所以我想我必須用他們想要的信息開始一個新的問題 – Renge 2010-07-30 17:05:29

+0

我沒有做任何適當的分析(它有點意大利麪條),但是「if(board [0] [0] ='1'){「可能是錯誤的s/= /!=/ – 2010-07-30 18:45:33

回答

0

怎麼樣,如果你使用的簽名:

void checkboard(char** CurrentPlayer, char** squares, char &playerMarker, bool bGameOver,char playerOne[]); 

雖然我認爲這是不應該改變的唯一的事情,我會建議你讀多一點關於數組和指針,用較小的程序練習它。

我不知道你是如何編碼所有這些,而不會注意到它沒有按預期工作。

我也不知道是否有人會花時間爲你修復你的程序。這是作業。

+0

錯誤c2664:'checkboard':無法將參數2從'char [3] [3]'轉換爲'char **' 這就是構建錯誤我從做到這一點 – Renge 2010-07-30 16:54:28

+0

現在我很確定你在做功課。 = D – karlphillip 2010-07-30 16:56:26

+0

這是作業,但我卡住了,是我求助 – Renge 2010-07-30 19:32:02

0
if (board[0][0] = playerMarker) 

這將始終將playerMarker分配到板[0] [0]。你必須習慣「=」(賦值)和「==」(問題「這些是否相等?」)之間的區別。 Yoda Conditions雖然這不會幫助你在這裏。