-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
200 lines (147 loc) · 4 KB
/
main.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
194
195
196
197
198
199
200
package main
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/Dannflower/godle/logic"
"github.com/fatih/color"
)
var scanner *bufio.Scanner
func init() {
scanner = bufio.NewScanner(os.Stdin)
}
func main() {
printTitle()
printMenu()
handleMenuInput()
}
func printTitle() {
title := " _____ _ _ \n" +
" / ____| | | | \n" +
"| | __ ___ __| | | ___ \n" +
"| | |_ |/ _ \\ / _` | |/ _ \\ \n" +
"| |__| | (_) | (_| | | __/ \n" +
" \\_____|\\___/ \\__,_|_|\\___| \n"
fmt.Println(title)
}
func printMenu() {
fmt.Println("Options\t\tKey")
fmt.Println("-------\t\t---")
fmt.Println("Play\t\t p")
fmt.Println("Rules\t\t r")
fmt.Println("Quit\t\t q")
fmt.Println()
}
// Handles menu option selections
func handleMenuInput() {
for {
fmt.Print("Command: ")
scanner.Scan()
switch scanner.Text() {
case "p":
// Start new game
play()
printMenu()
case "r":
// Display rules, loop back to start of input
printRules()
printMenu()
case "q":
fmt.Println("Thanks for playing!")
os.Exit(0)
default:
fmt.Println("Invalid option.")
// loop back to start of input
}
}
}
func printRules() {
fmt.Println("Attempt to guess a randomly selected 5-letter word.")
fmt.Printf("You get %v guesses to get the right word.\n", logic.MaxGuesses)
fmt.Println("After guessing your guess will be displayed with color coding indicating the following:")
color.HiBlack("Gray - The letter is not in the word.")
color.Yellow("Yellow - The letter is in the word but is in the wrong position.")
color.Green("Green - The letter is in the word and in the right position.")
fmt.Println("If all guesses are exhausted, the answer will be revealed. Good luck word nerd!")
fmt.Println("Hit enter to return to the menu.")
scanner.Scan()
}
func handleWin(guesses []string) {
fmt.Println("You got it!")
fmt.Printf("Guesses: %v/%v\n", len(guesses), logic.MaxGuesses)
fmt.Println("Hit enter to return to the menu.")
scanner.Scan()
}
// Start the core game loop.
func play() {
logic.NewGame()
fmt.Println("Guess the word!")
for len(logic.Guesses) < logic.MaxGuesses {
fmt.Print("Guess: ")
scanner.Scan()
guess := scanner.Text()
err := logic.MakeGuess(guess)
if err != nil {
fmt.Printf("Invalid guess: %v.\n", err)
} else {
printGuessResult()
printAvailableLetters(logic.UsedLetters)
// Player has won!
if logic.HasWon(guess) {
handleWin(logic.Guesses)
return
}
}
}
fmt.Printf("Nice try! The word was '%s.'\n", logic.Answer)
fmt.Println("Hit enter to return to the menu.")
scanner.Scan()
}
// Prints the results of the last guess and all previous guesses
// with runes color coded depending on whether they are in the word,
// not in the word, or in the word but the wrong location.
func printGuessResult() {
for i, guess := range logic.Guesses {
capGuess := strings.ToUpper(guess)
colorResult := ""
for j, r := range capGuess {
colorResult += addHintColor(string(r), logic.Results[i][j])
}
fmt.Println(colorResult)
}
}
// Prints out the complete list of letters with any
// used in previous guesses printed in gray.
func printAvailableLetters(usedLetters map[rune]int) {
letters := []rune{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
line := ""
for i, letter := range letters {
colorLetter := string(letter)
// Add hint color if the letter has been used
if _, ok := usedLetters[letter]; ok {
colorLetter = addHintColor(colorLetter, usedLetters[letter])
}
line += colorLetter + " "
if i == 12 || i == 25 {
fmt.Println(line)
line = ""
}
}
}
// Returns the ANSI coded version of the string
// colored appropriately for the given hint.
func addHintColor(str string, hint int) string {
switch hint {
case logic.NotInWord:
return color.HiBlackString(str)
case logic.WrongPosition:
return color.YellowString(str)
case logic.CorrectPosition:
return color.GreenString(str)
default:
return str
}
}