Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.31 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Exercise4 {
  4.  
  5.     String[] possibleWords = {"cat", "dog", "cow", "milk", "work", "hello", "world", "monkey", "banana"};
  6.     String secretWord = pickRandomWord();
  7.     String underScores;
  8.  
  9.     int lives = 0;
  10.     boolean isWordGuessed = false;
  11.  
  12.  
  13.     public static void main(String[] args) {
  14.  
  15.         new Exercise4().run();
  16.     }
  17.  
  18.     public void run() {
  19.  
  20.         int menuSelection = -1;
  21.  
  22.         while (menuSelection != 0 && !isWordGuessed && lives != 10) {
  23.             printMenu();
  24.             System.out.println();
  25.             System.out.println("Please select menu option");
  26.             menuSelection = userInputMenu(0, 2);
  27.  
  28.             if (menuSelection == 1) {
  29.                 startGame();
  30.             } else if (menuSelection == 2) {
  31.                 System.out.println(secretWord);
  32.             } else {
  33.                 System.out.println("Invalid");
  34.             }
  35.  
  36.  
  37.         }
  38.     }
  39.  
  40.  
  41.     public void printMenu() {
  42.         System.out.println
  43.                 ("******************************************\n" +
  44.                         "* Hangman\n" +
  45.                         "******************************************\n" +
  46.                         "* 1) play\n" +
  47.                         "* 2) Show the word\n" +
  48.                         "* 0) Exit\n" +
  49.                         "******************************************");
  50.  
  51.  
  52.     }
  53.  
  54.     public int userInputMenu(int lower, int higher) {
  55.         Scanner sc = new Scanner(System.in);
  56.         int userInput = sc.nextInt();
  57.         while (userInput < lower && userInput >= higher) {
  58.             System.out.print("That's an invalid input try again: ");
  59.             userInput = sc.nextInt();
  60.  
  61.         }
  62.         return userInput;
  63.  
  64.     }
  65.  
  66.     public String pickRandomWord() {
  67.         int randomNumberOne = (int) (Math.random() * possibleWords.length);
  68.         int randomNumberTwo = (int) (Math.random() * possibleWords.length);
  69.         String randomWord = possibleWords[randomNumberOne];
  70.         possibleWords[randomNumberOne] = possibleWords[randomNumberTwo];
  71.         possibleWords[randomNumberTwo] = randomWord;
  72.  
  73.         return randomWord;
  74.  
  75.     }
  76.  
  77.     public void startGame() {
  78.  
  79.         Scanner sc = new Scanner(System.in);
  80.         while (lives > 10 && underScores.contains("_"))
  81.             System.out.println("Enter any letter");
  82.  
  83.         System.out.println(underScores);
  84.         String userGuess = sc.next();
  85.         gameConditions(userGuess);
  86.  
  87.     }
  88.  
  89.  
  90.     public void gameConditions(String userGuess) {
  91.         String newUnderScore = "";
  92.  
  93.         for (int i = 0; i < secretWord.length(); i++) {
  94.             if (secretWord.charAt(i) == userGuess.charAt(0)) {
  95.                 newUnderScore += userGuess.charAt(0);
  96.             } else if (underScores.charAt(i) != '_') {
  97.                 newUnderScore += secretWord.charAt(i);
  98.             } else {
  99.                 newUnderScore += "_";
  100.             }
  101.         }
  102.         if (underScores.equals(newUnderScore)) {
  103.             lives++;
  104.             printBoard();
  105.         } else {
  106.             underScores = newUnderScore;
  107.         }
  108.         if (underScores.equals(secretWord)) {
  109.             System.out.println("You are the winner! the word was: " + secretWord);
  110.             isWordGuessed = true;
  111.         }
  112.  
  113.  
  114.     }
  115.  
  116.     public void printBoard() {
  117.         if (lives <= 0) {
  118.             System.out.println("");
  119.             System.out.println("");
  120.             System.out.println("");
  121.             System.out.println("");
  122.             System.out.println("");
  123.             System.out.println("_ _ _");
  124.         } else if (lives == 1) {
  125.             System.out.println("");
  126.             System.out.println(" |");
  127.             System.out.println(" |");
  128.             System.out.println(" |");
  129.             System.out.println(" |");
  130.             System.out.println("_|_ _");
  131.         } else if (lives == 2) {
  132.             System.out.println(" ____");
  133.             System.out.println(" |");
  134.             System.out.println(" |");
  135.             System.out.println(" |");
  136.             System.out.println(" |");
  137.             System.out.println("_|_ _");
  138.         } else if (lives == 3) {
  139.             System.out.println(" ____");
  140.             System.out.println(" |/");
  141.             System.out.println(" |");
  142.             System.out.println(" |");
  143.             System.out.println(" |");
  144.             System.out.println("_|_ _");
  145.         } else if (lives == 4) {
  146.             System.out.println(" ____");
  147.             System.out.println(" |/  |");
  148.             System.out.println(" |");
  149.             System.out.println(" |");
  150.             System.out.println(" |");
  151.             System.out.println("_|_ _");
  152.         } else if (lives == 5) {
  153.             System.out.println(" ____");
  154.             System.out.println(" |/  |");
  155.             System.out.println(" |   o");
  156.             System.out.println(" |");
  157.             System.out.println(" |");
  158.             System.out.println("_|_ _");
  159.         } else if (lives == 6) {
  160.             System.out.println(" ____");
  161.             System.out.println(" |/  |");
  162.             System.out.println(" |   o");
  163.             System.out.println(" |   0");
  164.             System.out.println(" |");
  165.             System.out.println("_|_ _");
  166.         } else if (lives == 7) {
  167.             System.out.println(" ____");
  168.             System.out.println(" |/  |");
  169.             System.out.println(" |   o");
  170.             System.out.println(" |  /0");
  171.             System.out.println(" |");
  172.             System.out.println("_|_ _");
  173.         } else if (lives == 8) {
  174.             System.out.println(" ____");
  175.             System.out.println(" |/  |");
  176.             System.out.println(" |   o");
  177.             System.out.println(" |  /0\\");
  178.             System.out.println(" |");
  179.             System.out.println("_|_ _");
  180.         } else if (lives == 9) {
  181.             System.out.println(" ____");
  182.             System.out.println(" |/  |");
  183.             System.out.println(" |   o");
  184.             System.out.println(" |  /0\\");
  185.             System.out.println(" |  /");
  186.             System.out.println("_|_ _");
  187.         } else {
  188.             System.out.println(" ____");
  189.             System.out.println(" |/  |");
  190.             System.out.println(" |   o");
  191.             System.out.println(" |  /0\\");
  192.             System.out.println(" |  / \\");
  193.             System.out.println("_|_ _");
  194.         }
  195.     }
  196.  
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement