-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
266 lines (247 loc) · 7.36 KB
/
script.js
File metadata and controls
266 lines (247 loc) · 7.36 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// blocks ======================================================================
// must have a length that is a square number
var standard = {
id: "standard",
title: "Gobble",
blocks: [
['A','A','E','E','G','N'],
['A','C','H','O','P','S'],
['E','E','G','H','N','W'],
['E','H','R','T','V','W'],
['R','I','L','X','E','D'],
['O','S','S','E','I','T'],
['A','P','F','S','K','F'],
['T','A','O','O','W','T'],
['T','E','L','R','Y','T'],
['O','M','C','I','U','T'],
['D','Y','V','E','R','L'],
['H','L','R','N','Z','N'],
['Qu','M','N','U','I','H'],
['B','O','O','A','J','B'],
['I','S','D','T','Y','T'],
['E','E','I','N','S','U']
],
timer_length: 181,
scoring: ""
}
// from deluxe - see https://boardgamegeek.com/article/4975223#4975223
var big = {
id: "big",
title: "Big Gobble",
blocks: [
['A','A','A','F','R','S'],
['A','A','E','E','E','E'],
['A','A','F','I','R','S'],
['A','D','E','N','N','N'],
['A','E','E','E','E','M'],
['A','E','E','G','M','U'],
['A','E','G','M','N','N'],
['A','F','I','R','S','Y'],
['B','J','K','Qu','X','Z'],
['C','C','N','S','T','W'],
['C','E','I','I','L','T'],
['C','E','I','L','P','T'],
['C','E','I','P','S','T'],
['D','H','H','N','O','T'],
['D','H','H','L','O','R'],
['D','H','L','N','O','R'],
['D','D','L','N','O','R'],
['E','I','I','I','T','T'],
['E','M','O','T','T','T'],
['E','N','S','S','S','U'],
['F','I','P','R','S','Y'],
['G','O','R','R','V','W'],
['H','I','P','R','R','Y'],
['N','O','O','T','U','W'],
['O','O','O','T','T','U']
],
timer_length: 181,
scoring: ""
}
// ================================================================
// default to standard
var currentBlockSet = standard
var timer = {
interval: undefined,
remaining: standard.timer_length,
total: standard.timer_length,
isPaused: false
}
function changeBlockSet(blockSet) {
currentBlockSet = blockSet
timer.remaining = blockSet.timer_length
timer.total = blockSet.timer_length
$('header').html(blockSet.title)
// hide appropriate
if (currentBlockSet.id == 'big') {
hideElement($('#big'))
showElement($('#standard'))
}
if (currentBlockSet.id == 'standard') {
hideElement($('#standard'))
showElement($('#big'))
}
createBoard(blockSet)
$('.letter').addClass('white')
$('.underline').addClass('underlineRemoved')
$('.underlineRemoved').removeClass('underline')
$('#timer').html(blockSet.timer_length)
}
// using fisher yates shuffle: https://bost.ocks.org/mike/shuffle/
var shuffle = function(array) {
var currentIndex = array.length, temporaryValue, randomIndex;
// while there remain elements to shuffle
while (currentIndex) {
// pick a remaining element
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// and swap it with the current element
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function createBoard(blockSet) {
var $board = $('#board')
var orientations = ['up','right','left','down']
// delete existing letters if they exist
$('.letter-container').remove()
board = []
function createRow(array) {
var $newRow = $('<div></div>').addClass('row')
var newRow = []
for (var i = 0; i < array.length; i++) {
// randomly pick a letter from the current block i
var newLetter = array[i][Math.floor(Math.random() * array[i].length)]
var $newDiv = $('<div class="letter-container"><div class="letter">' + newLetter + '</div></div>')
.addClass(orientations[Math.floor(Math.random() * orientations.length)])
// underline certain letters
if (/[MWZ]/.test(newLetter)) {
$newDiv.addClass('underline')
}
$newRow.append($newDiv)
newRow.push(newLetter)
}
board.push(newRow)
return $newRow
}
blockSet.blocks = shuffle(blockSet.blocks)
for (var j = 0; j < Math.sqrt(blockSet.blocks.length); j++) {
$board.append(createRow(blockSet.blocks.slice(j * Math.sqrt(blockSet.blocks.length),(j + 1) * Math.sqrt(blockSet.blocks.length))))
}
}
function startTimer() {
// reset timer if game over
$('#timer').html('')
timer.interval = setInterval(function() {
if (!timer.isPaused) {
timer.remaining = timer.remaining - 1
}
if (timer.remaining > 10) {
$('#timer').html(timer.remaining)
} else if (timer.remaining > 0) {
$('#timer').html(timer.remaining).addClass('warning')
} else {
$('#timer').html('Game Over').removeClass('warning')
timer.remaining = timer.total
clearInterval(timer.interval)
hideElement($('#pause'))
hideElement($('#end'))
showElement($('#start'))
// show appropriate
if (currentBlockSet.id == 'big') {
showElement($('#standard'))
} else {
showElement($('#big'))
}
}
},1000)
}
function startGame() {
createBoard(currentBlockSet)
startTimer()
hideElement($('#start'))
hideElement($('#standard'))
hideElement($('#big'))
showElement($('#pause'))
showElement($('#end'))
}
function pauseGame() {
timer.isPaused = true
hideElement($('#pause'))
hideElement($('#end'))
showElement($('#unpause'))
$('.letter').addClass('white')
$('.underline').addClass('underlineRemoved')
$('.underlineRemoved').removeClass('underline')
}
function unpauseGame() {
timer.isPaused = false
hideElement($('#unpause'))
showElement($('#pause'))
showElement($('#end'))
$('.letter').removeClass('white')
$('.underlineRemoved').addClass('underline')
$('.underline').removeClass('underlineRemoved')
}
function endGame() {
timer.remaining = 0
}
function hideElement($element) {
$element.addClass('hidden')
}
function showElement($element) {
$element.removeClass('hidden')
}
// is word in board ============================================================
var board = []
// helper functions
function isLetter(letter,coord,board) {
return board[coord[0]][coord[1]] == letter
}
function sameCoordinate(coord1,coord2) {
return coord1[0] == coord2[0] && coord1[1] == coord2[1]
}
function getSurroundingCoordinates(coord,boardLength) {
var surroundingCoordinates = []
for (var i = Math.max(coord[0] -1,0); i < Math.min(coord[0] + 2,boardLength); i++) {
for (var j = Math.max(coord[1] -1,0); j < Math.min(coord[1] + 2,boardLength); j++) {
surroundingCoordinates.push(new Array(i,j))
}
}
return surroundingCoordinates.filter((e) => {
return !sameCoordinate(e,coord)
})
}
function getLetterCoordinates(letter,board) {
var letterCoordinates = []
for (var i = 0; i < board.length; i++) {
for (var j = 0; j < board.length; j++) {
if (board[i][j] == letter) {
letterCoordinates.push(new Array(i,j))
}
}
}
return letterCoordinates
}
// solver function
// TODO: refactor and finish
function testUserWord(word) {
word = word.split('')
var match = false
var matchCoordinates = []
for (var i = 0; i < word.length && matchCoordinates.length === 0; i++) {
// TODO: refactor and finish
}
// test every letter of the board against the first letter of the word
for (var i = 0; i < board.length && !match; i++) {
for (var j = 0; j < board[i].length && !match; j++) {
if (board[i][j] == word[0]) {
matchCoordinates.push([i,j])
// TODO: refactor and finish
}
}
}
return matchCoordinates
}