Skip to content

Commit

Permalink
Added randomPosition to resolverFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogatinov committed Jan 11, 2014
1 parent b60c08e commit 73de1c2
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
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/Factories/ResolverFactory.class
Binary file not shown.
15 changes: 1 addition & 14 deletions battleships/src/Battleships/Agent.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package Battleships;
import java.util.Random;

import Battleships.Behaviors.ShooterBehavior;
import Battleships.Factories.ResolverFactory;
import Battleships.Factories.ShipFactory;
import Battleships.Factories.ShotFactory;
import Battleships.Ships.AircraftCarrier;
import Battleships.Ships.Battleship;
import Battleships.Ships.Destroyer;
import Battleships.Ships.Minesweeper;
import Battleships.Ships.Submarine;
import Enums.Orientation;
import Enums.Position;
public class Agent
{
Expand Down Expand Up @@ -55,12 +47,7 @@ public Grid placeShips()
String currentShip = AircraftCarrier.class.getSimpleName();
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);
Position position = ResolverFactory.randomPosition();
grid.addShip(currentShip, position);
if(grid.isShipPlaced(currentShip)) {
currentShip = ResolverFactory.nextShip(currentShip);
Expand Down
11 changes: 5 additions & 6 deletions battleships/src/Battleships/BattleShipsEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,12 @@ private void initializeVariables() {
private void printInitialState() {
System.out.println("PlayerTurn " + gameState.isPlayerTurn());
System.out.println("Deployed " + gameState.isBothPlayerAndAgentShipsDeployed());

System.out.println("PlayerTurn " + gameState.isPlayerTurn());
System.out.println("Deployed " + gameState.isBothPlayerAndAgentShipsDeployed());
}

private void waitForPlayerToPlaceShips() {
while(!gameState.arePlayerShipsDeployed()){}
while(!gameState.arePlayerShipsDeployed()) {

}
}

private void loadingGame() {
Expand All @@ -69,10 +68,10 @@ private void AgentTurn() {
System.out.println(gameState.compHomeGrid.toString());
smith.setSunk(smith.getI(), smith.getJ());
gameState.setShipSunkStates();
this.threadSleep(1000);
this.wait(1000);
}
}
private void threadSleep(int miliseconds) {
private void wait(int miliseconds) {
try {
Thread.sleep(miliseconds);
} catch (InterruptedException e) {
Expand Down
13 changes: 13 additions & 0 deletions battleships/src/Battleships/Factories/ResolverFactory.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package Battleships.Factories;

import java.util.Random;

import Battleships.Ships.AircraftCarrier;
import Battleships.Ships.Battleship;
import Battleships.Ships.Destroyer;
import Battleships.Ships.Minesweeper;
import Battleships.Ships.Submarine;
import Enums.Orientation;
import Enums.Position;

public class ResolverFactory {
public static int AxisCoOrdinate(int Coordinate) {
Expand Down Expand Up @@ -41,4 +45,13 @@ else if(current.equalsIgnoreCase(Submarine.class.getSimpleName()))
return Minesweeper.class.getSimpleName();
return null;
}

public static Position randomPosition() {
Random random = new Random();
int x = random.nextInt(10);
int y = random.nextInt(10);
boolean orientation = random.nextBoolean();
return new Position(x, y,
orientation == true ? Orientation.Horizontal : Orientation.Vertical);
}
}

0 comments on commit 73de1c2

Please sign in to comment.