Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- public class Game {
- public static void main(String[] args) {
- int numOfGuesses = 0;
- GameHelper helper = new GameHelper();
- DotCom theDotCom = new DotCom();
- Random random = new Random();
- int randNum = random.nextInt(5);
- int[] locations = {randNum, randNum + 1, randNum + 2};
- theDotCom.setLocationCells(locations);
- boolean isAlive = true;
- while (isAlive) {
- String guess = helper.getUserInput("enter a number");
- String result = theDotCom.CheckingAGuess(guess);
- numOfGuesses++;
- if (result.equals("kill")) {
- isAlive = false;
- System.out.println("You took " + numOfGuesses + " guesses.");
- }
- }
- }
- }
- ______________
- import java.io.*;
- public class GameHelper {
- 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;
- }
- }
- __________
- public class DotCom {
- int[] locationCells;
- int numOfHits;
- public String CheckingAGuess(String UserGuess) {
- int guess = Integer.parseInt(UserGuess);
- String result = "miss";
- for (int cell : locationCells) {
- if (guess == cell) {
- result = "hit";
- numOfHits++;
- break;
- }
- }
- if (numOfHits == locationCells.length) {
- result = "kill";
- }
- System.out.println(result);
- return result;
- }
- public void setLocationCells(int[] locs) {
- locationCells = locs;
- }
- }
- ___________________
- public class DotComTest {
- public static void main (String[] args) {
- DotCom dot = new DotCom();
- int[] locations = {2,3,4};
- dot.setLocationCells(locations);
- String userGuess = "2";
- String result = dot.CheckingAGuess(userGuess);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement