From aed244ef71e5c9a8100f09c31a721fc91269eb5d Mon Sep 17 00:00:00 2001 From: Jason Woofenden Date: Sat, 9 Nov 2013 23:53:16 -0500 Subject: [PATCH] first stab at saving hp too --- main.coffee | 85 ++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 16 deletions(-) diff --git a/main.coffee b/main.coffee index 20746dc..c5b9da3 100644 --- a/main.coffee +++ b/main.coffee @@ -127,18 +127,19 @@ new_letter_queue = [] new_letter = -> if new_letter_queue.length l = new_letter_queue.shift() - if l is 'Q' - return 'Qu' - else - return l + l.letter = l.letter.toUpperCase() + if l.letter is 'Q' + l.letter = 'Qu' + return l + hp = 1 + Math.floor(Math.random() * (HP_MAX - 1)) r = Math.floor Math.random() * (letter_distribution_total + 1) for i in [0..25] r -= letter_distribution[i] if r <= 0 if letters[i] is 'Q' - return 'Qu' - return letters[i] - return 'Z' # just in case + return letter: 'Qu', hp: hp + return letter: letters[i], hp: hp + return letter: 'Z', hp: hp # just in case @@ -224,6 +225,48 @@ init_board_layout = () -> col_offset += board_col_heights[col_num] +# support obsolete save data format +load_game_0 = (encoded) -> + letters = (encoded.substr 0, board_tiles).split '' + for l in letters + new_letter_queue.push { + letter: l, + hp: 1 + Math.floor(Math.random() * (HP_MAX - 1)) + } + score = parseInt(encoded.substr(board_tiles), 10) + +load_game_1 = (encoded) -> + int = 0 + encoded = encoded.substr 1 + score = parseInt(encoded.substr(board_tiles * 3 / 2), 10) + for t in [0...(tiles.length * 3 / 2)] by 3 + int = 0 + for d in [0...3] + int *= 44 + char = encoded[t + 2 - d] + int += save_charset.indexOf(char) + t2hp = int % 11 + int = Math.floor(int / 11) + t2letter = String.fromCharCode(char_a + (int % 26)) + int = Math.floor(int / 26) + t1hp = int % 11 + int = Math.floor(int / 11) + t1letter = String.fromCharCode(char_a + (int % 26)) + new_letter_queue.push { + letter: t1letter, + hp: t1hp + } + new_letter_queue.push { + letter: t2letter, + hp: t2hp + } + +load_game = (encoded) -> + switch encoded.substr 0, 1 + when "1" + load_game_1(encoded) + else + load_game_0(encoded) init_board = -> encoded = window.location.hash @@ -232,8 +275,7 @@ init_board = -> unless encoded? and encoded.length > board_tiles encoded = get_cookie 'hexbog' if encoded? and encoded.length > board_tiles - new_letter_queue = (encoded.substr 0, board_tiles).split '' - score = parseInt(encoded.substr(board_tiles), HP_MAX) + load_game encoded # work out which grid spaces are connected # (neighbors, above, down) @@ -298,17 +340,28 @@ selected_word = -> word += tiles[i].text for i in selected return word.toLowerCase() +save_charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQR' +char_a = "a".charCodeAt(0) save_game = -> - encoded = '' - for t in tiles - encoded += t.text.substr 0, 1 + encoded = '1' # save format + for i in [0...tiles.length] by 2 + int = tiles[i].text.toLowerCase().charCodeAt(0) - char_a + int *= 11 + int += tiles[i].hp + int *= 26 + int += tiles[i+1].text.toLowerCase().charCodeAt(0) - char_a + int *= 11 + int += tiles[i+1].hp + for d in [0...3] + encoded += save_charset.substr(int % 44, 1) + int = Math.floor(int / 44) encoded += score set_cookie 'hexbog', encoded, 365 window.location.hash = encoded unsink = (tile) -> tile.new_hp = 10 - tile.text = new_letter() + tile.text = new_letter().letter tile.dom.html tile.text # remove the selected tiles from the board, create new tiles, and slide everything into place @@ -498,9 +551,9 @@ select_tile = (num) -> return new_tile = (num, x, y) -> - letter = new_letter() - - hp = 1 + Math.floor(Math.random() * (HP_MAX - 1)) + l = new_letter() + letter = l.letter + hp = l.hp html_tile = $("
#{letter}
") $board.append(html_tile) -- 1.7.10.4