2016-10-25 26 views
-2

我用java構建了一個tic tac toe遊戲,但我只有一個問題。我希望我的代碼能夠從​​和validPlayerTwoInput方法中來回跳動。正如你在我的main中看到的那樣,我在程序上調用了兩個方法,這是不正確的,因爲它只是在方法被調用後停止。我希望這一直持續下去,直到獲勝者確定。用Java中的方法來回穿越

我該怎麼做?

import java.util.*; 

public class tictactoe { 

    private static char board[][] = {{1,2,3}, {4,5,6}, {7,8,9}}; 
    char p1Sym, p2Sym; 

    public tictactoe() { 
     p1Sym ='X'; 
     p2Sym = 'O'; 
     boardFill(); 
    } 

    void boardFill() { 
     for (int i = 0; i < 3; i++) { 
      for (int j = 0; j < 3; j++) { 
       System.out.print(board[i][j]); 
       System.out.print(" | "); 
      } 
      System.out.println(); 
     } 
    } 

    void validInputPlayerOne() { 
     boolean isSet = true; 
     int player1Input, player1CorrectedInput; 
     System.out.println("Player 1, enter a number between 1-9: "); 

     Scanner player1 = new Scanner(System.in); 
     player1Input = player1.nextInt(); 

     Scanner correctedInput = new Scanner(System.in); 

     while(player1Input < 1 || player1Input >= 10) { 
      System.out.println("This isn't a number between 1-9, try again: "); 
      player1CorrectedInput = correctedInput.nextInt(); 
      player1Input = player1CorrectedInput; 
     } 

     // or 
     for (int i = 0; i < 3; i++) { 
      for (int j = 0; j < 3; j++) { 
       if (board[i][j] == player1Input) { 
        // set new value 
        board[i][j] = p1Sym; 
        // set 
        isSet = true; 
       } 
      } 
     } 

     if (!isSet) { 
      System.out.println("not found"); 
     } 
    } 

    void validInputPlayerTwo() { 
     boolean isSet = true; 
     int player2Input, player2CorrectedInput; 
     System.out.println("Player 2, enter a number between 1-9: "); 

     Scanner player2 = new Scanner(System.in); 
     player2Input = player2.nextInt(); 

     Scanner correctedInput = new Scanner(System.in); 

     while(player2Input < 1 || player2Input >= 10) { 
      System.out.println("This isn't a number between 1-9, try again: "); 
      player2CorrectedInput = correctedInput.nextInt(); 
      player2Input = player2CorrectedInput; 
     } 

     // or 
     for (int i = 0; i < 3; i++) { 
      for (int j = 0; j < 3; j++) { 
       if (board[i][j] == player2Input){ 

        board[i][j] = p2Sym; 

        isSet = true; 
       } 
      } 
     } 

     if (!isSet) { 
      System.out.println("not found"); 
     } 
    } 

    public static void main(String[] args) { 
     tictactoe t = new tictactoe();  

     t.validInputPlayerOne(); 
     t.boardFill(); 
     t.validInputPlayerTwo(); 
     t.boardFill();    
    } 
} 

回答

1

你可以這樣做:

int turn = 0; 
while (t.noWinner()) { 
    if (turn % 2 == 0) t.validInputPlayerOne(); 
    else t.validInputPlayerTwo(); 
    t.boardFill(); 
    turn += 1; 
} 

當然,現在你必須實際寫入noWinner功能。

1

回答你的問題直接,你可以有每個移動切換一個布爾值:

boolean firstPlayer = true; 
while (t.gameIsNotFinished()) { 
    if (firstPlayer) 
     t.validInputPlayerOne(); 
    else 
     t.validInputPlayerTwo(); 
    firstPlayer = !firstPlayer; 
} 

但是你有很多的你的代碼,你需要解決的其他問題。例如,如果玩家輸入一個無效值,那麼它會轉到下一個玩家,而不是要求他們重新輸入值。

您還應該嘗試使用一個validInputPlayer方法,該方法適用於兩個玩家的firstPlayer變量傳入。此刻,您在這些方法中有很多重複的代碼。