Manandy Software Development --- CREATE SQUARE ADJ MAT
Create Square ADJ MAT
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>
<script src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<div id="canvasHolder">
<canvas id="gameCanvas" width="320" height="480">Not supported</canvas>
</div>
CSS
.tile {
display: inline;
}
.inpath {
color: red;
}
#gameCanvas {
background-color: lightsalmon;
}
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();
...