Skip to content

Commit

Permalink
allow bias to be specified for binary tree algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
jamis committed Jan 25, 2011
1 parent 0f6a8c8 commit b6c253f
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 11 deletions.
46 changes: 37 additions & 9 deletions examples/maze.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@
<script src="../lib/algorithms/sidewinder.js" type="text/javascript"></script>
<script src="../lib/algorithms/wilson.js" type="text/javascript"></script>

<script type="text/javascript">
function resetMazes() {
for(var i = 0; i < arguments.length; i++) {
var id = arguments[i];
var element = document.getElementById(id);
element.mazeReset();
}
}
</script>

<style type="text/css">
* { font-family: sans-serif; }

Expand Down Expand Up @@ -119,22 +129,40 @@ <h1>"Hunt and Kill" Algorithm</h1>

<h1>Growing Tree Algorithm</h1>
<p>
<input type="text" id="growing_tree_input" value="random:100" /><br />
<small>E.g. random:50, newest:30, oldest:75, middle:100, or any comma-delimited combination of those.</small><br />
<small>You must click "reset" before the maze will recognize your changes.</small>
<input type="text" id="growing_tree_input" value="random:100" onchange="resetMazes('growingtree', 'growtree_bigger', 'growtree_biggest');" /><br />
<small>E.g. random:50, newest:30, oldest:75, middle:100, or any comma-delimited combination of those.</small>
</p>
<script type="text/javascript">
function growingTreeInput() {
return document.getElementById("growing_tree_input").value;
}
</script>
<div class="row">
<script type="text/javascript">Maze.createWidget("GrowingTree", 5, 5, { "class": "small", "watch": false, input: function() { return document.getElementById("growing_tree_input").value; } });</script>
<script type="text/javascript">Maze.createWidget("GrowingTree", 15, 15, { "class": "medium", "watch": false, id: "growtree_bigger", interval: 25, input: function() { return document.getElementById("growing_tree_input").value; } });</script>
<script type="text/javascript">Maze.createWidget("GrowingTree", 30, 30, { "class": "large", id: "growtree_biggest", interval: 10, "watch": false, input: function() { return document.getElementById("growing_tree_input").value; } });</script>
<script type="text/javascript">Maze.createWidget("GrowingTree", 5, 5, { "class": "small", "watch": false, input: growingTreeInput });</script>
<script type="text/javascript">Maze.createWidget("GrowingTree", 15, 15, { "class": "medium", "watch": false, id: "growtree_bigger", interval: 25, input: growingTreeInput });</script>
<script type="text/javascript">Maze.createWidget("GrowingTree", 30, 30, { "class": "large", id: "growtree_biggest", interval: 10, "watch": false, input: growingTreeInput });</script>
</div>
<hr />

<h1>Binary Tree Algorithm</h1>
<p>
Bias: <select id="binary_tree_input" onchange="resetMazes('binarytree', 'btree_bigger', 'btree_biggest');">
<option value="nw">Northwest</option>
<option value="ne">Northeast</option>
<option value="sw">Southwest</option>
<option value="se">Southeast</option>
</select>
</p>
<script type="text/javascript">
function binaryTreeInput() {
var s = document.getElementById('binary_tree_input');
return s.options[s.selectedIndex].value;
}
</script>
<div class="row">
<script type="text/javascript">Maze.createWidget("BinaryTree", 5, 5, { "class": "small" });</script>
<script type="text/javascript">Maze.createWidget("BinaryTree", 15, 15, { "class": "medium", id: "btree_bigger", interval: 25 });</script>
<script type="text/javascript">Maze.createWidget("BinaryTree", 30, 30, { "class": "large", id: "btree_biggest", interval: 10 });</script>
<script type="text/javascript">Maze.createWidget("BinaryTree", 5, 5, { "class": "small", input: binaryTreeInput });</script>
<script type="text/javascript">Maze.createWidget("BinaryTree", 15, 15, { "class": "medium", input: binaryTreeInput, id: "btree_bigger", interval: 25 });</script>
<script type="text/javascript">Maze.createWidget("BinaryTree", 30, 30, { "class": "large", input: binaryTreeInput, id: "btree_biggest", interval: 10 });</script>
</div>
<hr />

Expand Down
17 changes: 15 additions & 2 deletions src/algorithms/binary_tree.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,25 @@ class Maze.Algorithms.BinaryTree extends Maze.Algorithm
@x = 0
@y = 0

switch options.input ? "nw"
when "nw" then @bias = Maze.Direction.N | Maze.Direction.W
when "ne" then @bias = Maze.Direction.N | Maze.Direction.E
when "sw" then @bias = Maze.Direction.S | Maze.Direction.W
when "se" then @bias = Maze.Direction.S | Maze.Direction.E

@northBias = (@bias & Maze.Direction.N) != 0
@southBias = (@bias & Maze.Direction.S) != 0
@eastBias = (@bias & Maze.Direction.E) != 0
@westBias = (@bias & Maze.Direction.W) != 0

step: ->
return false if @y >= @maze.height

dirs = []
dirs.push Maze.Direction.N if @y > 0
dirs.push Maze.Direction.W if @x > 0
dirs.push Maze.Direction.N if @northBias and @y > 0
dirs.push Maze.Direction.S if @southBias and @y+1 < @maze.height
dirs.push Maze.Direction.W if @westBias and @x > 0
dirs.push Maze.Direction.E if @eastBias and @x+1 < @maze.width

direction = @rand.randomElement(dirs)
if direction
Expand Down

0 comments on commit b6c253f

Please sign in to comment.