2013-11-09 71 views
0

我幾乎完成了這個作業分配,但是我的輸出有一些打印錯誤。 遊戲功能正常,但方法printOBoardprintXBoard存在問題,當遊戲進行時,它會攪亂遊戲板。遊戲進行的時間越長,問題越嚴重。謝謝你的幫助。Java中的井字棋打印錯誤

import java.util.Scanner; 

public class TicTacToe { 

private enum Tiles { 
    X, O, EMPTY; 
} 
public static void main(String[] args) { 

    int i; 
    int j; 
    //initialize 2d array of enum types called "board" 
    Tiles[][] board = new Tiles[3][3]; 
    for (i=0; i<board.length; i++) 
     for (j=0; j<board.length; j++) 
      board[i][j] = Tiles.EMPTY; 

    //print out an empty board as a 2d array, with each tile set as "EMPTY" 
    printBoard(board); 
    int row, col; 
    int countEmpty=0; 

    //initial countEmpty count, if it's less than 1, the board is full and the game is over. 
    for (i=0; i<board.length; i++) 
     for (j=0; j<board.length; j++) 
      if (board[i][j] == Tiles.EMPTY) 
       countEmpty++; 

    while (countEmpty > 0) { 

     //Player O enters the row coordinate 
     System.out.println("Player O's turn."); 
     System.out.println("Player O: Enter row (0, 1, or 2):"); 
     Scanner stdin1 = new Scanner(System.in); 
     row = stdin1.nextInt(); 

     //Player O enters the column coordinate 
     System.out.println("Player O: Enter column (0, 1, or 2):"); 
     Scanner stdin2 = new Scanner(System.in); 
     col = stdin2.nextInt(); 
     //If the tile is empty, it was a valid move, and an 'O' is placed on the spot. 
     if (board[row][col] == Tiles.EMPTY) { 
      board[row][col] = Tiles.O; 
      //MOVE FOR O ******************** 
      printOBoard(board, row, col); 
      checkWin(board); 
      if (checkWin(board) == 2) 
       ; 
      else 
       break; 


     } 
     //If the tile is not empty, it was not a valid move, and Player O is prompted to try again. 
     else { 
      System.out.println("Space already occupied. Please choose another."); 
      System.out.println("Player O's turn."); 
      //Player 0 enters the row coordinate 
      System.out.println("Player O: Enter row (0, 1, or 2):"); 
      stdin1 = new Scanner(System.in); 
      row = stdin1.nextInt(); 

      //Player O enters the column coordinate 
      System.out.println("Player O: Enter column (0, 1, or 2):"); 
      stdin2 = new Scanner(System.in); 
      col = stdin2.nextInt(); 
      //ERROR MOVE FOR O******************** 
      board[row][col] = Tiles.O; 
      printOBoard(board, row, col); 



      checkWin(board); 
      if (checkWin(board) == 2) 
       ; 
      else 
       break; 
     } 



     //Player X enters the row coordinate 
     System.out.println("Player X's turn."); 
     System.out.println("Player X: Enter row (0, 1, or 2):"); 
     Scanner stdin3 = new Scanner(System.in); 
     row = stdin3.nextInt(); 

     //Player X enters the column coordinate 
     System.out.println("Player X: Enter column (0, 1, or 2):"); 
     Scanner stdin4 = new Scanner(System.in); 
     col = stdin4.nextInt(); 

     if (board[row][col] == Tiles.EMPTY) { 
      board[row][col] = Tiles.X; 
     printXBoard(board, row, col); 
      //MOVE FOR X ************************************************* 
     checkWin(board); 
     if (checkWin(board) == 2) 
      ; 
     else 
      break; 
     } 

     else { 
      System.out.println("Space already occupied. Please choose another."); 
      System.out.println("Player O's turn."); 
      System.out.println("Player O: Enter row (0, 1, or 2):"); 
      stdin3 = new Scanner(System.in); 
      row = stdin3.nextInt(); 

      //Player O enters the column coordinate 
      System.out.println("Player O: Enter column (0, 1, or 2):"); 
      stdin4 = new Scanner(System.in); 
      col = stdin4.nextInt(); 
      board[row][col] = Tiles.O; 
      //ERROR MOVE FOR X **************************************** 
      printXBoard(board, row, col); 
      checkWin(board); 
      if (checkWin(board) == 2) 
       ; 
      else 
       break; 
     } 

     //After both players move, we check to see if the board is full. 
     countEmpty = 0; 
     for (i=0; i<board.length; i++) 
      for (j=0; j<board.length; j++) 
       if (board[i][j] == Tiles.EMPTY) 
        countEmpty++;  

    } 

} 

//method printBoard prints out a grid of EMPTY's and returns nothing 
private static void printBoard(Tiles board[][]) { 
    int i, j; 

    System.out.println(" -----------------------------"); 
    System.out.println("|   |   |   |"); 
    for (i=0; i<board.length; i++){ 
     for (j=0; j<board.length; j++){ 
      System.out.printf("| " + board[i][j] + " "); 
     } 
     System.out.println("|"); 
     System.out.println("|   |   |   |"); 
     System.out.println(" -----------------------------"); 
     if (i<2) 
      System.out.println("|   |   |   |"); 
    } 
    return; 
} 

//method printXBoard prints out the grid modified with the addition of an X after Player X's turn 
private static void printXBoard(Tiles board[][], int curRow, int curCol) { 
    int i, j; 

    System.out.println(" -----------------------------"); 
    System.out.println("|   |   |   |"); 
    for (i=0; i<board.length; i++){ 
     for (j=0; j<board.length; j++){ 
      if (i == curRow && j == curCol) 
       board[i][j] = Tiles.X; 
      else 
       ; 
      System.out.printf("| " + board[i][j] + " "); 
     } 
     System.out.println("|"); 
     System.out.println("|   |   |   |"); 
     System.out.println(" -----------------------------"); 
     if (i<2) 
      System.out.println("|   |   |   |"); 
    } 
    return; 
} 

//method printOBoard prints out the grid modified with the addition of an X after Player X's turn 
private static void printOBoard(Tiles board[][], int curRow, int curCol) { 
    int i, j; 

    System.out.println(" -----------------------------"); 
    System.out.println("|   |   |   |"); 
    for (i=0; i<board.length; i++){ 
     for (j=0; j<board.length; j++){ 
      if (i == curRow && j == curCol) 
       board[i][j] = Tiles.O; 
      else 
       ; 
      System.out.printf("| " + board[i][j] + " "); 
     } 
     System.out.println("|"); 
     System.out.println("|   |   |   |"); 
     System.out.println(" -----------------------------"); 
     if (i<2) 
      System.out.println("|   |   |   |"); 
    } 
    return; 
} 


//method checkWin checks all possible winning combinations for both players. 
private static int checkWin(Tiles board[][]) { 
    if (board[0][0] == Tiles.X && board[0][1] == Tiles.X && board[0][2] == Tiles.X){ 
     System.out.println("Player X wins!"); 
     return 1; 
    } 
    else if (board[0][0] == Tiles.X && board[1][1] == Tiles.X && board[2][2] == Tiles.X){ 
     System.out.println("Player X wins!"); 
     return 1; 
    } 
    else if (board[0][0] == Tiles.X && board[1][0] == Tiles.X && board[2][0] == Tiles.X){ 
     System.out.println("Player X wins!"); 
     return 1; 
    } 
    else if (board[1][0] == Tiles.X && board[1][1] == Tiles.X && board[1][2] == Tiles.X){ 
     System.out.println("Player X wins!"); 
     return 1; 
    } 
    else if (board[2][0] == Tiles.X && board[2][1] == Tiles.X && board[2][2] == Tiles.X){ 
     System.out.println("Player X wins!"); 
     return 1; 
    } 
    else if (board[0][1] == Tiles.X && board[1][1] == Tiles.X && board[2][1] == Tiles.X){ 
     System.out.println("Player X wins!"); 
     return 1; 
    } 
    else if (board[0][2] == Tiles.X && board[1][2] == Tiles.X && board[2][2] == Tiles.X){ 
     System.out.println("Player X wins!"); 
     return 1; 
    } 
    else if (board[2][0] == Tiles.X && board[1][1] == Tiles.X && board[0][2] == Tiles.X){ 
     System.out.println("Player X wins!"); 
     return 1; 
    } 

    //check if player O wins 
    else if (board[0][0] == Tiles.O && board[0][1] == Tiles.O && board[0][2] == Tiles.O){ 
     System.out.println("Player O wins!"); 
     return 0; 
    } 
    else if (board[0][0] == Tiles.O && board[1][1] == Tiles.O && board[2][2] == Tiles.O){ 
     System.out.println("Player O wins!"); 
     return 0; 
    } 
    else if (board[0][0] == Tiles.O && board[1][0] == Tiles.O && board[2][0] == Tiles.O){ 
     System.out.println("Player O wins!"); 
     return 0; 
    } 
    else if (board[1][0] == Tiles.O && board[1][1] == Tiles.O && board[1][2] == Tiles.O){ 
     System.out.println("Player O wins!"); 
     return 0; 
    } 
    else if (board[2][0] == Tiles.O && board[2][1] == Tiles.O && board[2][2] == Tiles.O) { 
     System.out.println("Player O wins!"); 
     return 0; 
    } 
    else if (board[0][1] == Tiles.O && board[1][1] == Tiles.O && board[2][1] == Tiles.O) { 
     System.out.println("Player O wins!"); 
     return 0; 
    } 
    else if (board[0][2] == Tiles.O && board[1][2] == Tiles.O && board[2][2] == Tiles.O){ 
     System.out.println("Player O wins!"); 
     return 0; 
    } 
    else if (board[2][0] == Tiles.O && board[1][1] == Tiles.O && board[0][2] == Tiles.O) { 
     System.out.println("Player O wins!"); 
     return 0; 
    } 
    else 
     return 2; 
} 
} 
+0

Yikes - 這是很多代碼。考慮嘗試首先使用調試器或者用'System.out.println(「spot 120 variable x is:」+ x)''來檢查程序運行時的變量值來調試代碼。 –

+0

問題在於PrintOBoard和PrintXBoard的方法 - 大多數代碼都在一種方法中,用於檢查每種可能的獲勝Tic-Tac-Toe組合(稱爲checkWin),並將其放在最後。其餘的不應該那麼長。變量似乎是正確的,這只是一個間距問題 – user2044189

回答

0

有在第2次嘗試放置X(如果空間被O佔用),複製粘貼從O編碼,並沒有改變球員&拼貼的問題。

System.out.println("Player O's turn."); 
board[row][col] = Tiles.O; 

這是痛苦的閱讀代碼 - _ - 但你會得到更好的最終)。

0

爲什麼在將信件打印到您的開發板(我在我的Mac上運行)時發生這種情況的原因是,當您打印字母時,您正在使用println ...因爲您「再伴隨一定量的每個你應該用printf塊空間...例如:

System.out.printf("%10s", LetterVariableForTurn); 

這將給LetterVariableForTurn 10位文字,中編寫。

這裏是一個鏈接,幫助您更:printf

希望這有助於!