2016-10-25 3 views
-2

자바를 사용하여 tic tac toe 게임을 만들었지 만 한 가지 문제가 있습니다. 내 코드가 validPlayerOneInputvalidPlayerTwoInput 메소드에서 앞뒤로 튀어 나오기를 바랍니다. 내 주에서 볼 수 있듯이 메서드를 호출 한 후 중지되므로 두 메서드를 모두 절차 적으로 호출합니다. 승자가 결정될 때까지 계속해서 뛰고 싶습니다.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; 
} 

합니다. 예를 들어 플레이어가 잘못된 값을 입력하면 값을 다시 입력하라는 요청이 아닌 다음 플레이어로 이동합니다.

firstPlayer 변수가 전달 된 두 플레이어 모두에게 적용되는 단일 validInputPlayer 메서드를 사용해보십시오. 현재이 메서드에서 반복적 인 코드가 많이 있습니다.

관련 문제