diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..a070144 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..d24ea8e --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..be256d7 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1571074848637 + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..0990fe5 --- /dev/null +++ b/pom.xml @@ -0,0 +1,15 @@ + + + 4.0.0 + + com.imprioving + battleship + 1.0-SNAPSHOT + + + 11 + + + \ No newline at end of file diff --git a/src/main/java/BattleshipBust.java b/src/main/java/BattleshipBust.java new file mode 100644 index 0000000..49ebe3e --- /dev/null +++ b/src/main/java/BattleshipBust.java @@ -0,0 +1,85 @@ +import java.net.SocketImpl; +import java.util.ArrayList; +import java.util.Scanner; + +public class BattleshipBust { + private GameHelper helper = new GameHelper(); + private ArrayList battleshipList = new ArrayList<>(); + private int numOfGuesses = 0; + + private void setUpGame() { + SimpleBattleship ship1 = new SimpleBattleship(); + ship1.setName("Lucille"); + SimpleBattleship ship2 = new SimpleBattleship(); + ship2.setName("Drax"); + SimpleBattleship ship3 = new SimpleBattleship(); + ship3.setName("Lilith"); +// SimpleBattleship ship4 = new SimpleBattleship(); +// ship4.setName("Vincent"); + battleshipList.add(ship1); + battleshipList.add(ship2); + battleshipList.add(ship3); +// battleshipList.add(ship4); + + System.out.println("Your goal is to sink the three Ships."); + System.out.println("Lucille, Drax, Lilith"); + System.out.println("Try to sink them all in the fewest number of guesses."); + + for (SimpleBattleship lucilleToSet : battleshipList) { + ArrayList newLocation = helper.placeBattleship(3); + ship1.setLocationCells(newLocation); + } + for (SimpleBattleship draxToSet : battleshipList) { + ArrayList newLocation = helper.placeBattleship(3); + ship2.setLocationCells(newLocation); + } + for (SimpleBattleship lilithToSet : battleshipList) { + ArrayList newLocation = helper.placeBattleship(3); + ship3.setLocationCells(newLocation); + } +// for (SimpleBattleship vincentToSet : battleshipList) { +// ArrayList newLocation = helper.placeBattleship(3); +// ship4.setLocationCells(newLocation); +// } + + } + + public void startPlaying() { + while(!battleshipList.isEmpty()) { + String userGuess = helper.getUserInput("Enter a guess"); + checkUserGuess(userGuess); + } + finishGame(); + } + + private void checkUserGuess(String userGuess) { + numOfGuesses++; + String result = "miss"; + for (int x = 0; x < battleshipList.size(); x++) { + result = battleshipList.get(x).checkYourself(userGuess); + if (result.equals("hit")) { + break; + } + if (result.equals("kill")) { + battleshipList.remove(x); + break; + } + } + System.out.println(result); + } + + private void finishGame() { + System.out.println("All Ships are dead! Your stock is now worthless."); + if (numOfGuesses <= 18) { + System.out.println("Blah blach blach"); + } else { + System.out.println("more blah"); + } + } + + public static void main(String[] args) { + BattleshipBust game = new BattleshipBust(); + game.setUpGame(); + game.startPlaying(); + } +} diff --git a/src/main/java/GameHelper.java b/src/main/java/GameHelper.java new file mode 100644 index 0000000..af6cfcd --- /dev/null +++ b/src/main/java/GameHelper.java @@ -0,0 +1,74 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; + +public class GameHelper { + private static final String alphabet = "abcdefg"; + private int gridLength = 7; + private int gridSize = 49; + private int[] grid = new int[gridSize]; + private int comCount = 0; + + public String getUserInput(String prompt) { + String inputLine = null; + System.out.print(prompt + " "); + try { + BufferedReader is = new BufferedReader(new InputStreamReader(System.in)); + inputLine = is.readLine(); + if (inputLine.length() == 0) return null; + } catch (IOException e) { + System.out.println("IOException: " + e); + } + return inputLine.toLowerCase(); + } + + public ArrayList placeBattleship(int comSize) { + ArrayList alphaCells = new ArrayList(); + + String temp = null; + int[] coords = new int[comSize]; + int attempts = 0; + boolean success = false; + int location = 0; + + comCount++; + int incr = 1; + if ((comCount % 2) == 1) { + incr = gridLength; + } + while (!success & attempts++ < 200) { + location = (int) (Math.random() * gridSize); + System.out.println("Try " + location); + int x = 0; + success = true; + while (success && x < comSize) { + if (grid[location] == 0) { + coords[x++] = location; + location += incr; + if (location >= gridSize) { + success = false; + } + if (x > 0 && (location % gridLength == 0)) { + success = false; + } + } else { + success = false; + } + } + } + int x = 0; + int row = 0; + int column = 0; + while (x < comSize) { + grid[coords[x]] = 1; + row = (int) (coords [x] / gridLength); + column = coords [x] % gridLength; + temp = String.valueOf(alphabet.charAt(column)); + + alphaCells.add(temp.concat(Integer.toString(row))); + x++; + } + return alphaCells; + } +} diff --git a/src/main/java/SimpleBattleship.java b/src/main/java/SimpleBattleship.java new file mode 100644 index 0000000..67c4c41 --- /dev/null +++ b/src/main/java/SimpleBattleship.java @@ -0,0 +1,34 @@ +import java.util.ArrayList; + +public class SimpleBattleship { + private ArrayList locationCells; + private String name; + + public void setLocationCells(ArrayList locationCells) { + this.locationCells = locationCells; + } + + public void setName(String name) { + this.name = name; + } + + public String checkYourself(String userInput) { + String result = "miss"; + int index = locationCells.indexOf(userInput); + if (index >= 0) { + locationCells.remove(index); + result = "hit"; + } + if (locationCells.isEmpty()) { + result = "kill"; + System.out.println("Ouch! You sunk " + name + " : ( "); + } if (index <= 0) { + result = "miss"; + } +// else { +// result = "hit"; +// } + return result; + } +} + diff --git a/target/classes/BattleshipBust.class b/target/classes/BattleshipBust.class new file mode 100644 index 0000000..2cdaff5 Binary files /dev/null and b/target/classes/BattleshipBust.class differ diff --git a/target/classes/GameHelper.class b/target/classes/GameHelper.class new file mode 100644 index 0000000..3fed5c3 Binary files /dev/null and b/target/classes/GameHelper.class differ diff --git a/target/classes/META-INF/battleship.kotlin_module b/target/classes/META-INF/battleship.kotlin_module new file mode 100644 index 0000000..2983af7 Binary files /dev/null and b/target/classes/META-INF/battleship.kotlin_module differ diff --git a/target/classes/SimpleBattleship.class b/target/classes/SimpleBattleship.class new file mode 100644 index 0000000..bbd56d1 Binary files /dev/null and b/target/classes/SimpleBattleship.class differ