Real.BattleShip

Run Settings
LanguageJava
Language Version
Run Command
package real; import java.util.*; public class BattleShip { private ArrayList<String> locationCells; //an ArrayList of cell locations private String name; // the BattleShip's name public void setLocationCells(ArrayList<String> loc) { // A setter method that updates the BattleShip's location. locationCells = loc; // (Random location provided by the GameHelper placeDotCom() method) } public void setName(String n) { // Your basic setter method name = n; } public String checkYourself(String userInput) { String result = "miss"; int index = locationCells.indexOf(userInput); /* The ArrayList indexOf() method in action! If the user guess is one of the entries in the ArrayList, indexOf() will return its ArrayList location. If not, indexOf() will return -1. */ if (index >= 0) { locationCells.remove(index); // Using ArrayList's remove() method to delete an entry if (locationCells.isEmpty()) { // Using the isEmpty() method to see if all of the locations have been guessed result = "kill"; System.out.println("Ouch! You sunk " + name + " : ( "); // Tell the user when a BattleShip has been sunk } else { result = "hit"; } } return result; // Return: 'miss' or 'hit' or 'kill' } // end method } // close class
package real; import java.util.*; public class BattleShipRunner { // declare and intitialize the variable we'll neet private GameHelper helper = new GameHelper(); private ArrayList<BattleShip> battleShipList = new ArrayList<BattleShip>(); private int numOfGuesses = 0; private void setUpGame() { // first make some dot coms and give them locations // make three BattleShip objects, give'em names, and stick'em in the ArrayList BattleShip one = new BattleShip(); one.setName("Pets.com"); BattleShip two = new BattleShip(); two.setName("eToys.com"); BattleShip three = new BattleShip(); three.setName("Go2.com"); battleShipList.add(one); battleShipList.add(two); battleShipList.add(three); // print brief instructions for user System.out.println("Your goal is to sink three battleships."); System.out.println("Pets.com, eToys.com, Go2.com"); System.out.println("Try to sink them all in the fewest number of guesses"); for (BattleShip battleShip : battleShipList) { // repeat with each BattleShip in the list ArrayList<String> newLocation = helper.placeDotCom(3); // ask the helper for a BattleShip location battleShip.setLocationCells(newLocation); // call the setter method on this BattleShip to give it the location you just got from the helper } // close for loop } // close setUpGame method public void startPlaying(){ while (!battleShipList.isEmpty()) { // as long as the BattleShip list is Not empty String userGuess = helper.getUserInput("Enter a guess"); // get the user input checkUserGuess(userGuess); // call our own checkUserGuess method } // close while finishGame(); // call our own finishGame method } // close method public void checkUserGuess(String userGuess){ // find out if there's a hit (and kill on any BattleShip) numOfGuesses++; // increment the number of guesses the user has made String result = "miss"; // assume its a 'miss', unless told otherwise for (int x = 0; x < battleShipList.size(); x++) { // repeat with all DotComs in the list result = battleShipList.get(x).checkYourself(userGuess); // ask the BattleShip to check the user guess, looking for a hit (or kill) if (result.equals("hit")) { break; // get out of the loop early, no point in testing the others } if (result.equals("kill")) { battleShipList.remove(x); // this guy's dead, so take him out of the DotComs list then get out of the loop // break; } } // close for System.out.println(result); // print the result for the user } // close method private void finishGame(){ // print a message telling the user how he/she did in the game System.out.println("All Dot Coms are dead! Your stock is now worthless."); if (numOfGuesses <= 18) { System.out.println("It only took you " + numOfGuesses + " guesses."); System.out.println(" You got out before your options sank."); } else { System.out.println("Took you long enough. " + numOfGuesses + " guesses."); System.out.println("Fish are dancing with your options."); } }// close method public static void main(String[] args){ BattleShipRunner game = new BattleShipRunner(); //create the game object game.setUpGame(); // tell the game object to set up the game game.startPlaying(); // tell the game object to start the main game play loop (keeps asking for user input and checking the guess) } }
package real; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; /** * Created by fengxiaoping on 5/15/16. */ 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 battleShipCount = 0; public String getUserInput(String prompt) { String inputLine = null; System.out.println(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<String> placeDotCom(int comSize) { // You are not suppose to understand this ArrayList<String> alphaCells = new ArrayList<String>(); String[] alphacoords = new String[comSize]; String temp = null; int[] coords = new int[comSize]; int attempts = 0; boolean success = false; int location; battleShipCount++; int incr = 1; if ((battleShipCount % 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 { System.out.println("used" + location); success = false; } }//end of inner while loop }//end of outer while loop int x = 0; int row = 0; int column = 0; System.out.println("\n"); 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++; // System.out.println(" coord " + x +" = "+ alphaCells.get(x-1)); } //System.out.println("/n"); return alphaCells; }// end of method placeDotCom(int comSize) }
Editor Settings
Theme
Key bindings
Full width
Lines