2017-11-11 104 views
-2

我很抱歉請求幫助做家庭作業......但我被卡住了。賦值是TicTacToe java程序中的二維數組。
我的方法moveHuman和moveAI是無效的,但它並沒有節省遊戲進程的價值。所以我把它改成了char [] []方法,但是這導致程序在moveHuman方法被調用後掛起。我知道這一點,因爲我已經輸入了各種打印報表,所以我知道我的程序在進展中的位置,並打印出「moveHuman」,它甚至不會提示我輸入我想移動的位置。如果你想幫助我解決這個問題,那是晚上到午夜時分。大週五晚上的活動!謝謝!!二維數組不能保存價值

// File: TicTacToe.java 
//************************************************************************** 

import java.util.Random; 
import java.util.Scanner; 

public class jkcrosby_TicTacToe 

{ 
private static Scanner get; 
public static void main (String[] args) 
{ 
get = new Scanner (System.in); 
char[][] board = new char[ 3 ][ 3 ]; 

// Initialize the board to all spaces 
for (int r=0; r<board.length; r++){ 
    for(int c=0; c<board[r].length; c++){ 
    board[r][c]= ' '; 
    } 
} 
// Print the game board 
    printBoard(board); 
// Keep playing while the game isn't finished 
    while (checkWinner(board) =='N'){ 
// Have the human player make a move 
    moveHuman(board); 
// Print the board after the user plays 
    printBoard(board); 
// Check to see if the game is finished ('X' wins or 
// it is a tie). If it is, break out of the loop. 
    if (checkWinner(board) == 'X' || checkWinner(board) == 'O' || checkWinner(board) == 'T') 
    break; 
// Have the AI make a move 
    moveAI(board); 
// Print the board after the AI plays 
    printBoard(board); 
// Check to see who the winner is checkWinner(board); 
    } 
// If the winner is 'X' or 'O', print that, otherwise, it is a tie 
    char win=checkWinner(board); 
    switch (win) { 
    case 'X': case 'O': System.out.printf("%c wins!\n", win); 
      break; 
    case 'T': System.out.printf("It is a tie!\n"); 
      break; 
    } 
    }// end of main method 

    /** 
    * Validate a move for the human player 
    * 
    * @param board The game board 
    */ 
    public static void moveHuman (char[][] board) 
    { 
    get= new Scanner (System.in); 
    int r; 
    int c; 
    System.out.println("X's turn!\r\n"); 
    // Get the location from the user and validate it (within bounds 
    do{ 
     System.out.println("Enter the row and column, separated by spaces: "); 
     r=get.nextInt(); 
     c=get.nextInt(); 
    }while((r<0 || r>2 ||c<0||c>2)|| board[r][c] !=' '|| board[r][c]=='\u0000'); // and is already occupied). 
    // Mark the position in the board with an 'X' according to the user's 
    // specified location 
     board[r][c]='X'; 
    //return board[r][c]; 
} //end method moveHuman 

/** 
* Makes a move for the AI, and marks the board with an 'O'. 
* 
* @param board The game board 
*/ public static void moveAI (char[][] board) 
{ 
// Check that the random location generated is valid. 
// Keep recalculating the location if the one generated is 
// already occupied. 
    get = new Scanner (System.in); 
    int r; 
    int c; 
    Random rand = new Random(); 
    System.out.println("O's turn!"); 
    do{ 
     System.out.println("Enter the row and column, separated by spaces: "); 
     r = rand.nextInt(3); 
     c = rand.nextInt(3); 
    } while((r<0|| r>2|| c<0||c>2) || board[r][c]!=' '|| board[r][c]=='\u0000'); 
     board[r][c]='O'; 
// Be sure to mark the valid position in the board with an 'O' 
} 


/* ******************************************************************* * 
* ANYTHING BELOW THIS LINE DOES NOT NEED TO BE MODIFIED BY YOU. YOU * 
* CAN CERTAINLY LOOK AT THE CODE, THOUGH, TO SEE WHAT'S HAPPENING. * 
* IN FACT, YOU'RE ENCOURAGED TO LOOK AT THIS CODE AT SOME POINT.  * 
* ******************************************************************* */ 


/** 
* Prints out the tic-tac-toe board 
* 
* @param board The game board 
*/ 
public static void printBoard (char[][] board) 
{ 
    // Box drawing unicode characters: 

    char a = '\u250c';    // U+250C : top-left 
    char b = '\u2510';    // U+2510 : top-right 
    char c = '\u2514';    // U+2514 : bottom-left 
    char d = '\u2518';    // U+2518 : bottom-right 
    char e = '\u252c';    // U+252C : top-vertical-connector 
    char f = '\u2534';    // U+2534 : bottom-vertical-connector 
    char g = '\u251c';    // U+251C : left-horizontal-connector 
    char h = '\u2524';    // U+2524 : right-horizontal-connector 
    char i = '\u253c';    // U+253C : center plus sign connector 
    char j = '\u2500';    // U+2500 : horizontal 
    char k = '\u2502';    // U+2502 : vertical 
    String l = j + "" + j + "" + j; // Three horizontals 
    // Print out the game board 

    System.out.printf ("\n 0 1 2\n" + 
      " %c%s%c%s%c%s%c\n" + 
      "0 %c %c %c %c %c %c %c\n" + 
      " %c%s%c%s%c%s%c\n" + 
      "1 %c %c %c %c %c %c %c\n" + 
      " %c%s%c%s%c%s%c\n" + 
      "2 %c %c %c %c %c %c %c\n" + 
      " %c%s%c%s%c%s%c\n\n", 
      a, l, e, l, e, l, b, 
      k, board[0][0], k, board[0][1], k, board[0][2], k, 
      g, l, i, l, i, l, h, 
      k, board[1][0], k, board[1][1], k, board[1][2], k, 
      g, l, i, l, i, l, h, 
      k, board[2][0], k, board[2][1], k, board[2][2], k, 
      c, l, f, l, f, l, d); 

}   

/** 
* Checks the result of the game 
* 
* @param board The game board 
* @return   'X' if 'X' is the winner 
*     'O' if 'O' is the winner 
*     'T' if the game is a tie 
*     'N' if the game isn't finished 
*/ 
public static char checkWinner(char[][] board) 
{ 
    if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' ||  // Check row 0 
      board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' || // Check row 1 
      board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X' || // Check row 2 
      board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' || // Check col 0 
      board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' || // Check col 1 
      board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' || // Check col 2 
      board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' || // Check diag \ 
      board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X')  // Check diag/
    { 
     return 'X'; 
    } 
    else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' || // Check row 0 
      board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' || // Check row 1 
      board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O' || // Check row 2 
      board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' || // Check col 0 
      board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' || // Check col 1 
      board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' || // Check col 2 
      board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' || // Check diag \ 
      board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O')  // Check diag/
    { 
     return 'O'; 
    } 

    boolean finished = true; 

    // If there is a blank space in the board, the game isn't finished yet 
    for (int i = 0; i < board.length; i++) 
     for (int j = 0; j < board[ i ].length; j++) 
      if (board[ i ][ j ] == ' ') 
       finished = false; 

    // If the board is finished and 'X' or 'O' wasn't returned, then it is a tie 
    // Otherwise, the game is not finished yet 
    if (finished == true) 
     return 'T'; 
    else 
     return 'N'; 
}  

}

+0

[爲什麼「有人可以幫助我?」不是一個真正的問題?](http://meta.stackoverflow.com/q/284236) –

+0

我還會補充說,你的截止日期不是問題的一部分。如果您有真正的理由,請申請延期。否則,你的截止日期不是我們關心的問題。 –

+0

其實@Stephen C,截止日期是相對的,以反映緊迫性。我很高興你編輯了你寫過的無禮評論......兩次你應該進一步編輯它。不允許擴展。不,我沒有提交截止日期,但我的計劃有效。 :) 感謝你的關心!我很高興在這個社區有像你這樣善良和體貼的人。 – jkcrosby3

回答

0

有在你的舉動驗證錯誤。

moveHuman你應該檢查|| board[r][c] != ' '(即循環應該繼續如果選擇此舉不空白的正方形)。

同樣,在moveAI

do { 
    // Note the bound is *exclusive* - it should be 3 to get 
    // values in the range 0-2 
    r = rand.nextInt(3); 
    c = rand.nextInt(3); 
} while (board[r][c] != ' '); 
+0

是的,我有 ' while(board [r] [c] =='');' 最初,但它沒有任何意義。因爲 'do {... } while while((r <0 || r> 2 || c <0 || c> 2)&& board [r] [c] =='');' – jkcrosby3

+0

它適用於這些更改。我測試過了。 – teppic

+0

您已經測試了所有代碼並獲得了井字遊戲的工作? 好的...我會用==來表達你的意思,並將它改回到那個......但是爲什麼會把人類錯誤地移動到我身上。我將方法從public static void更改爲public static char [] []並返回board [r] [c],因爲它不是「保存」移動。 Eclipse和vim都不喜歡它是char [] []。有什麼想法嗎? 'public static char [] [] moveHuman(char [] [] board) {...... return board [r] [c]; }' – jkcrosby3

0

我修改了代碼,在我的問題,以反映正確的變化和代碼編譯。
特別是:

moveHuman和moveAI需要:

}while((r<0 || r>2 ||c<0||c>2)|| board[r][c] !=' '|| board[r][c]=='\u0000');

moveAI需要:

r = rand.nextInt(3); c = rand.nextInt(3);

,而不是什麼花哨的,我嘗試這樣做,給了我一個無限循環。

爲了簡單起見,我的moveHuman和moveAI方法被恢復爲靜態無效方法。