Dungeon Rats
Artificial Neural Net Rat Simulator
by Andy Novocin
HTML
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.5.2/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.0/backbone-min.js"></script>
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.css">
<script src="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.js"></script>
<script src="http://code.createjs.com/easeljs-0.7.0.min.js"></script>
<formset>
<label for="genomeClassic">Classic Genome</label>
<input type="text" id="genomeClassic" placeholder="Vanilla genome goes here" value=""/>
</formset>
<hr>
<formset>
<label for="genomeExplore">Many Worlds</label>
<input type="text" id="genomeExplore" placeholder="Many map genome goes here" value=""/>
</formset>
<hr>
<formset>
<label for="genomeParadise">Paradise Genome</label>
<input type="text" id="genomeParadise" placeholder="Paradise genome goes here" value=""/>
</formset>
<hr>
<formset>
<label for="genomeRobust">Robust Vanilla Genome</label>
<input type="text" id="genomeRobust" placeholder="Robust genome goes here" value=""/>
</formset>
<hr>
<button class="btn btn-std" id="start_game">Click to Start</button> <button class="btn btn-std" id="clone">Clone Rats</button>
<h3>
Ate <span id="food">0</span> time(s) ::
Energy <span id="energy">30</span> ::
Hit <span id="obst">0</span> obstacles ::
<span id="moves">0</span> move(s) :: Status <span id="pit">non-existent</span> :: Game Mode <span id="mode">NA</span>
</h3>
<div id="canvasHolder">
<canvas id="gameCanvas" width="640" height="640">Not supported</canvas>
</div>
<hr>
<h2>Results:</h2>
<table>
<thead>
<td>classic</td>
<td>world1</td>
<td>world2</td>
<td>world3</td>
<td>world4</td>
<td>world5</td>
<td>paradise</td>
<td>spot1</td>
<td>spot2</td>
<td>spot3</td>
<td>spot4</td>
<td>spot5</td>
</thead>
<tr>
<td...
CSS
.tile {
display: inline;
}
#gameCanvas {
background-color: lightgray;
}
JavaScript
var CanvasView = Backbone.View.extend({
initialize : function(options){
options = options || {};
this.container = options.container || new createjs.Container();
this.canvasEvents(this.cEvents);
this.init(options);
},
init : function(options){
},
canvasEvents : function(cEvents){
if (!cEvents) return this;
for (var key in cEvents){
var method = cEvents[key];
if (!_.isFunction(method)) method = this[cEvents[key]];
if (!method) continue;
method = _.bind(method, this);
var eventName = key;
this.container.on(eventName, method, this);
}
return this;
}
});
var GenRand = Backbone.Model.extend({
genRand: function(nnums, values, weights, replaceToggle){
if(replaceToggle) // if true then with replacement
return this.genRandWithReplacement(nnums, values, weights);
else
return this.genRandWithoutReplacement(nnums, values, weights);
},
genRandWithReplacement: function(nnums, values, weights){
var retnums = new Array()
var pdf = new Array();
var cdf = new Array();
var i, j;
var s = 0;
var rand;
for(i = 0; i < weights.length; i++){
s += weights[i];
}
for(i = 0; i < weights.length; i++){
pdf[i] = weights[i]/s;
}
cdf[0] = pdf[0];
for(i = 1; i < weights.length; i++){
cdf[i] = cdf[i-1] + pdf[i];
}
for(i = 0; i < nnums; i++){
rand = Math.random();
for(j = 0; j < cdf.length; j++)
{
if(cdf[j] > rand){
retnums[i] = values[j];
break;
}
}
}
return retnums.slice(0,nnums);
},
genRandWithoutReplacement: function(nnums, values, weights){
var retnums = new Array();
var dist = new Array();
...