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