Skip to content

Commit

Permalink
Added fleetsize check and exceptions to fleet validation - closes #18 -
Browse files Browse the repository at this point in the history
closes #19
  • Loading branch information
Philip Jensen committed Sep 22, 2015
1 parent a887a71 commit 912b93b
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ public BattleShipConfiguration() {
settings.put(ConfigKeys.GRID_WIDTH.toString(), "10");
settings.put(ConfigKeys.GRID_HEIGHT.toString(), "10");
settings.put(ConfigKeys.PLAYER_COUNT.toString(), "2");

settings.put(ConfigKeys.CANOE_COUNT.toString(), "0");
settings.put(ConfigKeys.CRUISER_COUNT.toString(), "1");
settings.put(ConfigKeys.SUBMARINE_COUNT.toString(), "1");
settings.put(ConfigKeys.DESTROUYER_COUNT.toString(), "1");
settings.put(ConfigKeys.BATTLESHIP_COUNT.toString(), "1");
settings.put(ConfigKeys.CARRIER_COUNT.toString(), "1");
}

public String getProperty(String key) {
Expand All @@ -20,6 +27,12 @@ public String getProperty(String key) {
public enum ConfigKeys {
GRID_WIDTH,
GRID_HEIGHT,
PLAYER_COUNT
PLAYER_COUNT,
CANOE_COUNT,
CRUISER_COUNT,
SUBMARINE_COUNT,
DESTROUYER_COUNT,
BATTLESHIP_COUNT,
CARRIER_COUNT
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package edu.dirtybit.battlechat.exceptions;

import edu.dirtybit.battlechat.BattleShipConfiguration;
import edu.dirtybit.battlechat.GameConfiguration;
import edu.dirtybit.battlechat.model.Fleet;
import edu.dirtybit.battlechat.model.Ship;

public class InvalidFleetsizeException extends Exception {

public InvalidFleetsizeException(String message) {
super(message);
}

public static String MakeMessage(GameConfiguration config, int canoes, int cruisers, int submarines, int destroyers, int battleships, int carriers)
{
String message = "Incorrect fleet size: ";
message += "canoes = " + canoes + "(" + Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.CANOE_COUNT.toString())) + "), ";
message += "cruisers = " + cruisers + "(" + Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.CRUISER_COUNT.toString())) + "), ";
message += "submarines = " + submarines + "(" + Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.CANOE_COUNT.toString())) + "), ";
message += "destroyers = " + canoes + "(" + Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.CANOE_COUNT.toString())) + "), ";
message += "battleships = " + canoes + "(" + Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.CANOE_COUNT.toString())) + "), ";
message += "carriers = " + canoes + "(" + Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.CANOE_COUNT.toString())) + ")";
return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package edu.dirtybit.battlechat.exceptions;

import edu.dirtybit.battlechat.model.Ship;

public class ShipOutOfBoundsException extends Exception {

private Ship ship;

public ShipOutOfBoundsException(Ship ship, int endx, int endy) {
super("Ship of type \"" + ship.getShiptype().toString() + "\" was placed outside the board, or extends out of the board at: (\"" + ship.getX() + "\",\"" + ship.getY() + "\" - \"" + endx + "\",\"" + endy + "\")");
this.ship = ship;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package edu.dirtybit.battlechat.exceptions;

import edu.dirtybit.battlechat.model.Ship;

public class ShipsOverlapException extends Exception {

public ShipsOverlapException(int x, int y) {
super("Two ships overlapped at: (\"" + x + "\",\"" + y + "\")");
}
}
21 changes: 16 additions & 5 deletions src/main/java/edu/dirtybit/battlechat/model/Board.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package edu.dirtybit.battlechat.model;

import edu.dirtybit.battlechat.BattleShipConfiguration;
import edu.dirtybit.battlechat.GameConfiguration;

import java.util.ArrayList;

public class Board extends BaseBoard
{
private Perspective perspective;
private Fleet fleet;
private boolean cleared;

public Board(int width, int height)
public Board(GameConfiguration config)
{
super(width, height);
this.perspective = new Perspective(width, height);
this.width = width;
this.height = height;
super(Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.GRID_WIDTH.toString())), Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.GRID_HEIGHT.toString())));
this.fleet = Fleet.fromConfig(config);
this.perspective = new Perspective(this.width, this.height);
this.clear();
this.cleared = true;
}
Expand All @@ -29,6 +32,14 @@ public Perspective getPerspective()
return this.perspective;
}

public Fleet getFleet() {
return fleet;
}

public void setFleet(Fleet fleet) {
this.fleet = fleet;
}

@Override
public void clear()
{
Expand Down
27 changes: 15 additions & 12 deletions src/main/java/edu/dirtybit/battlechat/model/Fleet.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
package edu.dirtybit.battlechat.model;

import edu.dirtybit.battlechat.BattleShipConfiguration;
import edu.dirtybit.battlechat.GameConfiguration;

import java.util.ArrayList;

public class Fleet {
private static final int CANOE = 0;
private static final int CRUISERS = 1;
private static final int SUBMARINES = 1;
private static final int DESTROYERS = 1;
private static final int BATTLESHIPS = 1;
private static final int CARRIERS = 1;

private ArrayList<Ship> ships;

public Fleet()
{
this(CANOE, CRUISERS, SUBMARINES, DESTROYERS, BATTLESHIPS, CARRIERS);
}

public Fleet(int canoes, int cruisers, int submarines, int destroyers, int battleships, int carriers) {
this.ships = new ArrayList<Ship>();

Expand All @@ -41,6 +32,18 @@ public Fleet(int canoes, int cruisers, int submarines, int destroyers, int battl
}
}

public static Fleet fromConfig(GameConfiguration config)
{
int canoes = Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.CANOE_COUNT.toString()));
int cruisers = Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.CRUISER_COUNT.toString()));
int submarines = Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.SUBMARINE_COUNT.toString()));
int destroyers = Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.DESTROUYER_COUNT.toString()));
int battleships = Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.BATTLESHIP_COUNT.toString()));
int carriers = Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.CARRIER_COUNT.toString()));

return new Fleet(canoes, cruisers, submarines, destroyers, battleships, carriers);
}

public ArrayList<Ship> getShips() {
return ships;
}
Expand Down
89 changes: 65 additions & 24 deletions src/main/java/edu/dirtybit/battlechat/model/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import edu.dirtybit.battlechat.BattleShipConfiguration;
import edu.dirtybit.battlechat.GameConfiguration;
import edu.dirtybit.battlechat.Session;
import edu.dirtybit.battlechat.exceptions.InvalidFleetsizeException;
import edu.dirtybit.battlechat.exceptions.ShipOutOfBoundsException;
import edu.dirtybit.battlechat.exceptions.ShipsOverlapException;

import java.util.ArrayList;
import java.util.Iterator;
Expand All @@ -12,15 +15,11 @@ public class GameState extends Session {
private ArrayList<Board> boards;
private int currentPlayerIndex;

public GameState(GameConfiguration config, Player player)
{
public GameState(GameConfiguration config, Player player) {
super(config, player);
this.boards = new ArrayList<>();

this.initializeBoards(
Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.PLAYER_COUNT.toString())),
Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.GRID_WIDTH.toString())),
Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.GRID_HEIGHT.toString())));
this.initializeBoards(config);
}

@Override
Expand All @@ -29,21 +28,19 @@ public boolean shouldStart() {
.getProperty(BattleShipConfiguration.ConfigKeys.PLAYER_COUNT.toString()));
}

private void initializeBoards(int players, int width, int height)
{
for (int i = 0; i < players; i++)
{
this.boards.add(new Board(width, height));
private void initializeBoards(GameConfiguration config) {
int players = Integer.parseInt(config.getProperty(BattleShipConfiguration.ConfigKeys.PLAYER_COUNT.toString()));

for (int i = 0; i < players; i++) {
this.boards.add(new Board(config));
}
}

private int getPlayerIndex(Player player)
{
private int getPlayerIndex(Player player) {
return getPlayers().indexOf(player);
}

public void setCell(Player player, int x, int y, CellType celltype)
{
public void setCell(Player player, int x, int y, CellType celltype) {
int index = this.getPlayerIndex(player);

boards.get(index).setCell(x, y, celltype);
Expand Down Expand Up @@ -76,47 +73,88 @@ public Player nextPlayer() {
return this.getPlayers().get(this.currentPlayerIndex);
}

public boolean validateFleet(Fleet fleet, Board board) {
public boolean validateFleet(Fleet fleet, Board board) throws ShipOutOfBoundsException, ShipsOverlapException, InvalidFleetsizeException {
boolean isvalid = true;

// TODO: check fleetsize?
// check that the fleet contains the right ships
int canoes,cruisers,submarines,destroyers,battleships,carriers;
canoes = cruisers = submarines = destroyers = battleships = carriers = 0;
for (Ship ship : fleet.getShips())
{
switch (ship.getShiptype())
{
case CANOE:
canoes++;
break;
case DESTROYER:
destroyers++;
break;
case CRUISER:
cruisers++;
break;
case SUBMARINE:
submarines++;
break;
case BATTLESHIP:
battleships++;
break;
case CARRIER:
carriers++;
break;
}
}
if ( canoes != Integer.parseInt(this.getConfig().getProperty(BattleShipConfiguration.ConfigKeys.CANOE_COUNT.toString())) ||
destroyers != Integer.parseInt(this.getConfig().getProperty(BattleShipConfiguration.ConfigKeys.DESTROUYER_COUNT.toString())) ||
cruisers != Integer.parseInt(this.getConfig().getProperty(BattleShipConfiguration.ConfigKeys.CRUISER_COUNT.toString())) ||
submarines != Integer.parseInt(this.getConfig().getProperty(BattleShipConfiguration.ConfigKeys.SUBMARINE_COUNT.toString())) ||
battleships != Integer.parseInt(this.getConfig().getProperty(BattleShipConfiguration.ConfigKeys.BATTLESHIP_COUNT.toString())) ||
carriers != Integer.parseInt(this.getConfig().getProperty(BattleShipConfiguration.ConfigKeys.CARRIER_COUNT.toString())) )
{
isvalid = false;
throw new InvalidFleetsizeException(InvalidFleetsizeException.MakeMessage(this.getConfig(),canoes,cruisers,submarines,destroyers,battleships,carriers));
}

if (board.isClear() == true) {
int i = 0;
while (i < fleet.getShips().size()) {
Ship ship = fleet.getShips().get(i);
int endx = ship.getRotation() == Rotation.Horizontal ? ship.getX() + ship.getShiptype().getLength() - 1 : ship.getX();
int endy = ship.getRotation() == Rotation.Vertical ? ship.getY() + ship.getShiptype().getLength() - 1 : ship.getY();

// Check if the start coordinates are on the board
if (ship.getX() < 0 || ship.getX() > board.getWidth() ||
ship.getY() < 0 || ship.getY() > board.getHeight()) {
isvalid = false;
throw new ShipOutOfBoundsException(ship, endx, endy);
} else {
// Check if end coordinates are on the board
switch (ship.getRotation()) {
case Horizontal:
int endx = ship.getX() + ship.getShiptype().getLength() - 1;
if (endx < 0 || endx > board.getWidth()) {
isvalid = false;
throw new ShipOutOfBoundsException(ship, endx, endy);
} else {
for (int x = ship.getX(); x <= endx; x++) {
if (board.getCells()[x][ship.getY()] == CellType.Empty) {
board.getCells()[x][ship.getY()] = CellType.Ship;
} else {
isvalid = false;
throw new ShipsOverlapException(x, ship.getY());
}
}
}
break;
case Vertical:
int endy = ship.getY() + ship.getShiptype().getLength() - 1;
if (endy < 0 || endy > board.getWidth()) {
isvalid = false;
throw new ShipOutOfBoundsException(ship, endx, endy);
} else {
for (int y = ship.getY(); y <= endy; y++) {
if (board.getCells()[ship.getX()][y] == CellType.Empty) {
board.getCells()[ship.getX()][y] = CellType.Ship;
} else {
isvalid = false;
throw new ShipsOverlapException(ship.getX(), y);
}
}
}
Expand All @@ -134,17 +172,20 @@ public boolean validateFleet(Fleet fleet, Board board) {
if (!isvalid) {
board.clear();
}
} else {
isvalid = false;
}
else { isvalid = false; }

return isvalid;
}

public void fire(Player player, Board board, int x, int y)
{
public void fire(Player player, Board board, int x, int y) {
// Check if not players own board?

if (board.getCells()[x][y] == CellType.Ship) { board.getCells()[x][y] = CellType.Hit; }
else { board.getCells()[x][y] = CellType.Miss; }
if (board.getCells()[x][y] == CellType.Ship) {
board.getCells()[x][y] = CellType.Hit;
} else {
board.getCells()[x][y] = CellType.Miss;
}
}
}
14 changes: 3 additions & 11 deletions src/main/java/edu/dirtybit/battlechat/model/Player.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
package edu.dirtybit.battlechat.model;

import edu.dirtybit.battlechat.GameConfiguration;

import java.util.UUID;

public class Player {
private UUID uuid;
private Fleet fleet;
private String givenName;

public Player(String givenName)
{
uuid = UUID.randomUUID();
fleet = new Fleet();
this.uuid = UUID.randomUUID();
this.givenName = givenName;
}

public UUID getId() {
return uuid;
}

public Fleet getFleet() {
return fleet;
}

public void setFleet(Fleet fleet) {
this.fleet = fleet;
}

public String getGivenName() {
return this.givenName;
}
Expand Down
Loading

0 comments on commit 912b93b

Please sign in to comment.