Advertisement
Vermiculus

Untitled

Dec 6th, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.00 KB | None | 0 0
  1. import java.util.*;
  2. /**
  3. * A simple game of Hangman, chooses a random word, 6 guesses are allowed, the user has the option of
  4. * playing again, and keeps track of how many games they have won.
  5. *
  6. * Laura Andre and Kevin Bolger
  7. * 12/5/11
  8. */
  9.  
  10. public class HangMan
  11. {
  12.  
  13.     public static int wordLength; //declares a public variable to count the number of letters in the word, static becuase it is used in both methods
  14.     public static String word; //declares a public variable word that will one of the twenty secret words, static becuase it is used in both methods
  15.     public int won = 0; //declare and initialize the public variable for the number of times the user wins the game, only used in Game method
  16.     public int lost = 0; //declare and initialize the public variable for the number of times the user looses the game, only used in Game method
  17.     public static HangMan Game = new HangMan(); //creates an public object to the method Game, static becuase it is used in both methods
  18.  
  19.     //main method
  20.     public static void main(String [] args){
  21.         //initial text displayed to user
  22.         System.out.println("Welcome to the game of Hangman!");
  23.         System.out.println("You will have 6 chances to guess what the word is.");  
  24.        
  25.         Game.Game(); //calls on the method Game
  26.     }
  27.  
  28.     //Game method
  29.     public void Game(){
  30.         word = null; //no initial value for the String word
  31.         Random rand = new Random(); //initializes random number calculator
  32.         int wordNum = rand.nextInt(19); //sets a random number value to the variable wordNum
  33.        
  34.         //creates a switch statement that assigns the numbers 0 to 20 to the String word, each with a different secret word
  35.         switch(wordNum){
  36.         case 0: word = "table"; break;
  37.         case 1: word = "computer"; break;
  38.         case 2: word = "house"; break;
  39.         case 3: word = "desk"; break;
  40.         case 4: word = "daring"; break;
  41.         case 5: word = "chair"; break;
  42.         case 6: word = "lamp"; break;
  43.         case 7: word = "phone"; break;
  44.         case 8: word = "chipotle"; break;
  45.         case 9: word = "pencil"; break;
  46.         case 10: word = "raise"; break;
  47.         case 11: word = "upwards"; break;
  48.         case 12: word = "movie"; break;
  49.         case 13: word = "fresh"; break;
  50.         case 14: word = "yield"; break;
  51.         case 15: word = "savor"; break;
  52.         case 16: word = "drive"; break;
  53.         case 17: word = "brick"; break;
  54.         case 18: word = "orange"; break;
  55.         case 19: word = "great"; break;
  56.         }
  57.        
  58.         StringBuilder userWord = new StringBuilder(wordLength); //builds a String userWord that will be the blanks that user fills in by inputting the correct letters of word
  59.        
  60.         wordLength = word.length(); //the variable word length is equal to the specific number of characters in whichever word is randomly chosen
  61.        
  62.         //for loop that assigns an underscore to indicate each of the characters in the randomly chosen word
  63.         for(int i = 0; i < wordLength; i++){
  64.             userWord.append('_');
  65.             userWord.append(' ');
  66.         }
  67.  
  68.        
  69.        
  70.         System.out.println("Your word is " + wordLength + " letters long. \n"); //tells the user the length of the randomly chosen word
  71.         int maxTries = 6; //sets the value for the variable maxTries, the user gets six guesses to guess the word
  72.         StringBuilder guessedLetters = new StringBuilder(); //builds the String that will show the user the letters they have guessed
  73.         int correct = 0; //sets the initial value to zero for the variable that will indicate how many letters the user has guessed correctly
  74.         String guessLetter; //variable for the letter that the user inputs as a guess
  75.        
  76.         int right = 0; //sets the initial value to zero
  77.         //while loop that repeats while the maxTries is greater than zero AND when the number of correct guesses is less than the number of letters in the secret word
  78.         while(maxTries > 0 && correct<wordLength){
  79.             System.out.println("The letters that you have guessed are: " + guessedLetters); //displays which letters the user has already guessed
  80.             System.out.print("What is your guess? "); //asks the user for their guess
  81.             Scanner input = new Scanner(System.in); //initializes the scanner
  82.             guessLetter = input.nextLine(); //sets the user's guess to the String guessLetter
  83.             System.out.print("\n");
  84.             guessedLetters.append(guessLetter + " "); //appends the String guessedLetters to add the user's last guess to the list of guessed letters
  85.            
  86.             right = 0; //resets initial value to zero inside of while loop
  87.            
  88.             //for loop repeats while i is less than the number of letters in the secret word
  89.             for(int i = 0; i < wordLength; i++){
  90.                 //if statement for when the user guesses the correct letter
  91.                 if(guessLetter.charAt(0) == word.charAt(i))
  92.                 {
  93.                     userWord.setCharAt(2*i, guessLetter.charAt(0)); //sets the correct guessed letter to the correct spot in the userWord that the user is building
  94.                     correct++; //increases value by one
  95.                     right++; //increases value by one
  96.                    
  97.                     System.out.println("Correct!" + "\n"); //tells user that their guess was correct
  98.                     System.out.println(userWord + "\n"); //prints out how much of the userWord the user has built so far
  99.                 } //ends if statement      
  100.             } //ends for loop
  101.            
  102.             //if statement for when the user's guess is incorrect
  103.             if(right == 0){ //the right guess will still be zero because it was an incorrect guess
  104.                 maxTries--; //decreases the number of guesses the user has left by one
  105.                 System.out.println("Incorrect, you now have " + maxTries + " wrong guesses left.\n"); //tells the user that the guess was incorrect and how many guesses they have left
  106.                 System.out.println(userWord + "\n"); //prints out how much of the userWord the user has built so far
  107.             } //ends if statement  
  108.         } //ends while loop
  109.        
  110.         System.out.println(userWord + "\n"); //prints out how much of the userWord the user has built so far
  111.        
  112.         System.out.println("The word was: " + word); //tells the user what the secret word was that they were trying to guess
  113.         //creates an if statement that will keep track of the user's score, how many games they have won and lost
  114.         if(maxTries == 0){ //condition: when the user runs out of guesses
  115.             lost++; //they lost that game, so increases the number of loses by one
  116.         }else{ //if they have guesses left, that means that they guessed the word correctly
  117.             won++; //meaning they won the game, so number of wins increases by one
  118.         }
  119.         System.out.println("\nScoreboard: \nWins: " + won + "\nLoses: " + lost + "\n"); //displays the scoreboard to the user
  120.        
  121.         String answer; //creates a variable for the user's answer to whether or not they want to play again
  122.  
  123.         System.out.println("Would you like to play again, yes or no?\n");
  124.         Scanner input = new Scanner(System.in); //initializes scanner
  125.         answer = input.nextLine(); //sets the user's answer to the variable
  126.         //creates an if statement to figure out whether or not to repeat the method Game
  127.         if(answer.equalsIgnoreCase("yes")){ //condition: if the user's answer is yes
  128.             Game.Game(); //the method Game will repeat
  129.         }else{ //if not, then the user doesnt want to play again and quits the program
  130.             System.out.println("Thanks for playing!");
  131.         } //ends if statement
  132.     } //ends Game method
  133. } //ends class HangMan
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement