Advertisement
Vermiculus

Hangman (looked over)

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