//---------------------------- // 0 1 2 3 // --- --- --- --- // 0 | 2a 4 4 2c // 1 | 2a 4 4 2c // 2 | 2b 3 3 2d // 3 | 2b 1b 1c 2d // 4 | 1a 0 0 1d //---------------------------- function Cell(type, x, y) { this.x = x; this.y = y; this.type = type; } /* return true if cells's status is in "WIN" state */ function has_win() { for (var i=0; i 3) { return true; } if (y < 0 || y > 4) { return true; } if (matrix[x][y]) { return true; } matrix[x][y] = cell.type; } return false; } /* return true if target obj can move on the given direction(dir_index) */ function can_move(obj, dir_index) { /* judge if we can move on current dir_index */ /* try to fill the zero-table, if we hit an "non-zero" then it's forbidden */ var dx = directions[dir_index][0]; var dy = directions[dir_index][1]; var target_cell = cell_div_map[obj.id]; var x = target_cell.x; var y = target_cell.y; var test_cell = new Cell(target_cell.type, x + dx, y + dy); var matrix = [[0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0], [0,0,0,0,0]]; //catch out of bounds issue if (have_conflict(matrix, test_cell)) return false; for (var i=0; i这里'); else if (typeof(is_in_add_page) == "undefined") alert("点击“重新开始”或刷新此页面以重玩本布局"); return; //do nothing if the block is locked } var count = 0; var move = null; var dir_next = 0; if (game_steps.length > 0 && game_steps[game_steps.length - 1][0] === obj.id) { dir_next = (dir_revert[last_dir_index] + 1) % 4; } while (count < 4) { //4 directions if (can_move(obj, dir_next)) { move = dir_next; break; } dir_next = (dir_next + 1) % 4; count++; } if (move === null ) { //no valid move for current block soundManager.play("nomove"); return; } last_dir_index = move; //let's move! // we could get at MOST 2 moves! //move the cell var left = parseInt($(obj).css('left'), 10); var top = parseInt($(obj).css('top'), 10); var direction = directions[move]; var moves = ["-=", "", "+="]; var premove = moves[direction[0] + 1]; if (premove) $(obj).animate({left: premove+unit }, 100); premove = moves[direction[1] + 1]; if (premove) $(obj).animate({top: premove+unit }, 100); soundManager.play("move"); //save the cell cell_div_map[obj.id].x += direction[0]; cell_div_map[obj.id].y += direction[1]; //save last move game_steps.push([obj.id, move]); update_recent_steps(); //have we win? if (has_win()) { show_win_and_change_status(); } } /*当前的设计可能会在扩展华容道配置(不是十个方块)的时候存在潜在问题,需要修改 */ function create_cell_divs() { var div_arr = ['11', '12', '13', '14', '15', '16', '17', '18', '19', '110', '111', '112', '113', '114', '115', '116', '21', '22', '23', '24', '25', '26', '31', '32', '33', '34', '35', '36', '41']; var name_arr = ['兵', '卒', '勇', '士', '兵2', '卒2', '勇2', '士2', '兵3', '卒3', '勇3', '士3', '兵4', '卒4', '勇4', '士4', '赵云', '黄忠', '马超', '张飞', '关羽', '魏延', '关羽', '张飞', '马超', '黄忠', '赵云', '魏延', '曹操']; var jqobj = $("#canvas"); for (var i=0; i '; jqobj.append(html_str); } } /* this function CHANGES cell_div_map & cellid_to_index */ function hrd_render(cells) { var ccounts = [0, 0, 0, 0, 0]; for (var i=0; i 5) id_index = 6; var id ="hrdcell" + cell.type + id_index; var jqobj = $("#" + id); jqobj.show().css('left', x + 'px').css('top', y + 'px'); cell_div_map[id] = cell; cellid_to_index[id] = i; index_to_cellid[i] = id; } } /* ab - Action Buttion functions */ function ab_rollback() { if (game_steps.length <= 0) { soundManager.play("nomove"); return; } soundManager.play("move"); var step = game_steps.pop(); var id = step[0]; var move = step[1]; var direction = directions[move]; //roback the block var jqobj = $("#" + id); var left = parseInt(jqobj.css('left'), 10); var top = parseInt(jqobj.css('top'), 10); jqobj.css('left', left - unit * direction[0]); jqobj.css('top', top - unit * direction[1]); //roback the cell map cell_div_map[id].x -= direction[0]; cell_div_map[id].y -= direction[1]; update_recent_steps(); } function ab_reset() { if (game_steps.length <= 0) { soundManager.play("nomove"); return; } if (!locked) { //lock状态下就无需提示了 if (!confirm("您已经走了几步了,真的要重新开始吗?")) return; } locked = false; soundManager.play("move"); //reset all the status last_dir_index = 0; var cstr = $("#canvas").attr('tag'); carr = eval(cstr); for (var i=0; i 2) ? 2 : 1; } function get_cell_h(cell) { return (cell.type % 2) ? 1 : 2; } function get_cell_color(cell) { return ["rgb(0,200,0)", "rgb(0,0,200)", "rgb(200,0,200)", "rgb(200,0,0)"][cell.type - 1]; } /* hrd_small_canvas render and toggle for list/add/ */ function clear_marks() { set_cookie("hrdlist_ids", ""); $(".layout").removeClass("marked"); } function toggle_mark(layout) { $(layout).toggleClass("marked"); } function toggle_mark_and_remember(layout) { var ids = get_cookie("hrdlist_ids"); if (!ids) ids = ""; var id = layout.id.substring("layout".length); //layout{{#id}} var pos = ("," + ids + ",").indexOf("," + id + ","); if (pos == -1) { ids += "," + id; } else { ids = ids.substring(0, pos) + ids.substring(pos + ("," + id).length); } set_cookie("hrdlist_ids", ids); toggle_mark(layout); } function hrd_render_on_canvas() { var unit = 20; var border = 1; var i = 0; var ids = get_cookie("hrdlist_ids"); $('.layoutcanvas').each(function() { //calc the cells var layout = eval($(this).attr('tag')); var cells = []; for (i=0; i '); newdiv.css('left', x); newdiv.css('top', y); canvas.append(newdiv); } }); } //////////////////////////////////////////////////////////////////////////////////////////////// // ------ hrd_replay stuff ------ // function set_replay(obj, rid_or_steps, step_count) { hrd_replay_home(); var steps = null; if (obj !== null) { steps = eval($(obj).attr("tag")); $(".replay").removeClass("selected-replay"); $(obj).addClass("selected-replay"); $("#replay-info").text("#" + rid_or_steps + "," + step_count + "步"); } else { $("#replay-info").text(step_count + "步"); steps = rid_or_steps; } hrd_replay_steps = []; var i = 0; for (i=0; i= hrd_replay_steps.length) { hrd_replay_pause(); show_info("已经是最后一步了"); return false; } var step = hrd_replay_steps[hrd_replay_index]; var jqobj = $("#" + index_to_cellid[step[0]]); //move the obj var left = parseInt(jqobj.css('left'), 10); var top = parseInt(jqobj.css('top'), 10); var direction = directions[step[1]]; jqobj.css('left', left + unit * direction[0]); jqobj.css('top', top + unit * direction[1]); hrd_replay_index++; $("#countbox").text(hrd_replay_step2count[hrd_replay_index]); soundManager.play("move"); return true; } function hrd_replay_rollback() { if (hrd_replay_index === 0) return false; hrd_replay_index--; var step = hrd_replay_steps[hrd_replay_index]; var jqobj = $("#" + index_to_cellid[step[0]]); //move the obj back var left = parseInt(jqobj.css('left'), 10); var top = parseInt(jqobj.css('top'), 10); var direction = directions[step[1]]; jqobj.css('left', left - unit * direction[0]); jqobj.css('top', top - unit * direction[1]); $("#countbox").text(hrd_replay_step2count[hrd_replay_index]); soundManager.play("move"); return true; } function hrd_replay_play() { if (typeof(replay_interval) != "undefined" && replay_interval !== null) { clearInterval(replay_interval); replay_interval = null; } replay_interval = setInterval('hrd_replay_movenext()', 500); show_info("开始播放,一秒两步"); } function hrd_replay_pause() { if (typeof(replay_interval) != "undefined" && replay_interval !== null) { clearInterval(replay_interval); replay_interval = null; show_info("播放过程已暂停"); } } function hrd_replay_prev() { hrd_replay_pause(); hrd_replay_rollback(); } function hrd_replay_next() { hrd_replay_pause(); hrd_replay_movenext(); } function hrd_replay_home() { hrd_replay_pause(); hrd_replay_index = 0; //reset the status for (var i=0; i回退一步').insertBefore("#controlbox"); locked = false; game_steps = []; carr = result; hrd_play_creation(); $("#calcgate").show(); } function is_valid_layout(result) { //只要有曹操在中间,而且两个空的就行了,如果全是小块,就是14*1=15个 return (result.length >= 24 && result.length <= 45); } function hrd_hide_cells() { for (var i=0; i'; } $("#select_gates").html(gatestr); $("#select_gates").change(function () { window.location.href = 'http://fayaa.com/youxi/hrd/' + this.value + "/"; }); }); } });