forked from HarryDulaney/intro-to-java-programming
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathExercise07_35.java
112 lines (103 loc) · 3.72 KB
/
Exercise07_35.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package ch_07;
import java.util.*;
/**
* ***7.35 (Game: hangman) Write a hangman game that randomly generates a word and
* prompts the user to guess one letter at a time, as shown in the sample run. Each
* letter in the word is displayed as an asterisk. When the user makes a correct
* guess, the actual letter is then displayed. When the user finishes a word, display
* the number of misses and ask the user whether to continue to play with another
* word. Declare an array to store words, as follows:
* // Add any words you wish in this array
* String[] words = {"write", "that", ...};
* (Guess) Enter a letter in word ******* > p
* (Guess) Enter a letter in word p****** > r
* (Guess) Enter a letter in word pr**r** > p
* p is already in the word
* (Guess) Enter a letter in word pr**r** > o
* (Guess) Enter a letter in word pro*r** > g
* (Guess) Enter a letter in word progr** > n
* n is not in the word
* (Guess) Enter a letter in word progr** > m
* (Guess) Enter a letter in word progr*m > a
* The word is program. You missed 1 time
* Do you want to guess another word? Enter y or n>
*
* @author Harry D.
*/
public class Exercise07_35 {
static int missed = 0;
final static String[] words = {"words", "bread", "signal", "time", "display", "friend", "computer",
"forget",
"java"};
static boolean wordLoop = true;
static boolean gameLoop = true;
final static String prompt = "(Guess) Enter a letter in word ";
static String currentWord;
static int[] guessed;
static String display;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (gameLoop) {
currentWord = getRandomWord();
handleNewWord();
do {
System.out.print(prompt + display + " >");
String guess = in.next();
handleGuess(guess.charAt(0));
wordLoop = validateWord();
if (!wordLoop) {
System.out.println("The word is " + currentWord + " you missed " + missed + " times.");
}
} while (wordLoop);
System.out.println("Do you want to guess another word? y or n >");
gameLoop = in.next().charAt(0) == 'y';
}
System.out.println("Thanks for playing hangman!");
in.close();
}
private static boolean validateWord() {
boolean gameNotOver = false;
for (int pos : guessed) {
if (pos == 0) {
gameNotOver = true;
break;
}
}
return gameNotOver;
}
static String getRandomWord() {
return words[(int) (Math.random() * words.length)];
}
static void handleAsterisk() {
char[] letters = currentWord.toCharArray();
for (int i = 0; i < letters.length; i++) {
if (guessed[i] == 0) {
letters[i] = '*';
}
}
display = String.valueOf(letters);
}
static void handleNewWord() {
guessed = new int[currentWord.length()];
char[] d = new char[currentWord.length()];
Arrays.fill(d, '*');
display = String.valueOf(d);
}
static void handleGuess(char guess) {
char[] letters = currentWord.toCharArray();
for (int i = 0; i < letters.length; i++) {
if (letters[i] == guess) {
if (guessed[i] != 0) {
System.out.println(guess + " is already in the word.");
} else {
guessed[i] = 1;
handleAsterisk();
return;
}
break;
}
}
missed++;
System.out.println(guess + " is not in the word.");
}
}