Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/edu/nd/se2018/homework/ChipsChallenge/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Please go to:

https://github.com/cpickard828/SoftwareDevPractices

to see my submission (both deliverable and final submissions!)
This will be my submission area from now on.

Thank you!
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/edu/nd/se2018/homework/ColumbusGame/README.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ignore
15 changes: 15 additions & 0 deletions src/edu/nd/se2018/homework/ColumbusGame/Reflections.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
The design of my game is effective. In OceanExplorer, everything important instance of a class is instantiated.
The OceanMap, columbusShip, pirate1 and pirate2 are all instantiated here. Additionally, oceanMap.drawIslands is called here in order to
create the islands. These are randomly placed in spots that do not contain columbusShip or other islands. Then pirateShips are randomly placed
in areas with no islands or ships. The scene/stage is then set up. The various images are loaded in properly. What allows the images to
move is the startSailing function (also a method of OceanExplorer). Key events are handled, and the player is able to move Columbus in
any of the 4 cardinal directions. He will only move to spots that are in-bounds and have no islands. Once Columbus moves, pirate1 and
pirate2, Observers of columbusShip, the Subject/Observable, will try to move a space closer, if possible. Once all moves have been made,
the images are reloaded in at a different location (to simulate the feeling of movement). The game ends when a pirate ship catches
columbusShip. After a 1.5 second delay, the window closes, indicating that the game is over.

Stetch goal #1: The Reset Button
If I implemented a Reset button, I would simply draw a new map with newly instantiated ships, as if I had just restarted the program.

Stretch goal #2: Images for islands
I could simply iterate through the OceanMap, and load in an island image whenever "1" showed up in my 25x25 ocean map. (1 indicates island).
169 changes: 169 additions & 0 deletions src/edu/nd/se2018/homework/ColumbusGame/src/hwk4/OceanExplorer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package hwk4;

// Cameron Pickard

import java.awt.Point;

import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.KeyEvent;

import javafx.stage.Stage;
import java.util.Random;
import javafx.scene.layout.AnchorPane;

public class OceanExplorer extends Application {
AnchorPane root;
final private int cellSize = 28;
final private int mazeSize = 23;
boolean foundTarget = false;
Scene scene;
ImageView shipImageView, pirate1ImageView, pirate2ImageView;
Image shipImage, pirate1Image, pirate2Image;
OceanMap map = new OceanMap();
//Pane root;
Point targetPoint;
Ship columbusShip;
Thread thread;
int x = 10;
int y = 10;
int p1x;
int p2x;
int p1y;
int p2y;
Random rx = new Random();
Random ry = new Random();
PirateShip pirate1, pirate2;
//scene = new Scene(root,mazeSize*cellSize + cellSize*2,mazeSize*cellSize + cellSize*2);
public void start(Stage stage) throws Exception {
root = new AnchorPane();

ObservableList<Node> myList = root.getChildren();
map.drawMap(myList, cellSize); // all blue squares
//columbusShip = new Ship(x, y, cellSize, map);
map.drawIslands(myList, cellSize, x, y); // add 10 green squares
columbusShip = new Ship(x, y, cellSize, map);

int madeIsland = 0;
// randomly place pirate 1
while(madeIsland == 0) {
p1x = rx.nextInt(25);
p1y = ry.nextInt(25);
if(map.seeSpace(p1x, p1y) == 0 && (p1x != x && p1y != y)) {
pirate1 = new PirateShip(p1x, p1y, cellSize, map);
madeIsland = 1;
}
}
//randomly place pirate 2
madeIsland = 0;

while(madeIsland == 0) {
p2x = rx.nextInt(25);
p2y = ry.nextInt(25);
if(map.seeSpace(p2x, p2y) == 0 && (p2x != x && p2y != y)) {
pirate2 = new PirateShip(p2x, p2y, cellSize, map);
madeIsland = 1;
}
}
columbusShip.addObserver(pirate1);
columbusShip.addObserver(pirate2);
loadImages();
scene = new Scene(root,mazeSize*cellSize + cellSize*2,mazeSize*cellSize + cellSize*2);
stage.setTitle("Columbus Game");
stage.setScene(scene);

stage.show();
startSailing(stage);
}

private void loadImages(){


// Load Pirate 1's ship

pirate1Image = new Image("hwk4\\images\\pirateship.gif",cellSize, cellSize, false, true);
pirate1ImageView = new ImageView(pirate1Image);
pirate1ImageView.setX(pirate1.getLocation().x*cellSize);
pirate1ImageView.setY(pirate1.getLocation().y*cellSize);

root.getChildren().add(pirate1ImageView);

// Load Pirate 2's ship
pirate2Image = new Image("hwk4\\images\\pirateship.gif",cellSize, cellSize, false, true);
pirate2ImageView = new ImageView(pirate2Image);
pirate2ImageView.setX(pirate2.getLocation().x*cellSize);
pirate2ImageView.setY(pirate2.getLocation().y*cellSize);

root.getChildren().add(pirate2ImageView);

// Load Columbus' ship
shipImage = new Image("hwk4\\images\\ColumbusShip.png",cellSize, cellSize, false, true);
shipImageView = new ImageView(shipImage);
shipImageView.setX(columbusShip.getLocation().x*cellSize);
shipImageView.setY(columbusShip.getLocation().y*cellSize);


root.getChildren().add(shipImageView);

}

private void startSailing(Stage stage){
scene.setOnKeyPressed(new EventHandler<KeyEvent>(){
@Override
public void handle(KeyEvent ke) {
switch(ke.getCode()){
case RIGHT:
columbusShip.goEast();
break;
case LEFT:
columbusShip.goWest();
break;
case UP:
columbusShip.goNorth();
break;
case DOWN:
columbusShip.goSouth();
break;
default:
break;
}
// Update images
shipImageView.setX(columbusShip.getLocation().x*cellSize);
shipImageView.setY(columbusShip.getLocation().y*cellSize);

pirate1ImageView.setX(pirate1.getLocation().x*cellSize);
pirate1ImageView.setY(pirate1.getLocation().y*cellSize);

pirate2ImageView.setX(pirate2.getLocation().x*cellSize);
pirate2ImageView.setY(pirate2.getLocation().y*cellSize);

// Check to see if pirates caught Columbus
if(columbusShip.getLocation().x == pirate1.getLocation().x && columbusShip.getLocation().y == pirate1.getLocation().y) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
stage.close();
}
if(columbusShip.getLocation().x == pirate2.getLocation().x && columbusShip.getLocation().y == pirate2.getLocation().y) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
e.printStackTrace();
}
stage.close();
}

}
});
}
public static void main(String[] args) {
launch(args);
}
}
57 changes: 57 additions & 0 deletions src/edu/nd/se2018/homework/ColumbusGame/src/hwk4/OceanMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package hwk4;

// Cameron Pickard
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;

import java.util.Random;



public class OceanMap {
int[][] oceanGrid = new int[25][25]; // 0 empty, 1 island
final int dimensions = 25;

// Create the sea
public void drawMap(ObservableList<Node> root, int scale) {
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);
root.add(rect);
oceanGrid[x][y] = 0;
}
}
}
// Add 10 islands randomly
public void drawIslands(ObservableList<Node> root, int scale, int x, int y) {
Random rx = new Random();
Random ry = new Random();
int newX, newY;
int madeIsland = 0;
Rectangle rect;
for(int i = 0; i < 10; i++) {
madeIsland = 0;
while(madeIsland == 0) {
newX = rx.nextInt(25);
newY = ry.nextInt(25);
if(oceanGrid[newX][newY] == 0 && (newX != x && newY != y)) {
rect = new Rectangle(newX*scale, newY*scale, scale, scale);
rect.setStroke(Color.BLACK);
rect.setFill(Color.GREEN);
root.add(rect);
oceanGrid[newX][newY] = 1;
madeIsland = 1;
}
}
}
}

// Return status of space
public int seeSpace(int x, int y) {
return oceanGrid[x][y];
}
}
114 changes: 114 additions & 0 deletions src/edu/nd/se2018/homework/ColumbusGame/src/hwk4/PirateShip.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package hwk4;

// Cameron Pickard

import java.awt.Point;

import java.util.Observer;
import java.util.Observable;

import javafx.scene.shape.Circle;

public class PirateShip implements Observer {
int xCell;
int yCell;
int unitSize; // Scaling factor
OceanMap oceanMap;

Ship columbusShip;

public PirateShip(int x, int y, int unitSize, OceanMap map){
xCell = x;
yCell = y;
this.unitSize = unitSize;
this.oceanMap = map; // We need a reference to it so we can access the grids!
}

// Function runs when Ship notifies observers
public void update(Observable observable, Object arg) {
columbusShip = (Ship) observable;
int cx = columbusShip.getLocation().x;
int cy = columbusShip.getLocation().y;
if(cx > this.xCell) {
if(goEast()) {
return;
}
else {
if(cy < this.yCell) {
goNorth();
return;
}
else if(cy>this.yCell) {
goSouth();
return;
}
else
return;
}
}
else if(cx < this.xCell) {
if(goWest()) {
return;
}
else {
if(cy < this.yCell) {
goNorth();
return;
}
else if(cy>this.yCell) {
goSouth();
return;
}
else
return;
}
}
else {
if(cy < this.yCell) {
goNorth();
return;
}
else if(cy>this.yCell) {
goSouth();
return;
}
else
return;
}
}
public Point getLocation(){
Point position = new Point(this.xCell, this.yCell);
return position;
}

// Movement functions
public boolean goEast() {
if(this.xCell < 24 && oceanMap.seeSpace(this.xCell + 1, this.yCell) != 1 ) {
this.xCell++;
return true;
}
return false;
}
public boolean goWest() {
if(this.xCell>0 && oceanMap.seeSpace(this.xCell - 1, this.yCell)!= 1 ) {
this.xCell--;
return true;
}
return false;
}
public boolean goSouth() {
if(this.yCell < 24 && oceanMap.seeSpace(this.xCell, this.yCell+1) != 1) {
this.yCell++;
return true;
}
return false;
}
public boolean goNorth() {
if(this.yCell > 0 && oceanMap.seeSpace(this.xCell, this.yCell-1) != 1) {
this.yCell--;
return true;
}
return false;
}
}

Loading