Skip to content

Commit

Permalink
Added generics for easier use.
Browse files Browse the repository at this point in the history
App still broken, doesn't paint the Ships and fails on first MouseClick
  • Loading branch information
Aleksandar authored and Aleksandar committed Jan 10, 2014
1 parent 5243baa commit 768f4be
Show file tree
Hide file tree
Showing 29 changed files with 177 additions and 348 deletions.
Binary file not shown.
Binary file modified battleships/bin/Battleships/Adapters/HomeMousePressListener.class
Binary file not shown.
Binary file modified battleships/bin/Battleships/Agent.class
Binary file not shown.
Binary file modified battleships/bin/Battleships/BattleShipsEngine.class
Binary file not shown.
Binary file modified battleships/bin/Battleships/Behaviors/HShipGridSetter.class
Binary file not shown.
Binary file modified battleships/bin/Battleships/Behaviors/ShipGridSetterBehavior.class
Binary file not shown.
Binary file modified battleships/bin/Battleships/Behaviors/VShipGridSetter.class
Binary file not shown.
Binary file not shown.
Binary file modified battleships/bin/Battleships/Factories/GUIFactory.class
Binary file not shown.
Binary file not shown.
Binary file modified battleships/bin/Battleships/Factories/ShipFactory.class
Binary file not shown.
Binary file modified battleships/bin/Battleships/GUI.class
Binary file not shown.
Binary file modified battleships/bin/Battleships/GameState.class
Binary file not shown.
Binary file modified battleships/bin/Battleships/Grid.class
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import javax.swing.JTextField;

import Battleships.GameState;
import Battleships.Factories.AxisResolverFactory;
import Battleships.Factories.ResolverFactory;
import Battleships.Graphics.PlayerPanel;

public class AttackMousePressListener extends MouseAdapter
Expand Down Expand Up @@ -45,6 +45,6 @@ public void mousePressed(MouseEvent event)
}

private int resolveAxisCoOrdinate(int x) {
return AxisResolverFactory.resolveAxisCoOrdinate(x);
return ResolverFactory.AxisCoOrdinate(x);
}
}
27 changes: 18 additions & 9 deletions battleships/src/Battleships/Adapters/HomeMousePressListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,37 @@
import java.awt.event.MouseEvent;

import Battleships.GUI;
import Battleships.Factories.AxisResolverFactory;
import Battleships.Factories.ResolverFactory;
import Battleships.Ships.AircraftCarrier;
import Enums.Position;

public class HomeMousePressListener extends MouseAdapter {
private GUI gui;

private String currentShip;

public HomeMousePressListener(GUI gui)
{
this.gui = gui;
this.currentShip = AircraftCarrier.class.getName();
}

public void mousePressed(MouseEvent event) {
int gridj= resolveAxisCoOrdinate(event.getX());
int gridi= resolveAxisCoOrdinate(event.getY());
if(!this.gui.gameState.isBothPlayerAndAgentShipsDeployed()) {
gui.deploy(gridi,gridj);
}


gui.placeShip(currentShip, new Position(gridi, gridj, gui.orientation));
currentShip = ResolverFactory.nextShip(currentShip);
if(currentShip == null)
gui.gameState.setPlayerShipsDeployed();
System.out.println("Element corresponds to " + gridi + gridj);

if(gui.gameState.arePlayerShipsDeployed()) {
gui.setOut("All Ships Deployed, Player's Turn! Click on the left grid to fire shots");
gui.setOut(gui.gameState.turnToString());
}
}
private int resolveAxisCoOrdinate(int x) {
return AxisResolverFactory.resolveAxisCoOrdinate(x);
return ResolverFactory.AxisCoOrdinate(x);
}

}
38 changes: 23 additions & 15 deletions battleships/src/Battleships/Agent.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import java.util.Random;

import Battleships.Behaviors.ShooterBehavior;
import Battleships.Factories.ShipFactory;
import Battleships.Factories.ShotFactory;
import Battleships.Ships.AircraftCarrier;
import Battleships.Ships.Battleship;
Expand All @@ -13,19 +14,17 @@
public class Agent
{
private InfluenceMap m = null;
private Grid g = null;
private Grid grid = null;
private int i=-1;
private int j=-1;
private ShooterBehavior shooterBehavior;
// private Random generator;

public Agent()
{
m = new InfluenceMap();
g= new Grid(10,10);
grid = new Grid(10,10);
}


public int getI()
{
return i;
Expand All @@ -45,33 +44,42 @@ public InfluenceMap setSunk(int i, int j)
public void nextShot(InfluenceMap m1, Grid Attackgrid)
{
m = m1;
g = Attackgrid;
grid = Attackgrid;
shooterBehavior = ShotFactory.getShooterBehavior(m.getNumberOfHotspots());
shooterBehavior.nextShot(m, g);
shooterBehavior.nextShot(m, grid);
i = shooterBehavior.i;
j = shooterBehavior.j;
}

public Grid placeShips()
{
while(!g.areShipsPlaced())
while(!grid.areShipsPlaced())
{
Random random = new Random();
int x = random.nextInt(10);
int y = random.nextInt(10);
boolean orientation = random.nextBoolean();
Position position = new Position(x, y, orientation == true ? Orientation.Horizontal : Orientation.Vertical);

// factory.setNextShip(AircraftCarrier.class.getName());
// factory.setNextShip(Battleship.class.getName());
// factory.setNextShip(Destroyer.class.getName());
// factory.setNextShip(Minesweeper.class.getName());
// factory.setNextShip(Submarine.class.getName());
if(!grid.isShipPlaced(AircraftCarrier.class.getSimpleName())) {
grid.addShip(AircraftCarrier.class.getSimpleName(), position);
}
else if(!grid.isShipPlaced(Battleship.class.getSimpleName())) {
grid.addShip(Battleship.class.getSimpleName(), position);
}
else if(!grid.isShipPlaced(Destroyer.class.getSimpleName())) {
grid.addShip(Destroyer.class.getSimpleName(), position);
}
else if(!grid.isShipPlaced(Submarine.class.getSimpleName())) {
grid.addShip(Submarine.class.getSimpleName(), position);
}
else if(!grid.isShipPlaced(Minesweeper.class.getName())) {
grid.addShip(Minesweeper.class.getSimpleName(), position);
}
}

System.out.println("agent grid");
System.out.println(g.toString());
System.out.println(grid.toString());

return g;
return grid;
}
}
31 changes: 4 additions & 27 deletions battleships/src/Battleships/BattleShipsEngine.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
package Battleships;

public class BattleShipsEngine {

public GameState gameState;
public boolean agentWins;
public boolean minePlaced;
public boolean destPlaced;
public boolean subPlaced;
public boolean battlePlaced;

public boolean agentMineSunk;
public boolean agentDestSunk;
public boolean agentSubSunk;
public boolean agentAirSunk;
private GUI gui;
private Agent smith;

Expand Down Expand Up @@ -40,10 +29,6 @@ private void endGame() {
}

private void initializeVariables() {
minePlaced = false;
destPlaced = false;
subPlaced = false;
battlePlaced = false;
gameState = new GameState();
gui = new GUI(gameState);
smith = new Agent();
Expand All @@ -64,11 +49,11 @@ private void waitForPlayerToPlaceShips() {
private void loadingGame() {
gameState.addAgentShips(smith.placeShips());
gameState.setPlayerTurn();
gui.outText.setText(gameState.turnToString());
gui.setOut(gameState.turnToString());
}

private void startGame() {
while (!gameState.IsGameOver())
while (!gameState.areAgentShipsSunk() && !gameState.arePlayerShipsSunk())
{
this.PlayerTurn();
gui.repaint();
Expand All @@ -78,26 +63,23 @@ private void startGame() {
private void isAgentWinner() {
if(gameState.arePlayerShipsSunk())
{
gameState.SetGameOver();
gameState.setPlayerTurn();
}
}
private void isPlayerWinner() {
if(gameState.areAgentShipsSunk())
{
System.out.println("All sunk");
gameState.SetGameOver();
gameState.PlayerIsTheWinner();
}
}
private void AgentTurn() {
while(gameState.isAgentTurn()) {
System.out.println("agent turn");
smith.nextShot(gameState.influenceMap, gameState.compHomeGrid);
gui.agentShot(smith.getI(),smith.getJ());
gameState.agentShot(smith.getI(),smith.getJ(),this.gui);
System.out.println("shot at " + smith.getI() + " " +smith.getJ());
System.out.println(gameState.compHomeGrid.toString());
gameState.determineIfShotSunkAShip(gui, smith);
smith.setSunk(smith.getI(), smith.getJ());
gameState.setShipSunkStates();
this.threadSleep(1000);
this.isAgentWinner();
Expand All @@ -118,11 +100,6 @@ private void PlayerTurn() {
}
}

private void resetGame() {
gameState = new GameState();
gui.reset();
}

public static void main (String args[])
{
BattleShipsEngine engine = new BattleShipsEngine();
Expand Down
4 changes: 2 additions & 2 deletions battleships/src/Battleships/Behaviors/HShipGridSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected void UpdateBoard(int CoordinateX, int CoordinateY) {
}

@Override
protected void IsPositionOccupied(int CoordinateX, int CoordinateY) {
protected void IsPositionOccupied(int CoordinateX, int CoordinateY) throws PositionOccupiedException {
for (int c = CoordinateY; c < CoordinateY + shipLength; c++) {
while (board.getGridVal(CoordinateX, c) != GridValue.EmptyCellValue) {
throw new PositionOccupiedException();
Expand All @@ -23,7 +23,7 @@ protected void IsPositionOccupied(int CoordinateX, int CoordinateY) {
}

@Override
protected void IsPositionExceedsBoard(int CoordinateX, int CoordinateY) {
protected void IsPositionExceedsBoard(int CoordinateX, int CoordinateY) throws PositionExceedsBoardException {
if (CoordinateY + shipLength > board.getWidth()) {
throw new PositionExceedsBoardException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package Battleships.Behaviors;

import Battleships.Grid;
import Battleships.exception.PositionExceedsBoardException;
import Battleships.exception.PositionOccupiedException;
import Enums.GridValue;

public abstract class ShipGridSetterBehavior {
Expand All @@ -24,7 +26,7 @@ public void placeShipOnGrid(int CoordinateX, int CoordinateY) {
this.UpdateBoard(CoordinateX, CoordinateY);
}

protected abstract void IsPositionExceedsBoard(int CoordinateX, int CoordinateY);
protected abstract void IsPositionOccupied(int CoordinateX, int CoordinateY);
protected abstract void IsPositionExceedsBoard(int CoordinateX, int CoordinateY) throws PositionExceedsBoardException;
protected abstract void IsPositionOccupied(int CoordinateX, int CoordinateY) throws PositionOccupiedException;
protected abstract void UpdateBoard(int CoordinateX, int CoordinateY);
}
4 changes: 2 additions & 2 deletions battleships/src/Battleships/Behaviors/VShipGridSetter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected void UpdateBoard(int CoordinateX, int CoordinateY) {
}

@Override
protected void IsPositionOccupied(int CoordinateX, int CoordinateY) {
protected void IsPositionOccupied(int CoordinateX, int CoordinateY) throws PositionOccupiedException {
for (int c = CoordinateX; c < CoordinateX + shipLength; c++) {
while (board.getGridVal(c, CoordinateY) != GridValue.EmptyCellValue) {
throw new PositionOccupiedException();
Expand All @@ -23,7 +23,7 @@ protected void IsPositionOccupied(int CoordinateX, int CoordinateY) {
}

@Override
protected void IsPositionExceedsBoard(int CoordinateX, int CoordinateY) {
protected void IsPositionExceedsBoard(int CoordinateX, int CoordinateY) throws PositionExceedsBoardException {
if (CoordinateX + shipLength > board.getHeight()) {
throw new PositionExceedsBoardException();
}
Expand Down
27 changes: 0 additions & 27 deletions battleships/src/Battleships/Factories/AxisResolverFactory.java

This file was deleted.

7 changes: 2 additions & 5 deletions battleships/src/Battleships/Factories/GUIFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,13 @@ public GUIFactory(GUI gui) {
}

public void resetLayout() {
this.setLayout();
this.initializeVariables();
this.setContentPane();
this.finishLayout();
}

private void initializeVariables() {
gui.paintMineSunk= false;
gui.paintDestSunk= false;
gui.paintSubSunk= false;
gui.paintBattleSunk= false;
gui.paintAirSunk= false;
gui.orientation = Orientation.Vertical;
gui.showMap= true;
}
Expand Down
44 changes: 44 additions & 0 deletions battleships/src/Battleships/Factories/ResolverFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package Battleships.Factories;

import Battleships.Ships.AircraftCarrier;
import Battleships.Ships.Battleship;
import Battleships.Ships.Destroyer;
import Battleships.Ships.Minesweeper;
import Battleships.Ships.Submarine;

public class ResolverFactory {
public static int AxisCoOrdinate(int Coordinate) {
if (Coordinate < 20)
return 0;
else if (Coordinate < 40)
return 1;
else if (Coordinate < 60)
return 2;
else if (Coordinate < 80)
return 3;
else if (Coordinate < 100)
return 4;
else if (Coordinate < 120)
return 5;
else if (Coordinate < 140)
return 6;
else if (Coordinate < 160)
return 7;
else if (Coordinate < 180)
return 8;
else if (Coordinate < 200)
return 9;
return -1;
}
public static String nextShip(String current) {
if(current.equalsIgnoreCase(AircraftCarrier.class.getSimpleName()))
return Battleship.class.getSimpleName();
else if(current.equalsIgnoreCase(Battleship.class.getSimpleName()))
return Destroyer.class.getSimpleName();
else if(current.equalsIgnoreCase(Destroyer.class.getSimpleName()))
return Submarine.class.getSimpleName();
else if(current.equalsIgnoreCase(Submarine.class.getSimpleName()))
return Minesweeper.class.getSimpleName();
return null;
}
}
3 changes: 3 additions & 0 deletions battleships/src/Battleships/Factories/ShipFactory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package Battleships.Factories;

import java.util.Random;

import Battleships.Grid;
import Battleships.Ships.AircraftCarrier;
import Battleships.Ships.Battleship;
Expand All @@ -9,6 +11,7 @@
import Battleships.Ships.Submarine;
import Battleships.exception.PositionExceedsBoardException;
import Battleships.exception.PositionOccupiedException;
import Enums.Orientation;
import Enums.Position;

public class ShipFactory {
Expand Down
Loading

0 comments on commit 768f4be

Please sign in to comment.