-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathWordGuess.java
More file actions
115 lines (96 loc) · 3.47 KB
/
WordGuess.java
File metadata and controls
115 lines (96 loc) · 3.47 KB
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
113
114
115
/**
* Write a program to guess a word by trying to guess the individual characters.
* The word to be guessed shall be provided using the command-line argument.
*
* Your program shall look like:
* > java WordGuess testing
* Key in one character or your guess word: t
* Trail 1: t__t___
* Key in one character or your guess word: g
* Trail 2: t__t__g
* Key in one character or your guess word: e
* Trail 3: te_t__g
* Key in one character or your guess word: testing
* Trail 4: Congratulation!
* You got in 4 trials
*
* Hints:
* 1. Set up a boolean array to indicate the positions of the word
* that have been guessed correctly.
* 2. Check the length of the input String to determine whether the player enters
* a single character or a guessed word. If the player enters a single character,
* check it against the word to be guessed, and update the boolean array that
* keeping the result so far.
* 3. Try retrieving the word to be guessed from a text file (or a dictionary) randomly.
*/
package javaexercises.difficult;
import java.util.Random;
import java.util.Scanner;
public class WordGuess {
private final String[] words = {
"testing", "hello", "world", "template", "java"
, "maining", "computer", "processor", "univercity"
, "boolean", "string", "integer", "character"
, "indicator", "controller", "model", "view"
};
private String secretWord;
private boolean[] secretWordMatches;
public static void main(String[] args)
{
WordGuess aWordGuess = new WordGuess();
aWordGuess.setSecretWord();
aWordGuess.runGame();
}
private void setSecretWord(String word) {
secretWord = word;
}
private void setSecretWord() {
Random rand = new Random();
secretWord = words[rand.nextInt(words.length-1)];
}
private String getSecretWord() {
return secretWord;
}
private String getTrialWordWithMatches()
{
StringBuilder str = new StringBuilder();
for (int i = 0; i < secretWord.length(); i++) {
str.append( (secretWordMatches[i] ? secretWord.charAt(i) : '_') );
}
return str.toString();
}
private void checkTrialWord(char ch)
{
for (int i = 0; i < secretWord.length(); i++)
{
if (secretWordMatches[i]) {
continue;
}
secretWordMatches[i] = secretWord.charAt(i) == ch;
}
}
private void runGame()
{
Scanner in = new Scanner(System.in);
secretWordMatches = new boolean[secretWord.length()];
int trials = 0;
while (true) {
System.out.print("Key in one character or your guess word: ");
String trialWord = (in.hasNext()) ? in.next() : "";
trials++;
if (trialWord.length() < 1) {
continue;
}
if (trialWord.length() == 1) {
checkTrialWord(trialWord.charAt(0));
trialWord = getTrialWordWithMatches();
System.out.printf("Trail %1$d: %2$s\n", trials, trialWord);
}
if (trialWord.equals(secretWord)) {
System.out.printf("Trail %d: Congratulation!\n", trials);
break;
}
}
System.out.printf("You got in %d trials\n", trials);
}
}