-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjfb-hangman.go
193 lines (168 loc) · 4.9 KB
/
jfb-hangman.go
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"bufio"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/exec"
"strings"
)
func gallows(parts int) {
fmt.Println(" ----------+")
fmt.Print( " ")
if parts > 0 {
fmt.Print("|")
} else {
fmt.Print(" ")
}
fmt.Println( " |")
fmt.Print( " ")
if parts > 1 {
fmt.Print("O")
} else {
fmt.Print(" ")
}
fmt.Println(" |")
fmt.Print( " ")
if parts > 3 {
fmt.Print("\\")
} else {
fmt.Print(" ")
}
if parts > 2 {
fmt.Print("|")
} else {
fmt.Print(" ")
}
if parts > 4 {
fmt.Print("/")
} else {
fmt.Print(" ")
}
fmt.Println( " |")
fmt.Print( " ")
if parts > 5 {
fmt.Print("/")
} else {
fmt.Print(" ")
}
fmt.Print(" ")
if parts > 6 {
fmt.Print("\\")
} else {
fmt.Print(" ")
}
fmt.Println( " |")
fmt.Println(" |")
fmt.Println(" |")
fmt.Println(" |")
fmt.Println(" |")
fmt.Println("-------------------------------------")
}
func get_word()(string) {
resp, err := http.Get("http://setgetgo.com/randomword/get.php")
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return string(body)
}
func main() {
var word, word_show, guessed, guess, message string
message = ""
// get some screen-drawing characters the kludgy way
// instead of using some terminal library
cmd := exec.Command("tput", "home")
home, err := cmd.Output()
if err != nil {
panic(err)
}
cursor_home := string(home)
cmd = exec.Command("tput", "do")
do, err := cmd.Output()
if err != nil {
panic(err)
}
cursor_down := string(do)
cmd = exec.Command("clear")
clear, err := cmd.Output()
if err != nil {
panic(err)
}
screen_clear := string(clear)
// pick a word and generate same-length string of blanks (underscores)
word = get_word()
for i := 0; i < len(word); i++ {
word_show += "_"
}
on_gallows := 0
for {
fmt.Print(screen_clear)
gallows(on_gallows)
// exit the loop after drawing the gallows if the player has lost
if on_gallows > 6 {
break
}
// print the blanks plus correctly-guessed letters
fmt.Print(cursor_home + "Word:" + cursor_down + word_show)
// move the cursor below the gallows
for i := 0; i < 10; i++ {
fmt.Print(cursor_down)
}
// exit the loop after printing the word if the player has won
if word_show == word {
break
}
// print and clear any error/status message
if len(message) > 0 {
fmt.Println("** " + message)
message = ""
}
// print previously-guessed letters for player's reference
if len(guessed) > 0 {
fmt.Println("Previous guesses: " + guessed)
}
// prompt and read the guess, converting to lower-case
fmt.Print("Guess a letter: ")
bio := bufio.NewReader(os.Stdin)
guess_in, hasMoreInLine, err := bio.ReadLine()
if err != nil {
fmt.Println(guess, hasMoreInLine, err, strings.Index(word, guess))
panic(err)
}
guess = strings.ToLower(string(guess_in))
if len(guess_in) != 1 || guess[0] < 'a' || guess[0] > 'z' {
message = "Please guess exactly one letter."
continue
}
if strings.Index(guessed, guess) >= 0 {
message = "You already guessed '" + guess + "'."
continue
}
// add guessed letter to list of guesses
guessed += string(guess)
if strings.Index(strings.ToLower(word), guess) == -1 {
// guessed letter not found in secret word;
// increase counter of bad guesses
on_gallows++
} else {
// guessed letter was found in the secret word;
// substitute it into the string of blanks/revealed letters
word_show_bytes := []byte(word_show)
for i := 0; i < len(word); i++ {
if strings.ToLower(word)[i] == guess[0] {
word_show_bytes[i] = word[i]
}
}
word_show = string(word_show_bytes)
}
}
fmt.Print("\nYou")
if word_show == word {
fmt.Println(" win!")
} else {
fmt.Println("'re zestfully dead! The word was \"" + word + "\".")
}
}