-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
239 lines (192 loc) · 4.51 KB
/
utils.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"fmt"
"go_boggle_server/trie"
"math/rand"
"sync"
"time"
)
func startGame(room *Room) {
roomName := room.RoomName
broadcastStart(roomName)
}
func findAllValidWords(constGrid [][]string, trie *trie.Trie) []string {
words := []string{}
for i := 0; i < NUM; i++ {
for j := 0; j < NUM; j++ {
newWords := dfs(i, j, constGrid, trie)
for _, word := range newWords {
if !contains(words, word) {
words = append(words, word)
}
}
}
}
return words
}
func initGame(roomName string, trie *trie.Trie, random bool) {
constGrid := make([][]string, NUM)
allCharacters := []string{}
for i := 0; i < NUM; i++ {
constGrid[i] = []string{}
}
chosenBoggle := BOGGLE_1992
if rand.Intn(2) == 0 {
chosenBoggle = BOGGLE_1983
}
for i := 0; i < NUM*NUM; i++ {
randIndex := rand.Intn(6)
char := chosenBoggle[i][randIndex : randIndex+1]
if char == "Q" {
char += "u"
}
constGrid[i/NUM] = append(constGrid[i/NUM], char)
allCharacters = append(allCharacters, char)
}
allValidWords := findAllValidWords(constGrid, trie)
totalScore := calculateTotalPossibleScore(allValidWords)
clientRoomsLock.Lock()
defer clientRoomsLock.Unlock()
room := &Room{
AllCharacters: allCharacters,
AllValidWords: allValidWords,
TotalScore: totalScore,
Player1: 0,
Player2: 0,
Player1WS: nil,
Player2WS: nil,
Countdown: [2]int{3,0},
RoomLock: &sync.Mutex{},
RoomName: roomName,
Player1MissedTurns: 0,
Player2MissedTurns: 0,
}
clientRooms[roomName] = room
// if random, also add to the list
if(random) {
fmt.Println("added to random!")
randomRooms = append(randomRooms, room)
}
}
func dfs(i, j int, constGrid [][]string, trie *trie.Trie) []string {
s := Tile{i, j}
marked := make([][]bool, NUM)
for i := 0; i < NUM; i++ {
marked[i] = make([]bool, NUM)
}
return dfs2(s, constGrid[i][j], marked, constGrid, trie)
}
func dfs2(v Tile, prefix string, marked [][]bool, constGrid [][]string, commonTrie *trie.Trie) []string {
marked[v.I][v.J] = true
words := []string{}
if len(prefix) > 2 && commonTrie.ContainsWord(prefix) {
words = append(words, prefix)
}
for _, adj := range adj2(v.I, v.J) {
if !marked[adj.I][adj.J] {
newWord := prefix + constGrid[adj.I][adj.J]
if commonTrie.ContainsPrefix(newWord) {
newWords := dfs2(adj, newWord, marked, constGrid, commonTrie)
words = append(words, newWords...)
}
}
}
marked[v.I][v.J] = false
return words
}
func adj2(i, j int) []Tile {
adj := []Tile{}
if i > 0 {
adj = append(adj, Tile{i - 1, j})
if j > 0 {
adj = append(adj, Tile{i - 1, j - 1})
}
if j < NUM-1 {
adj = append(adj, Tile{i - 1, j + 1})
}
}
if i < NUM-1 {
adj = append(adj, Tile{i + 1, j})
if j > 0 {
adj = append(adj, Tile{i + 1, j - 1})
}
if j < NUM-1 {
adj = append(adj, Tile{i + 1, j + 1})
}
}
if j > 0 {
adj = append(adj, Tile{i, j - 1})
}
if j < NUM-1 {
adj = append(adj, Tile{i, j + 1})
}
return adj
}
func calculateTotalPossibleScore(allValidWords []string) int {
total := 0
for _, word := range allValidWords {
switch len(word) {
case 3, 4:
total++
case 5:
total += 2
case 6:
total += 3
case 7:
total += 5
default:
total += 11
}
}
return total
}
func makeID(length int) string {
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
rand.Seed(time.Now().UnixNano())
b := make([]byte, length)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}
func numberOfClients(room *Room) int {
num := 0
if room.Player1WS != nil {
num++
}
if room.Player2WS != nil {
num++
}
return num
}
// helper function to find if a slice contains a string
func contains(slice []string, item string) bool {
for _, s := range slice {
if s == item {
return true
}
}
return false
}
func findRoomIndex(rooms []*Room, name string) int {
for i, room := range rooms {
if room.RoomName == name {
return i
}
}
return -1 // return -1 if not found
}
// Function to remove a Room from the list by index
func removeRoom(rooms []*Room, index int) []*Room {
if index < 0 || index >= len(rooms) {
return rooms // return the original slice if index is out of bounds
}
return append(rooms[:index], rooms[index+1:]...)
}
func popFirstRoom(rooms []*Room) ([]*Room, *Room) {
if len(rooms) == 0 {
return rooms, nil // return the original slice and nil if it's empty
}
firstRoom := rooms[0]
return rooms[1:], firstRoom
}