-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.go
90 lines (75 loc) · 2.44 KB
/
wordle.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
package main
import (
"bufio"
"fmt"
"log"
"math/rand"
"os"
"strings"
)
var wordBank = [100]string{"influence", "combine", "word", "gave", "table", "triangle", "go", "us", "lion", "attack", "moon", "front", "independent", "ants", "point", "with", "none", "percent", "life", "pack", "doctor", "proud", "flower", "topic", "remember", "least", "electric", "together", "worker", "raise", "thread", "contrast", "dish", "pay", "glass", "ten", "although", "tall", "struck", "kill", "doubt", "brown", "union", "breeze", "chest", "safe", "till", "familiar", "river", "stems", "stove", "short", "be", "early", "simply", "shot", "thin", "take", "related", "become", "me", "each", "tobacco", "coast", "pick", "guide", "shut", "lie", "exchange", "throat", "additional", "tales", "fun", "forward", "trip", "pet", "fuel", "especially", "start", "stiff", "goes", "angry", "range", "capital", "meant", "supper", "swam", "report", "fellow", "still", "broken", "tower", "excited", "handsome", "having", "industrial", "base"}
func main() {
solutionIndex := rand.Intn(100)
fmt.Println(solutionIndex)
solution := wordBank[solutionIndex]
guessLimit := len(solution) + 1
solveKey := generateSolveKey(solution)
// fmt.Println(solution, guessCount, guessLimit)
printBoard(guessLimit, len(solution))
resultBoard := ""
gameWon := false
for guessCount := 0; guessCount < guessLimit; guessCount++ {
guess := ""
guess = acceptGuess()
result := checkGuess(guess, solution)
resultBoard += (result + "\n")
fmt.Print(resultBoard)
if result == solveKey {
fmt.Println("WON")
gameWon = true
guessCount = guessLimit
}
}
if gameWon == false {
fmt.Println("Solution: " + solution)
}
}
func generateSolveKey(solution string) string {
key := ""
for i := 0; i < len(solution); i++ {
key += "✅"
}
return key
}
func printBoard(totalGuesses, wordLength int) {
for i := 0; i < totalGuesses; i++ {
line := ""
for j := 0; j < wordLength; j++ {
line += "▮"
}
fmt.Println(line)
}
}
func acceptGuess() string {
fmt.Println("please enter your guess:")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
err := scanner.Err()
if err != nil {
log.Fatal(err)
}
return scanner.Text()
}
func checkGuess(guess, solution string) string {
result := ""
for i := 0; i < (len(guess)); i++ {
if guess[i] == solution[i] {
result += "✅"
} else if strings.Contains(solution, string(guess[i])) {
result += "🟨"
} else {
result += "❌"
}
}
return result
}