-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhangman.groovy
85 lines (64 loc) · 2.25 KB
/
hangman.groovy
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
// class Hangman{
// private String theWord;
// private String wordArr;
// private String wordDisplay;
// Hangman(String theWord){
// this.theWord = theWord;
// String[] this.wordArr = theWord.split('');
// String this.wordDisplay = [];
// for (int i = 0; i < wordArr.size(); i++){
// wordDisplay.add('-')
// }
// }
// def getTheWord(){
// return this.theWord;
// }
// def display(){
// println(this.wordDisplay);
// }
// def guess(){
// println("Guess a letter.");
// String guess = System.in.newReader().readLine() as String;
// }
// }
public static void main(String[] args){
println("Pass the computer to Player 1 and enter a word for Player 2 to guess.")
String word = System.in.newReader().readLine();
String[] wordArr = word.toLowerCase().split('');
def hiddenWord = [];
for (int i = 0; i < wordArr.size(); i++){
hiddenWord.add('-');
}
def lettersGuessed = [];
println("Now pass the computer to Player 2. Let the Game begin!")
int guessesLeft = 9;
while(hiddenWord.contains('-') && guessesLeft > 0){
println("Word: " + hiddenWord);
println("Guess a letter");
String userGuess = System.in.newReader().readLine() as String;
if(!lettersGuessed.contains(userGuess)){
if(wordArr.contains(userGuess)){
println("'${userGuess}' is in the word!");
for(int i = 0; i < wordArr.size(); i++){
if(wordArr[i] == userGuess){
hiddenWord[i] = userGuess;
}
}
lettersGuessed.add(userGuess);
println("Letters Guessed: " + lettersGuessed);
} else {
lettersGuessed.add(userGuess);
guessesLeft--;
println("'${userGuess}' is not in the word. You have ${guessesLeft} guesses left")
println("Letters Guessed: " + lettersGuessed);
}
} else {
println("You already guessed that. Try again!")
}
}
if(guessesLeft == 0){
println("You Lose!");
} else {
println("You win!");
}
}