예제 #1
0
  @Override
  public void nextStepForAllGames() {
    lock.writeLock().lock();
    try {
      for (Game game : games) {
        if (game.isGameOver()) {
          game.newGame();
        }
        game.tick();
      }

      HashMap<Player, PlayerData> map = new HashMap<Player, PlayerData>();
      for (int i = 0; i < games.size(); i++) {
        Game game = games.get(i);

        Player player = players.get(i);

        map.put(
            player,
            new PlayerData(
                gameType.getBoardSize(),
                decoder.encode(game.getBoardAsString()),
                player.getScore(),
                game.getMaxScore(),
                game.getCurrentScore(),
                player.getCurrentLevel() + 1,
                player.getMessage()));
      }

      screenSender.sendUpdates(map);

      for (int index = 0; index < players.size(); index++) {
        Player player = players.get(index);
        Game game = games.get(index);
        try {
          String board = game.getBoardAsString().replace("\n", "");

          if (logger.isDebugEnabled()) {
            logger.debug(String.format("Sent for player '%s' board \n%s", player, board));
          }

          controllers.get(index).requestControl(player, board);
        } catch (IOException e) {
          logger.error(
              "Unable to send control request to player "
                  + player.getName()
                  + " URL: "
                  + player.getCallbackUrl(),
              e);
        }
      }
    } catch (Error e) {
      e.printStackTrace();
      logger.error("nextStepForAllGames throws", e);
    } finally {
      lock.writeLock().unlock();
    }
  }
예제 #2
0
  public PlayerServiceImpl() {
    lock = new ReentrantReadWriteLock(true);

    //        gameType = new SnakeGame();
    gameType = new BombermanGame();
    //        gameType = new MinesweeperGame();
    //        gameType = new BattlecityGame();

    decoder = new GuiPlotColorDecoder(gameType.getPlots());
  }
예제 #3
0
 // for testing
 void setGameType(GameType gameType, GameSaver saver) {
   this.gameType = gameType;
   this.saver = saver;
   decoder = new GuiPlotColorDecoder(gameType.getPlots());
 }
예제 #4
0
 @Override
 public int getBoardSize() {
   return gameType.getBoardSize();
 }
예제 #5
0
 @Override
 public String getGameType() {
   return gameType.gameName();
 }