2017-02-11 124 views
-1

我想在tictactoe游戏中添加以下功能:如果玩家轮到他在某段时间(10秒)内没有做任何事情,另一位玩家轮到了。在GameHub类中(扩展了一个用于创建一个游戏的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块”

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

启动但它不能正常工作=>它只是为“playerPlayingO”(如果他延迟10秒,他错过了他)。 playerPlayingX不受影响......

可能我也失去了一些东西别的......

回答

0

如果你使用一个JavaFX您可以使用此一task - 只需交任务运行是这样的:

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

后,可能需要玩家触发开关摆弄同步一点,也许终止任务,但这可能工作为您的游戏。

PS:如果你不使用JavaFX,你可以简单地创建一个类来扩展你自己的任务:Thread