SHARE
TWEET

Untitled

a guest Dec 14th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.*;
  2. public class Hangman {
  3.   public static void main(String[] args)
  4.   {
  5.     Scanner scanner = new Scanner(System.in);    
  6.     String guesses[] = { "one", "two", "three", "four",
  7.       "five", "eat", "sidewalks", "dog", "please", "doctor",
  8.       "six", "element", "fear", "feel",
  9.       "loyalty", "pride", "humble", "lust", "love", "blood"};
  10.    
  11.     boolean weArePlaying = true;
  12.     while(weArePlaying)
  13.     {
  14.       int randomNumber = (int)(Math.random()*guesses.length);
  15.       char randomWordToGuess[] = guesses[randomNumber].toCharArray();
  16.       int ammountOfGuesses = 6;
  17.       char playerGuess[] = new char[randomWordToGuess.length];
  18.      
  19.       for(int i=0; i<playerGuess.length; i++)
  20.       {
  21.         playerGuess[i] =  '_';
  22.       }
  23.      
  24.       boolean wordIsGuessed = false;
  25.       int tries = 0;
  26.      
  27.       while(tries != ammountOfGuesses)
  28.       {
  29.         System.out.println("Current Guesses: ");
  30.         print(playerGuess);
  31.         System.out.printf("You have %d tries left.\n", ammountOfGuesses-tries);
  32.         System.out.println("Enter a letter: ");
  33.         char input = scanner.nextLine().charAt(0);
  34.         char[]letters=new char[6];
  35.        
  36.         tries++;
  37.  
  38.           for(int i=0; i<randomWordToGuess.length; i++)
  39.           {
  40.             if(randomWordToGuess[i] == input)
  41.             {
  42.               playerGuess[i] = input;
  43.               tries--;
  44.             }
  45.             else
  46.              letters[i]=input;
  47.           }
  48.          
  49.           if(isTheWordGuessed(playerGuess))
  50.           {
  51.             wordIsGuessed = true;
  52.             System.out.println("Congratulations, the word was "+"\n\t''"+guesses[randomNumber]+"''.");
  53.             break;
  54.           }
  55.        
  56.       }
  57.       /* End of wordIsGuessed */
  58.       if(!wordIsGuessed){
  59.         System.out.println("You lose, the word was "+"\n\t''"+guesses[randomNumber]+"''.");
  60.       }
  61.      
  62.       System.out.println("Would you like to play again? (yes/no) ");
  63.       String choice = scanner.nextLine();
  64.       if(choice.equals("no")){
  65.         weArePlaying = false;
  66.       }
  67.      
  68.     }/*End of We Are Playing*/
  69.    
  70.     System.out.println("Game Over!");
  71.   }
  72.  
  73.   public static void print(char array[]){
  74.     for(int i=0; i<array.length; i++){ // Assign empty dashes at start "_ _ _ _ _ _ _ _"
  75.       System.out.print(array[i] + " ");
  76.     }
  77.     System.out.println();
  78.   }
  79.  
  80.   public static boolean isTheWordGuessed(char[] array){
  81.     boolean condition = true;
  82.     for(int i=0; i<array.length; i++){
  83.       if(array[i] == '_'){
  84.         condition = false;
  85.       }
  86.     }
  87.     return condition;
  88.   }
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top