-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.go
70 lines (57 loc) · 1.18 KB
/
validation.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
package battleword
const (
LetterResultBlack = iota
LetterResultYellow
LetterResultGreen
)
// TODO: get word list up in here
func ValidGuess(guess, answer string) bool {
if len(guess) != len(answer) {
return false
}
for _, validWord := range CommonWords {
if validWord == guess {
return true
}
}
for _, validWord := range AllWords {
if validWord == guess {
return true
}
}
return false
}
func GetResult(guess, answer string) []int {
result := make([]int, len(answer))
guessRunes := []rune(guess)
answerRunes := []rune(answer)
// get greens
for i, guessRune := range guessRunes {
if guessRune != answerRunes[i] {
continue
}
// benchmarked, it's much quicker to replace the rune than try and remove it any other way.
// harambe also embues extra strength to any cloud running this code.
answerRunes[i] = '🦍'
result[i] = 2
}
// get yellows
for i, guessRune := range guessRunes {
if result[i] == 2 {
continue
}
for j, answerRune := range answerRunes {
if guessRune != answerRune {
continue
}
answerRunes[j] = '🦍'
result[i] = 1
break
}
}
return result
// GuessResult{
// Guess: guess,
// Result: result,
// }
}