Advertisement
joxaren

SlightlyLessStupidDotComGame

May 19th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.36 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class DotCom {
  4.     private ArrayList<String> locationCells;
  5.     private String name;
  6.  
  7.     DotCom(String name) {
  8.         this.name = name;
  9.     }
  10.  
  11.     ArrayList locations = new ArrayList();
  12.  
  13.     void setLocationCells(ArrayList<String> loc) {
  14.         locationCells = loc;
  15.     }
  16.  
  17.     void checkYourself(String tehGuess) {
  18.         String result = "miss";
  19.         int index = locationCells.indexOf(tehGuess);
  20.         if (index >= 0) {
  21.             locationCells.remove(index);
  22.             if (locationCells.isEmpty()) {
  23.                 result = "kill";
  24.                 System.out.println("Ouch! You sank +" + name);
  25.             } else {
  26.                 result = "hit";
  27.             }
  28.         }
  29.     }
  30. }
  31.  
  32. _________________________________
  33.  
  34. import java.util.ArrayList;
  35.  
  36. public class DotComBust {
  37.     private int numOfGuesses;
  38.     private GameHelper helper = new GameHelper();
  39.     private ArrayList<DotCom> dotComsList = new ArrayList<>();
  40.  
  41.     DotComBust () {
  42.         setUpGame();
  43.         startPlaying();
  44.     }
  45.  
  46.     private void setUpGame() {
  47.         DotCom one = new DotCom("bluh.com");
  48.         DotCom two = new DotCom("meh.com");
  49.         DotCom three = new DotCom("ugh.com");
  50.         dotComsList.add(one);
  51.         dotComsList.add(two);
  52.         dotComsList.add(three);
  53.  
  54.         System.out.println("Your goal is to sink three dot coms.");
  55.         System.out.println("bluh.com, meh.com, ugh.com");
  56.         System.out.println("Try to sink them all in the fewest number of guesses.");
  57.  
  58.         for (DotCom dotComsToSet : dotComsList) {
  59.             ArrayList<String> newLocation = helper.placeDotCom(3);
  60.             dotComsToSet.setLocationCells(newLocation);
  61.         }
  62.     }
  63.  
  64.     private void startPlaying() {
  65.         while (!dotComsList.isEmpty()) {
  66.             String userGuess = helper.getUserInput("Enter a guess.");
  67.             CheckUserGuess(userGuess);
  68.         }
  69.         finishGame();
  70.     }
  71.  
  72.     private void CheckUserGuess(String userGuess) {
  73.         numOfGuesses++;
  74.         String result = "miss";
  75.         for (DotCom dotComsToTest : dotComsList) {
  76.             result = dotComsToTest.checkYourself(userGuess);
  77.             if (result.equals("hit")) {
  78.                 break;
  79.             }
  80.             if (result.equals("kill")) {
  81.                 dotComsList.remove(dotComsToTest);
  82.                 break;
  83.             }
  84.         }
  85.         System.out.println(result);
  86.     }
  87.  
  88.     private void finishGame () {
  89.         System.out.println("All Dot Coms are dead! Your stock is now worthless.");
  90.         if (numOfGuesses <= 18) {
  91.             System.out.println("It only took you " + numOfGuesses + " guesses.");
  92.             System.out.println(" You got out before your options sank.");
  93.         } else {
  94.             System.out.println("Took you long enough. "+ numOfGuesses + " guesses.");
  95.             System.out.println("Fish are dancing with your options");
  96.         }
  97.     }
  98.  
  99.     public static void main(String[] args) {
  100.         DotComBust d = new DotComBust();
  101.     }
  102. }
  103.  
  104. ______________________________
  105.  
  106. import java.io.*;
  107. import java.util.*;
  108.  
  109. public class GameHelper {
  110.  
  111.     private static final String alphabet = "abcdefg";
  112.     private int gridLength = 7;
  113.     private int gridSize = 49;
  114.     private int[] grid = new int[gridSize];
  115.     private int comCount = 0;
  116.  
  117.     public String getUserInput(String prompt) {
  118.         String inputLine = null;
  119.         System.out.print(prompt + " ");
  120.         try {
  121.             BufferedReader is = new BufferedReader(
  122.                     new InputStreamReader(System.in));
  123.             inputLine = is.readLine();
  124.             if (inputLine.length() == 0) return null;
  125.         } catch (IOException e) {
  126.             System.out.println("IOException: " + e);
  127.         }
  128.         return inputLine.toLowerCase();
  129.     }
  130.  
  131.     public ArrayList<String> placeDotCom(int comSize) {
  132.         ArrayList<String> alphaCells = new ArrayList<String>();
  133.         String[] alphacoords = new String[comSize]; // holds ‘f6’ type coords
  134.         String temp = null; // temporary String for concat
  135.         int[] coords = new int[comSize]; // current candidate coords
  136.         int attempts = 0; // current attempts counter
  137.         boolean success = false; // flag = found a good location ?
  138.         int location = 0; // current starting location
  139.  
  140.         comCount++; // nth dot com to place
  141.         int incr = 1; // set horizontal increment
  142.         if ((comCount % 2) == 1) { // if odd dot com (place vertically)
  143.             incr = gridLength; // set vertical increment
  144.         }
  145.         while (!success & attempts++ < 200) { // main search loop (32)
  146.             location = (int) (Math.random() * gridSize); // get random starting point
  147.             //System.out.print(" try " + location);
  148.             int x = 0; // nth position in dotcom to place
  149.             success = true; // assume success
  150.             while (success && x < comSize) { // look for adjacent unused spots
  151.                 if (grid[location] == 0) { // if not already used
  152.                     coords[x++] = location; // save location
  153.                     location += incr; // try ‘next’ adjacent
  154.                     if (location >= gridSize) { // out of bounds - ‘bottom’
  155.                         success = false; // failure
  156.                     }
  157.                     if (x > 0 && (location % gridLength == 0)) { // out of bounds - right edge
  158.                         success = false; // failure
  159.                     }
  160.                 } else { // found already used location
  161.                     // System.out.print(" used " + location);
  162.                     success = false; // failure
  163.                 }
  164.             }
  165.         } // end while
  166.  
  167.         int x = 0; // turn location into alpha coords
  168.         int row = 0;
  169.         int column = 0;
  170.         // System.out.println("\n");
  171.         while (x < comSize) {
  172.             grid[coords[x]] = 1; // mark master grid pts. as ‘used’
  173.             row = (int) (coords[x] / gridLength); // get row value
  174.             column = coords[x] % gridLength; // get numeric column value
  175.             temp = String.valueOf(alphabet.charAt(column)); // convert to alpha
  176.  
  177.             alphaCells.add(temp.concat(Integer.toString(row)));
  178.             x++;
  179.             // System.out.print(" coord "+x+" = " + alphaCells.get(x-1));
  180.         }
  181.  
  182.         // System.out.println("\n");
  183.  
  184.         return alphaCells;
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement