-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboggle.coffee
205 lines (186 loc) · 5.21 KB
/
boggle.coffee
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
Util =
build_table_from_2d_cell_array: (array_2d) ->
table = $("<table border=1>")
for row in array_2d
tr = $("<tr>")
for td in row
tr.append(td)
table.append(tr)
table
shuffle_array: (array) ->
_.sortBy(array, Math.random)
random_char: (s) ->
i = Math.floor(Math.random() * s.length)
s.charAt(i)
class DiceShaker
shake: (num_squares, letter_dice) ->
throw "unexpected number of dice" if num_squares != letter_dice.length
numbers = [0...num_squares]
dice = Util.shuffle_array(numbers)
_.map dice, (die) ->
letter = Util.random_char letter_dice[die]
letter = 'QU' if letter == 'Q'
letter
class Board
constructor: (@display, @size, letter_dice) ->
@num_squares = @size * @size
@dice = new DiceShaker().shake(@num_squares, letter_dice)
for die, i in @dice
display.place_die(i, die)
get_letter: (i) -> @dice[i]
for_all_squares: (f) ->
for square in [0...@num_squares] by 1
f(square)
is_adjacent: (s1, s2) ->
return false if s1 == s2
r1 = Math.floor(s1 / @size)
c1 = s1 % @size
r2 = Math.floor(s2 / @size)
c2 = s2 % @size
return (Math.abs(r1-r2) <= 1) && (Math.abs(c1-c2) <= 1)
Display = (size) ->
$("#boggle").append("<p>Click on letters to make words.</p>")
do ->
table_data = ->
_.map [0...size], (row) ->
_.map [0...size], (col) ->
n = row * size + col
$("<td>").attr("id", "pos#{n}").css("height", "30px").css("width", "30px")
table = Util.build_table_from_2d_cell_array(table_data())
$("#boggle").append(table)
word_entry_span = $("<span>")
$("#boggle").append(word_entry_span)
scratchpad = $("<pre>")
$("#boggle").append("<h2>Answers</h2>")
$("#boggle").append(scratchpad)
self =
square: (i) ->
$("#pos#{i}")
index: (element) ->
id = $(element).attr("id")
parseInt(id.match(/\d+/)[0])
place_die: (i, value) ->
self.square(i).html(value)
on_click_square: (callback) ->
handler = ->
callback(self.index(this))
$("td").click handler
color: (pos, color) ->
self.square(pos).css("background", color)
word_entry: ->
word_entry_span.html('')
field = $("<pre>")
word_entry_span.append(field)
append_hidden_button = (label) ->
button = $("<input type='button'>")
button.attr("value", label)
word_entry_span.append(button)
button.hide()
button
back_button = append_hidden_button("BACK")
save_button = append_hidden_button("SAVE")
self =
field:
set: (text) -> field.html(text)
back_button:
hide: -> back_button.hide()
show: -> back_button.show()
on_click: (f) -> back_button.click(f)
save_button:
hide: -> save_button.hide()
show: -> save_button.show()
on_click: (f) -> save_button.click(f)
scratchpad:
add_word: (s) ->
scratchpad.append(s + "\n")
class Word_builder
constructor: (@board) ->
@square_indexes = []
add: (i) ->
@square_indexes.push(i)
backspace: ->
@square_indexes.pop()
text: ->
_.map(@square_indexes, (i) =>
@board.get_letter(i)
).join('')
already_used: (i) ->
i in @square_indexes
in_reach: (new_i) ->
return true if @square_indexes.length == 0
last_square = @last_square_selected()
@board.is_adjacent(last_square, new_i)
validate_new_letter: (new_i) ->
throw "already used" if @already_used(new_i)
throw "out of reach" if !@in_reach(new_i)
last_square_selected: () ->
return undefined if @square_indexes.length == 0
@square_indexes[@square_indexes.length - 1]
redraw_board: (display) ->
color = (i) =>
return "lightgreen" if i == @last_square_selected()
return "lightblue" if @already_used(i)
return "white" if @in_reach(i)
return "#DDD"
@board.for_all_squares (i) ->
display.color(i, color(i))
class Word_entry
constructor: (@board, @display) ->
@word = new Word_builder(board)
{@field, @back_button, @save_button} = @display.word_entry()
@word.redraw_board(@display)
@display.on_click_square (i) => @on_click_letter(i)
@back_button.on_click => @backspace()
@save_button.on_click => @save()
redraw: ->
@word.redraw_board(@display)
text = @word.text()
@field.set(text)
if text.length > 0
@back_button.show()
@save_button.show()
else
@back_button.hide()
@save_button.hide()
on_click_letter: (i) ->
try
@word.validate_new_letter(i)
catch error
alert error
return
@word.add(i)
@redraw()
backspace: ->
@word.backspace()
@redraw()
save: ->
@display.scratchpad.add_word(@word.text())
@word = new Word_builder(@board)
@redraw()
LetterDice =
[
"AAEEGN",
"ELRTTY",
"AOOTTW",
"ABBJOO",
"EHRTVW",
"CIMOTU",
"DISTTY",
"EIOSST",
"DELRVY",
"ACHOPS",
"HIMNQU",
"EEINSU",
"EEGHNW",
"AFFKPS",
"HLNNRZ",
"DEILRX",
]
boggle = ->
size = 4
do ->
display = Display(size)
board = new Board(display, size, LetterDice)
new Word_entry(board, display)
jQuery(document).ready ->
boggle()