JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
89bc69503ccad5ab719cfaeae58fd7dd18994afc
[hexbog.git] / main.coffee
1 #   HexBog, a word game
2 #   Copyright (C) 2012 Jason Woofenden
3
4 #   This program is free software: you can redistribute it and/or modify
5 #   it under the terms of the GNU Affero General Public License as published by
6 #   the Free Software Foundation, either version 3 of the License, or
7 #   (at your option) any later version.
8
9 #   This program is distributed in the hope that it will be useful,
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #   GNU Affero General Public License for more details.
13
14 #   You should have received a copy of the GNU Affero General Public License
15 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 ##############################################
18 ##############    settings    ################
19 ##############################################
20
21 tile_radius = 26
22 tile_width = tile_radius * 2
23
24 fade_ms = 400
25 slide_ms = 2000
26
27 board_col_heights = [5, 6, 7, 8, 7, 6, 5]
28
29 # code and css will need adjusting if you change HP_MAX
30 HP_MAX = 10
31
32 ##############################################################
33 ##############    fix javascript some more    ################
34 ##############################################################
35
36 # so annoying that setTimeout has its arguments in the wrong order
37 timeout = (ms, callback) ->
38         setTimeout callback, ms
39
40 # warning: it's shalow (sub-elements are not cloned)
41 Array::clone = ->
42         return this.slice(0)
43
44 Array::sum = ->
45         ret = 0
46         ret += i for i in this
47         return ret
48
49 # ascending. All values must be Numbers
50 Array::num_sort = -> return this.sort((a, b) -> return a - b)
51
52 Array::last = ->
53         return this[this.length - 1]
54
55
56 ##############################################################
57 ##############    cookies (auto-save game)    ################
58 ##############################################################
59
60 set_cookie = (name, value, days) ->
61         date = new Date()
62         date.setTime date.getTime()+(days*24*60*60*1000)
63         cookie = "#{name}=#{value}; expires=#{date.toGMTString()}; path=/"
64         document.cookie = cookie
65 window.sc = set_cookie
66
67 get_cookie = (name) ->
68         key = name + '='
69         for c in document.cookie.split /; */
70                 if c.indexOf key is 0
71                         return c.substr key.length
72         return null
73
74 delete_cookie = (name) ->
75         set_cookie name, '', -1
76 window.dc = delete_cookie
77
78
79 board_cols = board_col_heights.length
80 board_tiles = board_col_heights.sum()
81 board_col_top_px = []
82 score = 0
83 tiles = new Array(board_tiles)
84 board_neighbors = [] # array of tile numbers "next to" this one
85 tile_top_px = [] # array of pixel coordinates for top of column
86 board_left_px = [] # array of pixel coordinates for left of column
87 board_aboves = [] # array of tile numbers above, starting from top
88 board_below = [] # tile number of next tile below or false
89
90 selected = []
91
92 letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
93 letter_distribution = [
94         14355 # a
95          3968 # b
96          6325 # c
97          7045 # d
98         20258 # e
99          2739 # f
100          5047 # g
101          4372 # h
102         13053 # i
103           516 # j
104          2600 # k
105          9631 # l
106          5115 # m
107         10082 # n
108         11142 # o
109          5292 # p
110           287 # qu
111         12341 # r
112         16571 # s
113         10215 # t
114          6131 # u
115          1728 # v
116          2184 # w
117           619 # x
118          3512 # y
119           831 # z
120
121 ]
122
123 letter_distribution_total = 175973 # letter_distribution.sum()
124
125
126 new_letter_queue = []
127 new_letter = ->
128         if new_letter_queue.length
129                 l = new_letter_queue.shift()
130                 l.letter = l.letter.toUpperCase()
131                 if l.letter is 'Q'
132                         l.letter = 'Qu'
133                 return l
134         hp = 1 + Math.floor(Math.random() * (HP_MAX - 1))
135         r = Math.floor Math.random() * (letter_distribution_total + 1)
136         for i in [0..25]
137                 r -= letter_distribution[i]
138                 if r <= 0
139                         if letters[i] is 'Q'
140                                 return letter: 'Qu', hp: hp
141                         return letter: letters[i], hp: hp
142         return letter: 'Z', hp: hp # just in case
143
144
145
146 # in memory it's layed out like this:
147 # a c f j m
148 # b d g k n
149 #   e h l
150 #     i
151 # for display, columns are slid vertically like so:
152 #       f
153 #     c   j
154 #   a   g   m
155 #     d   k
156 #   b   h   n
157 #     e   l
158 #       i
159 #
160 # work out which grid spaces are connected
161 init_board_layout = () ->
162         col_offset = 0
163         middle_col = (board_cols - 1) / 2
164
165         # how many tiles before the current tile?
166         for col_num in [0 .. board_cols - 1]
167                 if col_num < middle_col
168                         fw_other = 1
169                 else
170                         fw_other = -1
171
172                 if col_num > middle_col
173                         bw_other = 1
174                 else
175                         bw_other = -1
176
177                 is_first_col = col_num is 0
178                 is_last_col = col_num is board_cols - 1
179
180                 neighbors = []
181                 push = (offset) ->
182                         neighbors.push col_offset + offset
183
184                 col_top_px = Math.abs col_num - middle_col
185                 col_top_px *= tile_radius
186                 board_col_top_px.push col_top_px
187
188                 above = []
189                 for i in [0 .. board_col_heights[col_num] - 1]
190                         is_top_tile = i is 0
191                         is_bottom_tile = i is board_col_heights[col_num] - 1
192
193                         # link tile number to pixel "top" and "left" of containing column
194                         tile_top_px.push col_top_px + i * tile_width
195                         board_left_px.push col_num * tile_width
196
197                         # aboves (array of tile numbers above, starting from top)
198                         board_aboves.push above.clone()
199                         above.push i + col_offset
200
201                         # below (SINGLE tile number of tile below or false)
202                         if is_bottom_tile
203                                 board_below.push false
204                         else
205                                 board_below.push col_offset + i + 1
206
207                         # neighbors (array of tile numbers "next to" this one)
208                         neighbors = []
209                         unless is_top_tile # upward link
210                                 push i - 1
211                         unless is_bottom_tile # downward links
212                                 push i + 1
213                         unless is_first_col # leftward links
214                                 unless is_bottom_tile and bw_other is -1
215                                         push i - board_col_heights[col_num - 1]
216                                 unless is_top_tile and bw_other is -1
217                                         push i - board_col_heights[col_num - 1] + bw_other
218                         unless is_last_col # rightward links
219                                 unless is_bottom_tile and fw_other is -1
220                                         push i + board_col_heights[col_num]
221                                 unless is_top_tile and fw_other is -1
222                                         push i + board_col_heights[col_num] + fw_other
223
224                         board_neighbors.push neighbors.clone()
225
226                 col_offset += board_col_heights[col_num]
227
228 # support obsolete save data format
229 load_game_0 = (encoded) ->
230         letters = (encoded.substr 0, board_tiles).split ''
231         for l in letters
232                 new_letter_queue.push {
233                         letter: l,
234                         hp: 1 + Math.floor(Math.random() * (HP_MAX - 1))
235                 }
236         score = parseInt(encoded.substr(board_tiles), 10)
237
238 load_game_1 = (encoded) ->
239         int = 0
240         encoded = encoded.substr 1
241         score = parseInt(encoded.substr(board_tiles * 3 / 2), 10)
242         for t in [0...(tiles.length * 3 / 2)] by 3
243                 int = 0
244                 for d in [0...3]
245                         int *= 44
246                         char = encoded[t + 2 - d]
247                         int += save_charset.indexOf(char)
248                 t2hp = int % 11
249                 int = Math.floor(int / 11)
250                 t2letter = String.fromCharCode(char_a + (int % 26))
251                 int = Math.floor(int / 26)
252                 t1hp = int % 11
253                 int = Math.floor(int / 11)
254                 t1letter = String.fromCharCode(char_a + (int % 26))
255                 new_letter_queue.push {
256                         letter: t1letter,
257                         hp: t1hp
258                 }
259                 new_letter_queue.push {
260                         letter: t2letter,
261                         hp: t2hp
262                 }
263
264 load_game = (encoded) ->
265         switch encoded.substr 0, 1
266                 when "1"
267                         load_game_1(encoded)
268                 else
269                         load_game_0(encoded)
270
271 init_board = ->
272         encoded = window.location.hash
273         if encoded? and encoded.charAt 0 is '#'
274                 encoded = encoded.substr 1
275         unless encoded? and encoded.length > board_tiles
276                 encoded = get_cookie 'hexbog'
277         if encoded? and encoded.length > board_tiles
278                 load_game encoded
279
280         # work out which grid spaces are connected
281         # (neighbors, above, down)
282         init_board_layout()
283
284 $big_tip = null # initialized by init_html_board
285 $little_tip = null # initialized by init_html_board
286 $score_display = null # initialized by init_html_board
287 $definition_body = null # initialized by init_html_board
288 update_selection_display = ->
289         word = selected_word()
290         $big_tip.removeClass('good')
291         if word.length > 0
292                 if word.length < 3
293                         $big_tip.html word
294                         $little_tip.html "Click more tiles (3 minimum)"
295                 else
296                         if is_word word
297                                 if word.indexOf(word.substr(word.length - 1)) < word.length - 1
298                                         last = 'last '
299                                 else
300                                         last = ''
301                                 $little_tip.html "Click the #{last}\"#{word.substr(word.length - 1)}\" for #{score_for word} points"
302                                 $big_tip.html "<a href=\"http://en.wiktionary.org/wiki/#{word}\" target=\"_blank\" title=\"click for definition\">#{word}</a>"
303                                 $big_tip.addClass('good')
304                         else
305                                 $big_tip.html word
306                                 $little_tip.html "\"#{word}\" is not in the word list."
307         else
308                 $big_tip.html "← Click a word"
309                 $little_tip.html "(tiles must be touching)"
310
311         # color the selected tiles according to whether they're a word or not
312         if word.length
313                 classes = ['selected_word', 'selected']
314                 if is_word word
315                         c = 0
316                 else
317                         c = 1
318                 for num in selected
319                         tiles[num].dom.addClass classes[c]
320                         tiles[num].dom.removeClass classes[1 - c]
321
322 # unselects the last tile of the selecetion
323 unselect_tile = ->
324         _unselect_tile()
325         update_selection_display()
326
327 _unselect_tile = ->
328         num = selected.pop()
329         html_tile = tiles[num].dom
330         html_tile.removeClass 'selected_word'
331         html_tile.removeClass 'selected'
332
333 unselect_all = ->
334         while selected.length
335                 _unselect_tile()
336         update_selection_display()
337
338 selected_word = ->
339         word = ''
340         word += tiles[i].text for i in selected
341         return word.toLowerCase()
342
343 save_charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQR'
344 char_a = "a".charCodeAt(0)
345 save_game = ->
346         encoded = '1' # save format
347         for i in [0...tiles.length] by 2
348                 int = tiles[i].text.toLowerCase().charCodeAt(0) - char_a
349                 int *= 11
350                 int += tiles[i].hp
351                 int *= 26
352                 int += tiles[i+1].text.toLowerCase().charCodeAt(0) - char_a
353                 int *= 11
354                 int += tiles[i+1].hp
355                 for d in [0...3]
356                         encoded += save_charset.substr(int % 44, 1)
357                         int = Math.floor(int / 44)
358         encoded += score
359         set_cookie 'hexbog', encoded, 365
360         window.location.hash = encoded
361
362 unsink = (tile) ->
363         tile.new_hp = 10
364         tile.text = new_letter().letter
365         tile.dom.html tile.text
366
367 # remove the selected tiles from the board, create new tiles, and slide everything into place
368 blip_selection = ->
369         difficulty = 11 - Math.log(400 + score) # higher numbers are easier
370         force = difficulty * score_for selected_word() # how much tile restoration we have left to do
371         word_length = selected_word().length
372         faders = selected.num_sort()
373         selected = []
374         update_selection_display()
375         neighbors = {}
376         nneighbors = {}
377         for i in faders
378                 tiles[i].dom.unbind('click').fadeOut fade_ms
379                 tiles[i].new_hp = tiles[i].hp
380                 for n in board_neighbors[i]
381                         neighbors[n] = tiles[n]
382                         for nn in board_neighbors[n]
383                                 nneighbors[nn] = tiles[nn]
384         for i in faders
385                 delete nneighbors[i]
386                 delete neighbors[i]
387         for k, v of neighbors
388                 delete nneighbors[k]
389         # convert to arrays so we can sort, etc
390         nneighbors = (v for k, v of nneighbors)
391         neighbors = (v for k, v of neighbors)
392         boom = [
393                 {
394                         tiles: neighbors,
395                         up: [],
396                         down: []
397                 },
398                 {
399                         tiles: nneighbors,
400                         up: [],
401                         down: []
402                 }
403         ]
404         for n in boom
405                 for t in n.tiles
406                         if t.hp is 0
407                                 n.down.push t
408                         else
409                                 n.up.push t
410         switch word_length
411                 when 3
412                         boom[0].flips = 1
413                         boom[0].force = 2
414                         boom[1].flips = 0
415                         boom[1].force = 0
416                 when 4
417                         boom[0].flips = 'all'
418                         boom[0].force = 4
419                         boom[1].flips = 0
420                         boom[1].force = 2
421                 when 5
422                         boom[0].flips = 'all'
423                         boom[0].force = 6
424                         boom[1].flips = 2
425                         boom[1].force = 4
426                 when 6
427                         boom[0].flips = 'all'
428                         boom[0].force = 10
429                         boom[1].flips = 5
430                         boom[1].force = 6
431                 when 7
432                         boom[0].flips = 'all'
433                         boom[0].force = 10
434                         boom[1].flips = 'all'
435                         boom[1].force = 10
436                 else
437                         boom[0].flips = 0
438                         boom[0].force = 0
439                         boom[1].flips = 0
440                         boom[1].force = 0
441                         # unsink/heal the whole board
442                         for t in tiles
443                                 if t.hp is 0
444                                         unsink t
445                                 else
446                                         t.new_hp = 10
447         for b in boom
448                 if b.flips is 'all' or b.flips >= b.down.length
449                         for t in b.down
450                                 unsink t
451                 else
452                         down_count = b.down.length
453                         while b.flips > 0 and down_count
454                                 b.flips -= 1
455                                 flipper = Math.floor(Math.random() * down_count)
456                                 unsink b.down[flipper]
457                                 down_count -= 1
458                                 # move the last tile back into range
459                                 b.down[flipper] = b.down[down_count]
460                 if b.force > 0
461                         for t in b.up
462                                 t.new_hp = t.hp + b.force
463         for i in tiles
464                 i.new_hp ?= i.hp - 1
465                 if i.new_hp < 0
466                         i.new_hp = 0
467                 else if i.new_hp > HP_MAX
468                         i.new_hp = HP_MAX
469                 if i.new_hp isnt i.hp
470                         i.dom.removeClass "hp#{i.hp}"
471                         i.dom.addClass "hp#{i.new_hp}"
472                         i.hp = i.new_hp
473                 delete i.new_hp
474         timeout fade_ms + 1, ->
475                 # which tiles need to be slid down
476                 sliders = (false for i in tiles)
477
478                 prev_col_top = null
479                 next_new_y = null
480                 for deleted in faders
481                         # find the tile number of the top tile in this column
482                         if board_aboves[deleted].length is 0
483                                 col_top = deleted
484                         else
485                                 col_top = board_aboves[deleted][0]
486
487                         # reset location where new tiles appear when we change columns
488                         if prev_col_top isnt col_top
489                                 next_new_y = -10 - tile_width
490                                 prev_col_top = col_top
491
492                         tiles[deleted].dom.remove()
493
494                         # For each each tile above the one we've deleted:
495                         # 1. move it down one slot in the data scructures
496                         # 2. mark it as needing to slide
497                         dest = deleted
498                         aboves = board_aboves[deleted].clone().reverse()
499                         for above in aboves
500                                 tiles[dest] = tiles[above]
501                                 tiles[dest].id = dest
502                                 tiles[dest].dom.data 'tile_number', dest
503                                 sliders[dest] = true
504                                 --dest
505                         sliders[col_top] = true # the new tile needs to be slid down too
506
507                         new_tile col_top, board_left_px[col_top], next_new_y
508                         next_new_y -= tile_width + 50
509
510                 for slide, i in sliders
511                         if slide
512                                 tiles[i].dom.animate {top: "#{tile_top_px[i]}px"}, slide_ms
513                                 sliders[i] = false
514                 save_game()
515
516 score_for = (word) -> Math.round(Math.pow(1.7, word.length))
517
518 activate_selection = ->
519         word = selected_word()
520         if word.length < 3
521                 # FIXME make this a hint
522                 log "Too short: \"#{word}\""
523                 return
524         unless is_word word
525                 # FIXME make this automatically part of the selection display
526                 log "Not on word list: \"#{word}\""
527                 return
528         word_score = score_for word
529         score += word_score
530         $score_display.html score
531         # FIXME make some kind of animation showing score gain
532         log "blipped \"#{word}\" for #{word_score} points"
533         blip_selection()
534         look_up_definition word
535         $('#definition').click()
536
537
538 show_definition = (word, type, definition, language) ->
539         html = "<a href=\"http://en.wiktionary.org/wiki/#{word}\" target=\"_blank\">"
540         html += "#{word.substr(0, 1).toUpperCase() + word.substr(1)}</a>, #{type}"
541         if language isnt 'English'
542                 html += " (#{language})"
543         html += ': '
544         html += definition
545         html += '<div id="definition_credit">Definition &copy;<a href="http://en.wiktionary.org/" target="_blank">wiktionary.org</a> CC-BY-SA</div>'
546         $definition_body.html html
547
548
549 select_tile = (num) ->
550         html_tile = tiles[num].dom
551         # html_tile.css backgroundColor: tile_selected_color
552         selected.push num
553         update_selection_display()
554         return
555
556 new_tile = (num, x, y) ->
557         l = new_letter()
558         letter = l.letter
559         hp = l.hp
560
561         html_tile = $("<div class=\"tile hp#{hp}\" style=\"left: #{x}px; top: #{y}px\" unselectable=\"on\">#{letter}</div>")
562         $board.append(html_tile)
563
564         html_tile.data 'tile_number', num
565         tiles[num] = text: letter, dom: html_tile, hp: hp, id: num
566
567         html_tile.click ->
568                 me = $(this)
569                 num = me.data 'tile_number'
570                 return if tiles[num].hp < 1
571                 if num in selected
572                         if selected_word().length > 2 and num is selected.last()
573                                 activate_selection()
574                         else
575                                 if selected.length > 1
576                                         unselect_all()
577                                         select_tile num
578                                 else
579                                         unselect_all()
580                 else # (not clicking on selected tile)
581                         if selected.length > 0 and not (num in board_neighbors[selected.last()])
582                                 unselect_all()
583                         select_tile num
584
585
586 $board = null
587 init_html_board = ->
588         $('#loading').remove()
589         $big_tip = $('#big_tip')
590         $little_tip = $('#little_tip')
591         $score_display = $('#score')
592         $score_display.html score
593         $definition_body = $('#definition_body')
594         $board = $('#board')
595         # make html for board
596         tile_number = 0
597         for col_num in [0 .. board_cols - 1]
598                 for num in [0 .. board_col_heights[col_num] - 1]
599                         x = col_num * tile_width
600                         y = board_col_top_px[col_num] + num * tile_width
601                         new_tile tile_number, x, y
602                         tile_number++
603
604 word_bins = []; word_bins.push(',') for [0...997]
605 hash_word = (word) ->
606         h = 0
607         for i in [0...word.length]
608                 h ^= word.charCodeAt(i) << ((i*3) % 21)
609         return h % 997
610 is_word = (str) ->
611         word_bins[hash_word str].indexOf(",#{str},") > -1
612
613 # this is called automatically by the compressed wordlist
614 parse_word_list = (compressed) ->
615         prefix = ''
616         cap_a = "A".charCodeAt 0
617         i = 0
618         next_chunk = ->
619                 chunk = compressed[i]
620                 for word in chunk.match(/[a-z]*[A-Z]/g)
621                         # the capital letter (at the end of the match) says how many characters
622                         # from the end of the previous word should be removed to create the prefix
623                         # for the next word. "A" for 0, "B" for 1, "C" for 2, etc
624                         bs = word[word.length - 1].charCodeAt(0) - cap_a
625                         word = prefix + word[0 ... word.length - 1]
626                         word_bins[hash_word word] += word + ','
627                         prefix = word[0 ... word.length - bs]
628                 if ++i is compressed.length
629                         return
630                 else
631                         timeout 1, next_chunk
632         timeout 1, next_chunk
633
634 extract_wiktionary_definiton = (html) ->
635         found = false
636         finds = {}
637         language = false
638         part = false
639
640         # clean HTML
641         ##################
642         # when we instantiate the html so we can use dom traversal, the browser
643         # will start loading images and such. This section attempts to mangle the
644         # html so no resources are loaded when the html is parsed.
645
646         # attributes
647         #                            src: <img>, <audio>, etc
648         #                         onload: only <body>?
649         #   archive,codebase,data,usemap: <object>
650         #                           href: <link>
651         #                 id,class,style: background: url(foo.png), etc
652         html = html.replace /(src|onload|archive|codebase|data|usemap|href|style|id|class)=['"][^"']*['"]/ig, '', html
653
654         elements = $(html)
655
656         valid_parts = ["Abbreviation", "Adjective", "Adverb", "Article", "Cardinal number", "Conjunction", "Determiner", "Interjection", "Noun", "Numeral", "Particle", "Preposition", "Pronoun", "Verb"]
657
658         edit_link_regex = new RegExp(' ?\\[edit\\] ?')
659
660         elements.each (i, el) ->
661                 #which tag: el.tagName
662                 if el.tagName is 'H2'
663                         # if we found a definition in the previous language section, run with it
664                         # (we only stop for verbs, in hopes of finding one in english)
665                         if found
666                                 return false # break
667                         part = false # mark us not being in a definition section unless the next section finds a part of speach header
668                         language = $(el).text().replace(edit_link_regex, '')
669                 if language and el.tagName is 'H3' or el.tagName is 'H4' # eg yak def uses one for english and one for dutch
670                         part = false
671                         text = $(el).text().replace(edit_link_regex, '')
672                         for p in valid_parts
673                                 if text is "#{p}"
674                                         part = p.toLowerCase()
675                                         # FIXME break
676                 if part and el.tagName is 'OL'
677                         $(el).children().each (i, el) ->
678                                 new_def = $(el).text()
679                                 if new_def.substr(0, 9) is '(obsolete' or new_def.substr(0, 8) is "(archaic" or new_def.substr(0, 20) is "Alternative form of " or new_def.substr(0, 24) is "Alternative spelling of "
680                                         key = 'lame'
681                                 else
682                                         if part is 'verb'
683                                                 key = 'verb'
684                                         else
685                                                 key = 'nonverb'
686                                 finds[key] ?= [part, new_def, language]
687                                 found = true
688                                 if part is 'verb'
689                                         # verbs are the best! stop scanning when we find one
690                                         return false # break
691                         if found.verb
692                                 return false # break
693
694         part_defs = (finds[i] for i in ['verb', 'nonverb', 'lame'] when finds[i])
695         unless part_defs.length
696                 return false
697
698         return part_defs[0]
699
700
701 look_up_definition = (word) ->
702         $definition_body.html "Looking up definition for \"#{word}\"..."
703         $.ajax({
704                 url: "http://en.wiktionary.org/w/api.php?action=parse&format=json&page=#{word}"
705                 jsonpCallback: "lud_#{word}" # always use the same callback for the same word so it's cacheable
706                 dataType: 'jsonp'
707                 cache: true
708                 success: (data, error_msg, xhr) ->
709                         if data?.parse?.text?['*']?
710                                 tdl = extract_wiktionary_definiton data.parse.text['*']
711                                 if tdl
712                                         show_definition word, tdl[0], tdl[1], tdl[2]
713                                 else
714                                         $definition_body.html "Oops, could't find a definition for \"#{word}\"."
715                         else
716                                 $definition_body.html "Sorry, couldn't find a definition for \"#{word}\"."
717         })
718
719 board_as_txt = ->
720         ret = ''
721         index = 0
722         for i in [0 .. board_min_height * 2]
723                 unless i % 2
724                         ret += '   ' + tiles[j].text for j in [board_min_height + index .. board_tiles - 1] by 2 * board_min_height + 1
725                 else
726                         ret += ' ' + tiles[j].text + '  ' for j in [index .. board_tiles - 1] by 2 * board_min_height + 1
727                         index += 1
728
729                 ret += '\n'
730         return ret
731
732 start_over = ->
733         selected = []
734         score = 0
735         $score_display.html score
736         for i in [0...board_tiles]
737                 selected.push i
738         blip_selection()
739
740 init_start_over_link = ->
741         $('#start-over').click (event) ->
742                 event.preventDefault()
743                 if confirm "Are you sure you want to start over? There is no undo."
744                         start_over()
745
746 cur_tab = 'instructions'
747 tabtab_height = 20
748 tab_height = 150
749 init_tab = (t) ->
750         $('#' + t).click ->
751                 return if t is cur_tab
752                 $('#' + cur_tab).removeClass('selected-tab').addClass('tab').animate({height: tabtab_height}, 1000)
753                 $('#' + t).removeClass('tab').addClass('selected-tab').animate({height: tab_height}, 1000)
754                 cur_tab = t
755 init_tabs = ->
756         for t in ['instructions', 'definition', 'donate', 'restart']
757                 init_tab t
758
759 init_keybinding = ->
760         $(window).keydown (e) ->
761                 switch e.keyCode
762                         when 32, 10, 13
763                                 activate_selection()
764                         when 27
765                                 unselect_all()
766
767 log = (msg) ->
768         console.log msg if console?.log?
769
770 init_game = ->
771         if $(window).height() >= 440
772                 $('#centerer').css('margin-top', '25px')
773         init_keybinding()
774         init_tabs()
775         init_board()
776         init_html_board()
777         init_start_over_link()
778         update_selection_display()
779
780 $(init_game)