Question
[Java] I have a Christopher Columbus sails the Ocean Blue Game implemented and I'd like to implement the Singleton Pattern regarding the OceanMap class. I'd
[Java]
I have a Christopher Columbus sails the Ocean Blue Game implemented and I'd like to implement the Singleton Pattern regarding the OceanMap class. I'd like to just have on instance of OceanMap, but I'm unsure how to rewrite the rest of the code to accomodate. Please take a look. Thanks in advance.
import java.util.Random; import javafx.application.*; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.input.KeyEvent; import javafx.scene.layout.AnchorPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Rectangle; import javafx.stage.Stage;
public class OceanExplorer extends Application { // initializes Island Map int[][] islandMap; int count; //Dimensions 24x24 final int dimensions = 24; final int scale = 33; final int islandCount = 3; Scene scene; Pane root; OceanMap oceanMap; Ship ship; PirateShip pirateShip; PirateShip pirateShip1; ImageView shipImageView; ImageView pImageView; ImageView p1ImageView; Label moves;
@Override public void start(final Stage oceanStage) throws Exception { // count = total amount of moves for each game count = 0; oceanMap = new OceanMap(dimensions, scale); islandMap = oceanMap.getMap();
root = new AnchorPane(); drawMap(); // have to draw islands before declaring ships so ships don't hide under an // island drawIslands(islandCount);
// declaring the three different ships/ adding pirates to observer list ship = new Ship(oceanMap); pirateShip = new PirateShip(oceanMap); pirateShip1 = new PirateShip(oceanMap); ship.addObserver(pirateShip); ship.addObserver(pirateShip1); loadShipImage(); loadPirateImage(); loadPirateImage2();
// 'reset' button Button reset = new Button("reset"); // button resets map if pressed reset.setOnAction(new EventHandler
// 'total moves' label moves = new Label("Total moves: " + count); moves.setLayoutX(300); moves.setLayoutY(500); root.getChildren().add(moves);
// setting the scene scene = new Scene(root, 800, 1000); oceanStage.setTitle("Christopher Columbus Sails the Ocean Blue"); oceanStage.setScene(scene); oceanStage.show();
startSailing();
}
// draws 10X10 grid public void drawMap() { for (int x = 0; x < dimensions; x++) { for (int y = 0; y < dimensions; y++) { Rectangle rect = new Rectangle(x * scale, y * scale, scale, scale); rect.setStroke(Color.BLACK); rect.setFill(Color.PALETURQUOISE); // add to the node tree in the pane root.getChildren().add(rect); } } }
// loads image of ship public void loadShipImage() { Image shipImage = new Image("ship.png", 50, 50, true, true); shipImageView = new ImageView(shipImage); shipImageView.setX(ship.getShipLocation().x * scale); shipImageView.setY(ship.getShipLocation().y * scale); root.getChildren().add(shipImageView); }
// loads first pirate ship public void loadPirateImage() { Image shipImage = new Image("pirateShip.png", 50, 50, true, true); pImageView = new ImageView(shipImage); pImageView.setX(pirateShip.getPirateLocation().x * scale); pImageView.setY(pirateShip.getPirateLocation().y * scale); root.getChildren().add(pImageView); }
// loads seconds pirate ship public void loadPirateImage2() { Image shipImage = new Image("pirateShip.png", 50, 50, true, true); p1ImageView = new ImageView(shipImage); p1ImageView.setX(pirateShip1.getPirateLocation().x * scale); p1ImageView.setY(pirateShip1.getPirateLocation().y * scale); root.getChildren().add(p1ImageView); }
// generates islands public void drawIslands(int i) { int count = 0; Random rand = new Random(); while (count < i) { int x; int y; // makes sure islands all are in different spots while (true) { x = rand.nextInt(dimensions); y = rand.nextInt(dimensions); if (islandMap[x][y] != 1) break; } Image islandImage = new Image("island.jpg", 50, 50, true, true); ImageView islandImageView = new ImageView(islandImage); islandImageView.setX(x * scale); islandImageView.setY(y * scale); islandMap[x][y] = 1; root.getChildren().add(islandImageView); count++; } }
// responds to key events private void startSailing() { scene.setOnKeyPressed(new EventHandler
public static void main(String[] args) { launch(args); }
}
import javafx.scene.layout.Pane;
//this class creates the 2d array "map" that the ships navigate through public class OceanMap { private static OceanMap firstInstance = null; private OceanMap() {} public static OceanMap getInstance() { if(firstInstance == null) { firstInstance = new OceanMap(); } return firstInstance; } int[][] myMap; int dimensions; int scale; int islandCount; Pane root; Ship ship;
public OceanMap(int dimensions, int scale) { this.dimensions = dimensions; this.scale = scale; myMap = new int[dimensions][dimensions]; }
public int[][] getMap() { return myMap; } }
import java.awt.Point; import java.util.Observable; import java.util.Observer; import java.util.Random;
public class PirateShip implements Observer { OceanMap oceanMap; Point shipLocation; Point pirateLocation; Random rand = new Random();
public PirateShip(OceanMap oceanMap) { // links ocean to ship this.oceanMap = oceanMap; // initially assigns ship random point while (true) { int x = rand.nextInt(oceanMap.dimensions); int y = rand.nextInt(oceanMap.dimensions); // initial point can't be an island if (oceanMap.getMap()[x][y] != 1) { pirateLocation = new Point(x, y); break; } } } public Point getPirateLocation() { return this.pirateLocation; }
@Override // each pirate ship moves based off the obervable ship's current location public void update(Observable o, Object arg) { if (o instanceof Ship) { shipLocation = ((Ship) o).getShipLocation(); movePirate(); } }
public void movePirate() { // x movement if (pirateLocation.x - shipLocation.x == 0) { } else if (pirateLocation.x - shipLocation.x < 0) { // checks for boundaries AND for islands (can't go through islands) if (pirateLocation.x < 24 && oceanMap.getMap()[pirateLocation.x + 1][pirateLocation.y] != 1) pirateLocation.x++; } else if (pirateLocation.x > 0 && oceanMap.getMap()[pirateLocation.x - 1][pirateLocation.y] != 1) pirateLocation.x--;
// y movement if (pirateLocation.y - shipLocation.y == 0) { } else if (pirateLocation.y - shipLocation.y < 0) { // checks for boundaries AND for islands if (pirateLocation.y < 24 && oceanMap.getMap()[pirateLocation.x][pirateLocation.y + 1] != 1) pirateLocation.y++; } else if (pirateLocation.y > 0 && oceanMap.getMap()[pirateLocation.x][pirateLocation.y - 1] != 1) pirateLocation.y--; }
import java.util.Observable; import java.util.Random; import java.awt.Point;
public class Ship extends Observable { OceanMap oceanMap; Point currentLocation; Random rand = new Random();
public Ship(OceanMap oceanMap) { // links ocean to ship this.oceanMap = oceanMap; oceanMap.ship = this; // initially assigns ship random point while (true) { int x = rand.nextInt(oceanMap.dimensions); int y = rand.nextInt(oceanMap.dimensions); // initial point can't be an island if (oceanMap.getMap()[x][y] != 1) { currentLocation = new Point(x, y); break; } } }
public Point getShipLocation() { return this.currentLocation; }
// directions account for islands public void move(String s) { if (s.equals("EAST")) { if (currentLocation.x < 24 && oceanMap.getMap()[currentLocation.x + 1][currentLocation.y] != 1) currentLocation.x++; } else if (s.equals("WEST")) { if (currentLocation.x > 0 && oceanMap.getMap()[currentLocation.x - 1][currentLocation.y] != 1) currentLocation.x--; } else if (s.equals("NORTH")) { if (currentLocation.y > 0 && oceanMap.getMap()[currentLocation.x][currentLocation.y - 1] != 1) currentLocation.y--; } else { if (currentLocation.y < 24 && oceanMap.getMap()[currentLocation.x][currentLocation.y + 1] != 1) currentLocation.y++; }
// notify the observers setChanged(); notifyObservers(); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started