2013-05-02 192 views
1

我的代碼運行,但它不打印出板上的任何東西。我很確定我做了一些錯誤的事情,如果有人能指出我朝着正確的方向,那將是很棒的..感謝您的幫助!井字棋遊戲2D陣列

#include<iostream> 
#include<iomanip> 

using namespace std; 

void drawBoard(char board[][3]); 
char checkWinner3by3(char board[][3]); 

. 
// 
// DO NOT MODIFY THE MAIN FUNCTION 
// 
int main() 
{ 
    // This array of chars represents the game board, and it holds the content 
    // of each space. By default all spaces are set to a blank space. 
    char board[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}}; 

    // The current player. Because X plays first, initialize to X 
    char player = 'X'; 

    // The winner. either 'X', 'O', or 't' if it's a tie. 
    // Or a blank space if the game has not finished. 
    char winner = ' '; 

    // These variables will hold the number of the row and column selected 
    // by the players. 
    int row; 
    int column; 

    // boolean variables used to verify if the move is valid. 
    bool is_move; 
    bool is_row; 
    bool is_column; 

    cout<<"************ TIC TAC TOE ************\n"; 

    // The game loops again and again until the game is over 
    while(winner == ' ') 
    { 
     //set the boolean variables back to false for a new turn. 
     is_move = false; 
     is_row = false; 
     is_column = false; 

     //draw the board. 
     drawBoard(board); 

     // If the game is not yet over show who`s the next player to move 
     cout << "Player "; 
     if(player == 'X') 
     { 
      cout << 1; 
     } 
     else 
     { 
      cout << 2; 
     } 
     cout << "'s turn:" << endl; 

     // Loop until the player selects a valid space for their move 
     is_move = false; 
     while(!is_move) 
     { 
      // Loop until the player selects a valid row 
      // Assume the user inputs a valid integer 
      is_row = false; 
      while(!is_row) 
      { 
       cout << "Row: "; 
       cin >> row; 
       if(row == 1 || row == 2 || row == 3) 
       { 
        is_row = true; 
       } 
       else 
       { 
        cout << endl << "Invalid row!" << endl; 
       } 
      } // end of input row loop 

      // Loop until the player selects a valid column 
      // Assume the user inputs a valid integer 
      is_column = false; 
      while(!is_column) 
      { 
       cout << "Column: "; 
       cin >> column; 
       if(column == 1 || column == 2 || column == 3) 
       { 
        is_column = true; 
       } 
       else 
       { 
        cout << endl << "Invalid column!" << endl; 
       } 
      } // end of input column loop 

      // If the space is empty, mark the chosen space and swich players 
      if(board[row-1][column-1] == ' ') 
      { 
       // Mark the space and end the player's turn 
       board[row-1][column-1] = player; 
       is_move = true; 

       // Switch to the other player 
       if(player == 'X') 
       { 
        player = 'O'; 
       } 
       else 
       { 
        player = 'X'; 
       } 
      } // end of marking space and changing players 

      // If the space is occupied 
      else 
      { 
       cout<<"The selected space is occupied!" << endl; 
       cout << "Select a different space:" << endl << endl; 
       drawBoard(board); 
      } 
     } // end of player's move loop 

     cout << endl; // for nice output formatting 

     //check if the player won. 
     winner = checkWinner3by3(board); 

     //If there's a winner 
     if(winner == 'X' || winner == 'O') 
     { 
      drawBoard(board); 

      //Display congratulations message 
      cout<<"Congratulations! Player "; 
      if(winner == 'X') 
      { 
       cout << 1; 
      } 
      else 
      { 
       cout << 2; 
      } 
      cout<<" is the winner!"<<endl; 
     } 
     else if(winner == 'T') 
     { 
      drawBoard(board); 

      //Display a message if it`s tie 
      cout << "It's a tie!" << endl; 
     } 

    } // End of player's turn loop 

    system("pause"); 
    return 0; 
} 


// 
// Prints the game board 
// We know the board is 3 by 3 so we don't need to have the number of rows as 
// a parameter. 
// 
// WRITE THIS FUNCTION 
// 
void drawBoard(char board[][3]) 
{ 
    char print[][3] = {{' ',' ',' '}, 
         {' ',' ',' '}, 
         {' ',' ',' '}}; 

    cout << "  1 2 3" << endl; 
    cout << " +---+---+---+" << endl; 

    cout << " 1" << " | " << print[0][0] << " | " << print[0][1] << " | " << print[0][2] << " | " << endl; 
    cout << " +---+---+---+" << endl; 

    cout << " 2" << " | " << print[1][0] << " | " << print[1][1] << " | " << print[1][2] << " | " << endl; 
    cout << " +---+---+---+" << endl; 

    cout << " 3" << " | " << print[2][0] << " | " << print[2][1] << " | " << print[2][2] << " | " << endl; 
    cout << " +---+---+---+" << endl; 

} 
// 
// Checks the whole board if there is a winner. 
// We know the board is 3 by 3 so we don't need to have the number of rows as 
// a parameter. 
// 
// WRITE THIS FUNCTION 
// 
char checkWinner3by3(char board[][3]) 
{ 


    for(i=0; i<3; i++) /* check rows */ 
    { 
     if(board[i][0]==board[i][1] && board[i][0]==board[i][2]) 
     { 
      return board[i][0]; 
     } 
    } 

    for(i=0; i<3; i++) /* check columns */ 
    { 
     if(board[0][i]==board[1][i] && board[0][i]==board[2][i]) 
     { 
      return board[0][i]; 
     } 
    } 
     /* test diagonals */ 
    if(board[0][0]==board[1][1] && board[1][1]==board[2][2]) 
    { 
     return board[0][0]; 
    } 

    if(board[0][2]==board[1][1] && board[1][1]==board[2][0]) 
    { 
     return board[0][2]; 
    } 

    return ' '; 
} 
+0

這是功課嗎? – rivimey 2013-05-02 00:12:54

+0

是的,這就是爲什麼我只需要有人引導我走向正確的方向。指出我有錯誤,所以我可以嘗試修復它們。我是新來的C++。 – Noob 2013-05-02 00:13:53

回答

1

Drawboard不使用它的board參數。

+0

它的工作原理!謝謝!你真的幫了我! – Noob 2013-05-02 00:33:32

0
for(i=0; i<3; i++) /* check rows */ 
    { 
     if(board[i][0]==board[i][1] && board[i][0]==board[i][2]) 
     { 
      return board[i][0]; 
     } 
    } 

您會在這裏看到一個錯誤。如果這是一項家庭作業,請嘗試找出錯誤是你自己的。

另外,我沒有在繪圖板功能中看到任何能讓它使用您的板陣列的東西。

這些是我能看到的兩個最大的問題。祝你好運。

+0

它現在的作品!非常感謝您的幫助 :)! – Noob 2013-05-02 00:33:00

+0

好吧,也許我今晚失明瞭,但是這段代碼有什麼問題? – 2013-05-02 00:37:30

+0

整數我從未初始化。我可能應該在我的引用中加入更多的內容,因爲如果在循環之前聲明瞭「int i」,則片段將會有效。 – Alec 2013-05-02 00:45:10

0
// If the space is occupied 
else 
{ 
    cout<<"The selected space is occupied!" << endl; 
    cout << "Select a different space:" << endl << endl; 
    drawBoard(board); 
} 

這部分也是一個問題。基於你的循環它會永遠運行。 如果你選擇的空間被佔用,你需要結束while(!is_move)循環才能獲得新的輸入。