2017-02-11 5 views
-1

tictactoeのゲームで次の機能を追加したいとします:プレーヤーの電源が入っていても、一定の時間(10秒)別のプレイヤーのターン。特定の時間が経過した後にプレーヤーがJavaで変化するようにする

"GameHub"クラス(1つのゲームだけを作成するためのServerクラスを拡張)では、ゲームの現在の状態を維持し、メッセージとしてサーバーに渡す内部クラス "GameState"すべてのクライアント/プレーヤーに転送されます)。目指す

public class GameHub extends Server { 

private GameState state; 

public GameHub(int port) throws IOException { 
    super(port); 
    state = new GameState(); 
    setAutoreset(true); 
} 

protected void messageReceived(int playerID, Object message) { 
    state.applyMessage(playerID, message); 
    sendToAll(state); 
} 

protected void playerConnected(int playerID) { 
    if (getPlayerList().length == 2) { 
     shutdownServerSocket(); 
     state.startFirstGame(); 
     sendToAll(state); 
    } 
} 

protected void playerDisconnected(int playerID) { 
    state.playerDisconnected = true; 
    sendToAll(state); 
} 

public static class GameState implements Serializable { 

    public boolean playerDisconnected; 

    public char[][] board; 

    public boolean gameInProgress; 

    public int playerPlayingX; 
    public int playerPlayingO; 
    public int currentPlayer; 

    public boolean gameEndedInTie; 
    public int winner; 

    public void applyMessage(int sender, Object message) {   

     if (gameInProgress && message instanceof int[] && sender == currentPlayer) { 
      int[] move = (int[]) message; 
      if (move == null || move.length != 2) { 
       return; 
      } 
      int row = move[0]; 
      int col = move[1]; 
      if (row < 0 || row > 2 || col < 0 || col > 2 || board[row][col] != ' ') { 
       return; 
      } 

      board[row][col] = (currentPlayer == playerPlayingX) ? 'X' : 'O'; 

      if (winner()) { 
       gameInProgress = false; 
       winner = currentPlayer; 
      } else if (tie()) { 
       gameInProgress = false; 
       gameEndedInTie = true; 
      } 

      else { 
       currentPlayer = (currentPlayer == playerPlayingX) ? playerPlayingO : playerPlayingX; 
      } 

     } else if (!gameInProgress && message.equals("newgame")) { 
      startGame(); 
     } 
    } 

    void startFirstGame() { 
     startGame(); 
    } 

    private void startGame() { 
     board = new char[3][3]; 
     for (int i = 0; i < 3; i++) { 
      for (int j = 0; j < 3; j++) { 
       board[i][j] = ' '; 
      } 
     } 
     int xPlr = (Math.random() < 0.5) ? 1 : 2; 
     playerPlayingX = xPlr; // Will be 1 or 2. 
     playerPlayingO = 3 - xPlr; // The other player (3 - 1 = 2, and 3 - 2 = 1) 
     currentPlayer = playerPlayingX; 
     gameEndedInTie = false; 
     winner = -1; 
     gameInProgress = true; 
    } 

    private boolean winner() { 
     if (board[0][0] != ' ' 
       && (board[0][0] == board[1][1] && board[1][1] == board[2][2])) { 
      return true; 
     } 
     if (board[0][2] != ' ' 
       && (board[0][2] == board[1][1] && board[1][1] == board[2][0])) { 
      return true; 
     } 
     for (int row = 0; row < 3; row++) { 
      if (board[row][0] != ' ' 
        && (board[row][0] == board[row][1] && board[row][1] == board[row][2])) { 
       return true; 
      } 
     } 
     for (int col = 0; col < 3; col++) { 
      if (board[0][col] != ' ' 
        && (board[0][col] == board[1][col] && board[1][col] == board[2][col])) { 
       return true; 
      } 
     } 
     return false; 
    } 

    private boolean tie() { 
     for (int i = 0; i < 3; i++) { 
      for (int j = 0; j < 3; j++) { 
       if (board[i][j] == ' ') { 
        return false; 
       } 
      } 
     } 
     return true; 
    } 

} 

}私は「カウントダウン」クラスを持っている時間測定のために

は、経過所要時間後に選手を変更することです。

 public class Countdown { 

     int timer; 

     public void counter(int timeFrame) { 

      timer = timeFrame; 

      Timer TimerA = new Timer(); 
      TimerTask TaskA = new TimerTask() { 

       @Override 
       public void run() { 
        if (timer >= 0) { 
         timer--; 
        } 
        if (timer == -1) { 
         currentPlayer = (currentPlayer == playerPlayingX) ? playerPlayingO : playerPlayingX; 
         TimerA.cancel(); 
        } 

       } 
      }; 
      TimerA.schedule(TaskA, 0, 1000); 
     } 

     public int getTimer(){ 
      return timer; 
     } 

    } 

正確にその部分で私は立ち往生しています。私の意見では、 "GameState"クラスのどこかにタイマーを追加して起動する必要がありますが、何らかの理由で私はそれを正確に把握できません。

int timeFrame = 10; 
Countdown C = new Countdown(); 
C.counter(timeFrame); 

私は

else {currentPlayer = (currentPlayer == playerPlayingX) ? playerPlayingO : playerPlayingX; 
int timeFrame = 10; 
Countdown C = new Countdown(); 
C.counter(timeFrame);} 

それは「elseブロック」で起動しなければならないと思った。しかし、それが正常に動作しない、彼は10秒を遅らせた場合=>それは彼、(単に「playerPlayingO」のために働きます彼のターンを逃す)。ただ、タスクはこのような何かを実行します - playerPlayingXは

答えて

0

はあなたがこのためにtaskを使用することができたJavaFXを使用している場合...

が、私も他の何か欠けているかもしれませ...影響されません:

Thread.sleep(10000); //Wait 10 Secs 
if (activePlayer == initialActivePlayer) switchPlayer(); //Better write your own "ShouldWeSwitch"-Condition 

あなたは同期で少し周りいじると、おそらく選手がスイッチをトリガーするが、これはあなたのゲームのために働くかもしれときにタスクを終了する必要があるかもしれません。

PS:JavaFXを使用していない場合は、Thread

のクラスを作成して、独自のタスクを作成できます
関連する問題