Advertisement
MrDoyle

Arrays) Hangman

Jan 25th, 2021
722
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.Arrays;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.  
  9.         char[] word1 = {'h', 'o', 't', 'e', 'l'};
  10.         char[] word2 = {'c', 'l', 'i', 'f', 'f'};
  11.         char[] word3 = {'b', 'e', 'a', 'c', 'h'};
  12.         char[] word4 = {'r', 'e', 'a', 'l', 'm'};
  13.         char[] word5 = {'a', 't', 't', 'i', 'c'};
  14.  
  15.         char[] finalWord = null;
  16.         Random rand = new Random();
  17.         int rand_int1 = rand.nextInt(6);
  18.  
  19.         if (rand_int1 == 1) {
  20.             finalWord = word1;
  21.         } else if (rand_int1 == 2) {
  22.             finalWord = word2;
  23.         } else if (rand_int1 == 3) {
  24.             finalWord = word3;
  25.         } else if (rand_int1 == 4) {
  26.             finalWord = word4;
  27.         } else {
  28.             finalWord = word5;
  29.         }
  30.  
  31.         String misses = "";
  32.         int maxTries = 10;
  33.         int numberOfGuesses = 0;
  34.  
  35.         char[] guess = {'_', '_', '_', '_', '_'};
  36.  
  37.         while (true) {
  38.             System.out.println("_=_=_=_=_=_=_=_=_=_=_=_=_=_=_");
  39.             System.out.println();
  40.             System.out.print("Word:  ");
  41.  
  42.             for (int i = 0; i < guess.length; i++) {
  43.                 System.out.print(guess[i] + " ");
  44.             }
  45.             System.out.println("\n\nMisses:  " + misses);
  46.  
  47.             if ((Arrays.equals(finalWord, guess))){
  48.                 System.out.println("YOU GOT IT!");
  49.                 break;
  50.             }
  51.  
  52.             if (numberOfGuesses == maxTries) {
  53.                 System.out.println("You used your " + maxTries + " number of tries");
  54.                 System.out.println("You did not guess the word correctly");
  55.                 break;
  56.             }
  57.  
  58.             System.out.print("\nGuess: ");
  59.             Scanner keyboard = new Scanner(System.in);
  60.             String input = keyboard.next();
  61.  
  62.             int total = 0;
  63.             for (int i = 0; i < guess.length; i++) {
  64.                 if (finalWord[i] == input.charAt(0)) {
  65.                     guess[i] = input.charAt(0);
  66.                     total++;
  67.                 }
  68.             }
  69.  
  70.             if (total == 0) {
  71.                 misses = misses + " " + input.charAt(0);
  72.             }
  73.  
  74.             numberOfGuesses = numberOfGuesses + 1;
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement