-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathguess.go
83 lines (74 loc) · 1.47 KB
/
guess.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
package main
import (
"errors"
"fmt"
"strings"
)
type letterGuess struct {
l string
c color
}
type fullGuess struct {
lg []letterGuess
word string
}
type guessList []fullGuess
type color string
const (
Grey color = "\033[37m"
Yellow color = "\033[33m"
Green color = "\033[32m"
)
func validateGuess(d dict, g string, gl guessList) (string, error) {
if len(g) != 5 {
return "The guess needs to have 5 letters.", errors.New("guess_size_error")
}
if !d.isWordInDictionary(g) {
return "That is not a word in our dictionary.", errors.New("doesnt_exist_error")
}
if gl.wordInGuessList(g) {
return "You have already guessed that word.", errors.New("repeated_guess")
}
return "", nil
}
func checkGuess(a string, g string) (fullGuess, bool) {
gResult := []letterGuess{}
guessed := false
for idx, letterRune := range g {
var color color
l := string(letterRune)
la := string(a[idx])
if l == la {
color = Green
} else if strings.Contains(a, l) {
color = Yellow
} else {
color = Grey
}
newL := letterGuess{l, color}
gResult = append(gResult, newL)
}
if g == a {
guessed = true
}
return fullGuess{gResult, g}, guessed
}
func (fg fullGuess) printGuess() {
for _, l := range fg.lg {
fmt.Print(l.c, l.l)
}
fmt.Println(Grey)
}
func (gl guessList) printPastGuesses() {
for _, g := range gl {
g.printGuess()
}
}
func (gl guessList) wordInGuessList(w string) bool {
for _, g := range gl {
if g.word == w {
return true
}
}
return false
}