Example #1
1
  /**
   * Handles the move put from a Client.
   *
   * @param params is a String array containing the blocks to place
   */
  public void doMovePut(String[] params) {
    Map<Point, Block> moves = new HashMap<>();

    for (String move : params) {
      String[] moveArg = move.split("@");
      int blockId = Integer.parseInt(moveArg[0]);
      int moveX = Integer.parseInt(moveArg[1].split(",")[0]);
      int moveY = Integer.parseInt(moveArg[1].split(",")[1]);

      moves.put(new Point(moveX, moveY), new Block(blockId));
    }

    try {
      game.doMovePut(moves);
      System.out.println("[Server] (ClientHandler) - Current game situation:");
      System.out.println(game.getBoard().toString());
    } catch (InvalidMoveException e) {
      sendError(IProtocol.Error.MOVE_INVALID.ordinal() + " Invalid move");
      game.sendPlayerTurn();
    } catch (TilesUnownedException e) {
      sendError(
          IProtocol.Error.MOVE_TILES_UNOWNED.ordinal() + " Player tried to place unowned tile");
      game.sendPlayerTurn();
    } catch (NullPointerException e) {
      System.out.println("[Server] ClientHandler - Game ended during turn.");
    }
  }
Example #2
0
  /**
   * Handles the trade from a Client.
   *
   * @param params is a String array containing the blocks to trade
   */
  public void doMoveTrade(String[] params) {
    List<Block> tradeBlocks = new ArrayList<>();

    for (String block : params) {
      tradeBlocks.add(new Block(Integer.parseInt(block)));
    }

    try {
      game.doMoveTrade(tradeBlocks);
    } catch (TradeFirstTurnException e) {
      sendError(IProtocol.Error.TRADE_FIRST_TURN.ordinal() + " You cannot trade on the first turn");
      game.sendPlayerTurn();
    } catch (TilesUnownedException e) {
      sendError(
          IProtocol.Error.MOVE_TILES_UNOWNED.ordinal() + " Player tried to place unowned tile");
      game.sendPlayerTurn();
    } catch (EmptyBagException e) {
      sendError(
          IProtocol.Error.DECK_EMPTY.ordinal() + " The bag does not contain this many blocks");
      game.sendPlayerTurn();
    }
  }