package de.towerdefence.server.match; import de.towerdefence.server.player.Player; import de.towerdefence.server.server.channels.match.placing.InvalidPlacementReason; import de.towerdefence.server.server.channels.match.placing.Tower; import lombok.AllArgsConstructor; import lombok.Getter; @AllArgsConstructor @Getter public class Match { public final static int MAP_SIZE_X = 10; public final static int MAP_SIZE_Y = 20; private final String matchId; private final Player player1; private final Player player2; private final Tower[][] player1Map = new Tower[MAP_SIZE_X][MAP_SIZE_Y]; private final Tower[][] player2Map = new Tower[MAP_SIZE_X][MAP_SIZE_Y]; public Player getOpponent(Player player) { boolean isPlayer1 = player1.equals(player); boolean isPlayer2 = player2.equals(player); if (!isPlayer1 && !isPlayer2) { return null; } if (isPlayer1) { return player2; } return player1; } /** * @return opponent */ public Player addTower(Player player, Tower tower, int x, int y) throws InvalidPlacementException { if (player != player1 && player != player2) { return null; } if (x < 0 || y < 0 || x + 1 > Match.MAP_SIZE_X || y + 1 > Match.MAP_SIZE_Y) { throw new InvalidPlacementException(InvalidPlacementReason.OUT_OF_BOUNDS); } if (player == player1) { if (player1Map[x][y] != null) { throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED); } player1Map[x][y] = tower; } else { if (player2Map[x][y] != null) { throw new InvalidPlacementException(InvalidPlacementReason.LOCATION_USED); } player2Map[x][y] = tower; } return getOpponent(player); } }