-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
164 lines (132 loc) · 3.82 KB
/
main.go
File metadata and controls
164 lines (132 loc) · 3.82 KB
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
package main
import (
"cyancoding/go-humanize"
"fmt"
"io/ioutil"
"strings"
"sync"
"github.com/slok/gospinner"
)
// Colors
const ColorReset = "\033[0m"
const ColorRed = "\033[31m"
const ColorGreen = "\033[32m"
const ColorYellow = "\033[33m"
const ColorBlue = "\033[34m"
const ColorPurple = "\033[35m"
const ColorCyan = "\033[36m"
// oxford.txt contains words you can find in a dictionary (valid boggle words)
// all-english.txt contains every english word (might not be in a dictionary)
var wordsFile string = "oxford.txt"
var wordsFound int
var searches int64
var wordsChecked int64
var words []string // Word list (about 436k)
var wordsFoundList []string
var s *gospinner.Spinner
var percentDone int
var width int = 4 // 4 or 5 for 4x4/5x5
func ReadWordsFile() []string {
byteData, err := ioutil.ReadFile(wordsFile)
if err != nil {
fmt.Println(err)
}
text := string(byteData)
words := strings.Fields(text)
return words
}
func main() {
fmt.Println(ColorPurple + "Welcome to the boggle solver/computer version!")
fmt.Println("Please choose one of the following")
fmt.Println("Solo mode (1), computer solve (2), input board (3)")
fmt.Println()
fmt.Print(ColorYellow + "Action > ")
var action int = 2
fmt.Scanln(&action)
fmt.Println()
fmt.Println(ColorPurple + "Please pick a board size")
fmt.Print(ColorYellow + "(1) 4x4, (2) 5x5 > ")
var boardSize int = 1
fmt.Scanln(&boardSize)
if boardSize == 2 {
width = 5
} else if boardSize != 1 { // Invalid value
fmt.Println(ColorRed + "Invalid response! Defaulting to 4x4")
width = 4
}
fmt.Println()
fmt.Println(ColorPurple + "Please pick your dictionary")
fmt.Println("(1) Only dictionary words, (2) all English words")
fmt.Print(ColorYellow + "Dictionary > ")
var dictionary int = 1
fmt.Scanln(&dictionary)
fmt.Println()
if dictionary == 2 {
wordsFile = "all-english.txt"
} else if dictionary != 1 {
fmt.Println(ColorRed + "Invalid response! Defaulting to oxford dictionary")
}
fmt.Println()
fmt.Println(ColorPurple + "Please pick an option")
fmt.Println("(1) Use repeat letters, (2) no repeat letters")
fmt.Print(ColorYellow + "Option > ")
var option int = 0
var useRepeats bool = true
fmt.Scanln(&option)
fmt.Println()
if option == 2 {
useRepeats = false
} else if option != 1 {
fmt.Println(ColorRed + "Invalid response! Defaulting to yes for repeat letters")
}
var group sync.WaitGroup
var dice [5][5]diceValue
group.Add(1)
go func() {
words = ReadWordsFile()
if action != 3 {
dice = RollDice()
}
defer group.Done()
}()
if action < 1 || action > 3 { // Action is not valid
action = 2
fmt.Println(ColorRed + "\nYou did not enter a valid number. Defaulting to 2")
}
if action == 3 {
dice = ManuallySetDice()
} else if action == 1 {
fmt.Println(ColorRed + "This feature is not ready yet!")
}
group.Wait() // Make sure the word compilation has finished
wordsFoundList = make([]string, 0)
fmt.Println(ColorPurple + "Below is the board")
fmt.Println()
PrintDice(dice)
fmt.Println()
s, _ = gospinner.NewSpinner(gospinner.Dots2)
s.Start(ColorCyan + "Finding matches (0% • 0)...")
// This is the actual for loop that runs the program
for i := 0; i < width; i++ {
for j := 0; j < width; j++ {
FindNearby(dice, i, j, "", useRepeats, nil)
percentDone += 100 / (width * width)
}
}
s.SetMessage(ColorGreen + "Found all matches!")
s.Succeed()
// Reports all of the words when we're done
for _, a := range wordsFoundList {
fmt.Println(a)
}
// Count the letters
var letterCount int64
for _, a := range wordsFoundList {
letterCount += int64(len(a))
}
// Give the user statistics
fmt.Println(ColorPurple+"Ran", humanize.Comma(searches), "times and tested",
humanize.Comma(wordsChecked), "words")
fmt.Println("That's", humanize.Comma(int64(len(wordsFoundList))), "words and",
humanize.Comma(letterCount), "points!")
}