Skip to content

Commit

Permalink
MapStore refactored. GameState now responsible if shotIsSuccessful.
Browse files Browse the repository at this point in the history
Still more work left on GUI
  • Loading branch information
Aleksandar authored and Aleksandar committed Jan 2, 2014
1 parent 339db4b commit 804a12d
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 108 deletions.
Binary file modified battleships/bin/Battleships/BattleShipsEngine.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/MapStore.class
Binary file not shown.
80 changes: 1 addition & 79 deletions battleships/src/Battleships/BattleShipsEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private void AgentTurn() {
gui.agentShot(smith.getI(),smith.getJ());
System.out.println("shot at " + smith.getI() + " " +smith.getJ());
System.out.println(gameState.compHomeGrid.toString());
determineIfShotSunkAShip(gui, smith);
gameState.determineIfShotSunkAShip(gui, smith);
gameState.setShipSunkStates();
this.threadSleep(1000);
this.isAgentWinner();
Expand All @@ -120,84 +120,6 @@ private void PlayerTurn() {
}
}

private void determineIfShotSunkAShip(GUI gui, Agent smith) {
System.out.println("Player Home board \n" + gameState.playerHomeGrid.toString());
if(gameState.playerHomeGrid.isMineSunk() && !gui.paintMineSunk)
{
for (int i = 0; i < 10; i++) //change these to ROWS to use the default
{
for (int j = 0; j < 10; j++)//change this to CoLumns for default
{
if(gameState.playerHomeGrid.getGridVal(i,j) == -6)
{
smith.setSunk(i,j);
gui.paintMineSunk = true;
}
}
}
}

if(gameState.playerHomeGrid.isDestSunk() && !gui.paintDestSunk)
{
for (int i = 0; i < 10; i++) //change these to ROWS to use the default
{
for (int j = 0; j < 10; j++)//change this to CoLumns for default
{
if(gameState.playerHomeGrid.getGridVal(i,j) == -1)
{
smith.setSunk(i,j);
gui.paintDestSunk = true;
}
}
}
}

if(gameState.playerHomeGrid.isSubSunk() && !gui.paintSubSunk)
{
for (int i = 0; i < 10; i++) //change these to ROWS to use the default
{
for (int j = 0; j < 10; j++)//change this to CoLumns for default
{
if(gameState.playerHomeGrid.getGridVal(i,j) == -5)
{
smith.setSunk(i,j);
gui.paintSubSunk = true;
}
}
}
}

if(gameState.playerHomeGrid.isBattleSunk() && !gui.paintBattleSunk)
{
for (int i = 0; i < 10; i++) //change these to ROWS to use the default
{
for (int j = 0; j < 10; j++)//change this to CoLumns for default
{
if(gameState.playerHomeGrid.getGridVal(i,j) == -4)
{
smith.setSunk(i,j);
gui.paintBattleSunk = true;
}
}
}
}

if(gameState.playerHomeGrid.isAirSunk() && !gui.paintAirSunk)
{
for (int i = 0; i < 10; i++) //change these to ROWS to use the default
{
for (int j = 0; j < 10; j++)//change this to CoLumns for default
{
if(gameState.playerHomeGrid.getGridVal(i,j) == -3)
{
smith.setSunk(i,j);
gui.paintAirSunk = true;
}
}
}
}
}

private void resetGame() {
gameState = new GameState();
gui.reset();
Expand Down
4 changes: 2 additions & 2 deletions battleships/src/Battleships/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ public void repaint()
{
for (int j = 0; j < 10; j++)//change this to CoLumns for default
{
if ( gameState.playerHomeGrid.getGridVal(i,j) == 1)
if (gameState.playerHomeGrid.getGridVal(i,j) == 1)
MissIcon.paint(attackPanelGraphics,(j*20),(i*20));
else
if ( gameState.isCompHomeGridLessThanMinus1(i,j))
if (gameState.compHomeGrid.getGridVal(i,j) < -1)
HitIcon.paint(attackPanelGraphics,(j*20),(i*20));


Expand Down
78 changes: 78 additions & 0 deletions battleships/src/Battleships/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,82 @@ else if(agentTurn)
public boolean isAgentTurn() {
return agentTurn && !playerTurn;
}

public void determineIfShotSunkAShip(GUI gui, Agent smith) {
System.out.println("Player Home board \n" + playerHomeGrid.toString());
if(playerHomeGrid.isMineSunk())
{
for (int i = 0; i < 10; i++) //change these to ROWS to use the default
{
for (int j = 0; j < 10; j++)//change this to CoLumns for default
{
if(playerHomeGrid.getGridVal(i,j) == -6)
{
smith.setSunk(i,j);
gui.paintMineSunk = true;
}
}
}
}

if(playerHomeGrid.isDestSunk())
{
for (int i = 0; i < 10; i++) //change these to ROWS to use the default
{
for (int j = 0; j < 10; j++)//change this to CoLumns for default
{
if(playerHomeGrid.getGridVal(i,j) == -1)
{
smith.setSunk(i,j);
gui.paintDestSunk = true;
}
}
}
}

if(playerHomeGrid.isSubSunk())
{
for (int i = 0; i < 10; i++) //change these to ROWS to use the default
{
for (int j = 0; j < 10; j++)//change this to CoLumns for default
{
if(playerHomeGrid.getGridVal(i,j) == -5)
{
smith.setSunk(i,j);
gui.paintSubSunk = true;
}
}
}
}

if(playerHomeGrid.isBattleSunk())
{
for (int i = 0; i < 10; i++) //change these to ROWS to use the default
{
for (int j = 0; j < 10; j++)//change this to CoLumns for default
{
if(playerHomeGrid.getGridVal(i,j) == -4)
{
smith.setSunk(i,j);
gui.paintBattleSunk = true;
}
}
}
}

if(playerHomeGrid.isAirSunk())
{
for (int i = 0; i < 10; i++) //change these to ROWS to use the default
{
for (int j = 0; j < 10; j++)//change this to CoLumns for default
{
if(playerHomeGrid.getGridVal(i,j) == -3)
{
smith.setSunk(i,j);
gui.paintAirSunk = true;
}
}
}
}
}
}
60 changes: 33 additions & 27 deletions battleships/src/Battleships/MapStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,66 @@
public class MapStore
{
private ArrayList<InfluenceMap> store;

private int turns;
private InfluenceMap best;

/**
Constructs an empty InfluenceMap store
*/
*/
public MapStore()
{
store = new ArrayList<InfluenceMap>();
}
/**
Adds a InfluenceMap into the InfluenceMap store
*/
*/
public void add(InfluenceMap IM)
{
store.add(IM);
}

/**Returns the map in the arraylist at the specified element if it exists
@throws IllegalArgumentException if the parameter is outside the arrayList boundary*/
public InfluenceMap getMap(int i)
{
int num = store.size();
if(i<0 || i > num)
int num = this.size();

if(i< 0 || i > num)
throw new IllegalArgumentException();
else
return store.get(i);
}

public InfluenceMap getBestMap()
{

int turns = 9999;
InfluenceMap current = null;
InfluenceMap best = null;
for(int i= 0; i<store.size(); i++)
{
current = store.get(i);
if(current.getTurns() < turns)
{
turns = current.getTurns();
best = current;
}

current = null;
}

this.setTurns(9999);
return this.findBest();
}


private InfluenceMap findBest() {
for(int i= 0; i < store.size(); i++)
{
this.setBetter(i);
}
return best;


}

private void setBetter(int index) {
InfluenceMap current = store.get(index);
if(current.getTurns() < turns) {
turns = current.getTurns();
best = current;
}
}

private void setTurns(int turns) {
this.turns = turns;
}

/**
Returns the number of InfluenceMap objects in the store
*/
*/
public int size()
{
return store.size();
Expand Down

0 comments on commit 804a12d

Please sign in to comment.