Advertisement
joxaren

StupidDotComGame

Apr 25th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. import java.util.Random;
  2.  
  3. public class Game {
  4.  
  5.     public static void main(String[] args) {
  6.         int numOfGuesses = 0;
  7.         GameHelper helper = new GameHelper();
  8.         DotCom theDotCom = new DotCom();
  9.  
  10.         Random random = new Random();
  11.         int randNum = random.nextInt(5);
  12.  
  13.         int[] locations = {randNum, randNum + 1, randNum + 2};
  14.  
  15.         theDotCom.setLocationCells(locations);
  16.         boolean isAlive = true;
  17.  
  18.         while (isAlive) {
  19.             String guess = helper.getUserInput("enter a number");
  20.             String result = theDotCom.CheckingAGuess(guess);
  21.             numOfGuesses++;
  22.             if (result.equals("kill")) {
  23.                 isAlive = false;
  24.                 System.out.println("You took " + numOfGuesses + " guesses.");
  25.             }
  26.         }
  27.     }
  28. }
  29.  
  30. ______________
  31. import java.io.*;
  32. public class GameHelper {
  33.     public String getUserInput(String prompt) {
  34.         String inputLine = null;
  35.         System.out.print(prompt + " ");
  36.         try {
  37.             BufferedReader is = new BufferedReader(
  38.                     new InputStreamReader(System.in));
  39.             inputLine = is.readLine();
  40.             if (inputLine.length() == 0 ) return null;
  41.         } catch (IOException e) {
  42.             System.out.println("IOException: " + e);
  43.         }
  44.         return inputLine;
  45.     }
  46. }
  47. __________
  48. public class DotCom {
  49.     int[] locationCells;
  50.     int numOfHits;
  51.  
  52.     public String CheckingAGuess(String UserGuess) {
  53.         int guess = Integer.parseInt(UserGuess);
  54.         String result = "miss";
  55.         for (int cell : locationCells) {
  56.             if (guess == cell) {
  57.                 result = "hit";
  58.                 numOfHits++;
  59.                 break;
  60.             }
  61.         }
  62.         if (numOfHits == locationCells.length) {
  63.             result = "kill";
  64.         }
  65.         System.out.println(result);
  66.         return result;
  67.     }
  68.  
  69.     public void setLocationCells(int[] locs) {
  70.         locationCells = locs;
  71.     }
  72. }
  73. ___________________
  74. public class DotComTest {
  75.     public static void main (String[] args) {
  76.         DotCom dot = new DotCom();
  77.         int[] locations = {2,3,4};
  78.         dot.setLocationCells(locations);
  79.         String userGuess = "2";
  80.         String result = dot.CheckingAGuess(userGuess);
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement