Skip to content

Commit

Permalink
Fixed bug when Newgame throws NullException
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogatinov committed Jan 10, 2014
1 parent c0320ce commit b5e26b5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 18 deletions.
Binary file modified battleships/bin/Battleships/Adapters/HomeMousePressListener.class
Binary file not shown.
Binary file modified battleships/bin/Battleships/Factories/GUIFactory.class
Binary file not shown.
Binary file modified battleships/bin/Battleships/GUI.class
Binary file not shown.
44 changes: 29 additions & 15 deletions battleships/src/Battleships/Adapters/HomeMousePressListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,41 @@ public class HomeMousePressListener extends MouseAdapter {
public HomeMousePressListener(GUI gui)
{
this.gui = gui;
this.currentShip = AircraftCarrier.class.getSimpleName();
}

private void NewGame() {
if(currentShip == null) {
this.currentShip = AircraftCarrier.class.getSimpleName();
}
}

private void AddShip(int gridX, int gridY) {
boolean valid = gui.placeShip(currentShip, new Position(gridX, gridY, gui.orientation));
if(valid)
currentShip = ResolverFactory.nextShip(currentShip);
}

private void NoMoreShips() {
if(currentShip == null) {
gui.gameState.setPlayerShipsDeployed();
}
}

private void Print() {
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());
}
}

public void mousePressed(MouseEvent event) {
if(!gui.gameState.arePlayerShipsDeployed()) {
this.NewGame();
int gridj= resolveAxisCoOrdinate(event.getX());
int gridi= resolveAxisCoOrdinate(event.getY());

boolean valid = gui.placeShip(currentShip, new Position(gridi, gridj, gui.orientation));
if(valid)
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());
}
this.AddShip(gridi, gridj);
this.NoMoreShips();
this.Print();
}
}
private int resolveAxisCoOrdinate(int x) {
Expand Down
4 changes: 4 additions & 0 deletions battleships/src/Battleships/Factories/GUIFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import javax.swing.JTextField;

import Battleships.GUI;
import Battleships.GameState;
import Battleships.Adapters.AttackMousePressListener;
import Battleships.Adapters.HideButtonAction;
import Battleships.Adapters.HomeMousePressListener;
Expand All @@ -36,6 +37,9 @@ public void resetLayout() {
this.initializeVariables();
this.setContentPane();
this.finishLayout();
gui.i = 0;
gui.j = 0;
gui.gameState = new GameState();
}

private void initializeVariables() {
Expand Down
3 changes: 0 additions & 3 deletions battleships/src/Battleships/GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@ else if (gameState.isPlayerHitShot(i,j))

public void reset()
{
i = 0;
j = 0;
gameState = new GameState();
guiFactory.resetLayout();
}

Expand Down

0 comments on commit b5e26b5

Please sign in to comment.