Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- /**
- * A simple game of Hangman, chooses a random word, 6 guesses are allowed, the user has the option of
- * playing again, and keeps track of how many games they have won.
- *
- * Laura Andre and Kevin Bolger
- * 12/5/11
- */
- public class HangMan
- {
- // Don't declare static variables. You aren't calling them from a static method, so just get rid of the "static" modifier.
- 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
- 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
- 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
- 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
- // should not introduce Hangman like this. Put it down near line 33.
- public static HangMan Game = new HangMan(); //creates an public object to the method Game, static becuase it is used in both methods
- //main method
- public static void main(String [] args){
- //initial text displayed to user
- System.out.println("Welcome to the game of Hangman!");
- System.out.println("You will have 6 chances to guess what the word is.");
- Game.Game(); //calls on the method Game
- }
- //Game method
- public void Game(){
- // (as in here)
- word = null; //no initial value for the String word
- Random rand = new Random(); //initializes random number calculator
- // 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"
- int wordNum = rand.nextInt(19); //sets a random number value to the variable wordNum
- //creates a switch statement that assigns the numbers 0 to 20 to the String word, each with a different secret word
- switch(wordNum){
- case 0: word = "table"; break;
- case 1: word = "computer"; break;
- case 2: word = "house"; break;
- case 3: word = "desk"; break;
- case 4: word = "daring"; break;
- case 5: word = "chair"; break;
- case 6: word = "lamp"; break;
- case 7: word = "phone"; break;
- case 8: word = "chipotle"; break;
- case 9: word = "pencil"; break;
- case 10: word = "raise"; break;
- case 11: word = "upwards"; break;
- case 12: word = "movie"; break;
- case 13: word = "fresh"; break;
- case 14: word = "yield"; break;
- case 15: word = "savor"; break;
- case 16: word = "drive"; break;
- case 17: word = "brick"; break;
- case 18: word = "orange"; break;
- case 19: word = "great"; break;
- }
- 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
- wordLength = word.length(); //the variable word length is equal to the specific number of characters in whichever word is randomly chosen
- //for loop that assigns an underscore to indicate each of the characters in the randomly chosen word
- for(int i = 0; i < wordLength; i++){
- userWord.append('_');
- userWord.append(' ');
- // it is perfectly valid to say userWord.append("_ "), but your way is equivalent. It is a choice of style.
- }
- // Just show the blank spaces using System.out.println(userWord);
- System.out.println("Your word is " + wordLength + " letters long. \n"); //tells the user the length of the randomly chosen word
- int maxTries = 6; //sets the value for the variable maxTries, the user gets six guesses to guess the word
- StringBuilder guessedLetters = new StringBuilder(); //builds the String that will show the user the letters they have guessed
- int correct = 0; //sets the initial value to zero for the variable that will indicate how many letters the user has guessed correctly
- String guessLetter; //variable for the letter that the user inputs as a guess
- int right = 0; //sets the initial value to zero
- //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
- while(maxTries > 0 && correct<wordLength){
- System.out.println("The letters that you have guessed are: " + guessedLetters); //displays which letters the user has already guessed
- System.out.print("What is your guess? "); //asks the user for their guess
- Scanner input = new Scanner(System.in); //initializes the scanner
- guessLetter = input.nextLine(); //sets the user's guess to the String guessLetter
- System.out.print("\n");
- guessedLetters.append(guessLetter + " "); //appends the String guessedLetters to add the user's last guess to the list of guessed letters
- right = 0; //resets initial value to zero inside of while loop
- //for loop repeats while i is less than the number of letters in the secret word
- for(int i = 0; i < wordLength; i++){
- //if statement for when the user guesses the correct letter
- if(guessLetter.charAt(0) == word.charAt(i))
- {
- userWord.setCharAt(2*i, guessLetter.charAt(0)); //sets the correct guessed letter to the correct spot in the userWord that the user is building
- correct++; //increases value by one
- right++; //increases value by one
- System.out.println("Correct!" + "\n"); //tells user that their guess was correct
- System.out.println(userWord + "\n"); //prints out how much of the userWord the user has built so far
- } //ends if statement
- } //ends for loop
- //if statement for when the user's guess is incorrect
- if(right == 0){ //the right guess will still be zero because it was an incorrect guess
- maxTries--; //decreases the number of guesses the user has left by one
- 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
- System.out.println(userWord + "\n"); //prints out how much of the userWord the user has built so far
- } //ends if statement
- } //ends while loop
- System.out.println(userWord + "\n"); //prints out how much of the userWord the user has built so far
- System.out.println("The word was: " + word); //tells the user what the secret word was that they were trying to guess
- //creates an if statement that will keep track of the user's score, how many games they have won and lost
- if(maxTries == 0){ //condition: when the user runs out of guesses
- lost++; //they lost that game, so increases the number of loses by one
- }else{ //if they have guesses left, that means that they guessed the word correctly
- won++; //meaning they won the game, so number of wins increases by one
- }
- System.out.println("\nScoreboard: \nWins: " + won + "\nLoses: " + lost + "\n"); //displays the scoreboard to the user
- String answer; //creates a variable for the user's answer to whether or not they want to play again
- System.out.println("Would you like to play again, yes or no?\n");
- Scanner input = new Scanner(System.in); //initializes scanner
- answer = input.nextLine(); //sets the user's answer to the variable
- //creates an if statement to figure out whether or not to repeat the method Game
- if(answer.equalsIgnoreCase("yes")){ //condition: if the user's answer is yes
- Game.Game(); //the method Game will repeat
- }else{ //if not, then the user doesnt want to play again and quits the program
- System.out.println("Thanks for playing!");
- // But what if the user entered "OMG DOGS EVERYWHERE!!!11!11!111one11"? Just a precaution.
- } //ends if statement
- } //ends Game method
- } //ends class HangMan
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement