KenKen generator
Builds a kenken board
by danShumway
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/2.0.0/handlebars.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore.js"></script>
<script id="board" type="text/x-handlebars-template">
<table>
{{#each row}}
<tr class="row_{{@index}}">
{{#each this.column}}
<th class="column_{{@index}}">{{this}}</th>
{{/each}}
{{/each}}
</table>
</script>
<div id="base">
Hello
</div>
CSS
th {
border-style:solid;
width:30px;
height:30px;
}
table {
border-collapse: collapse;
}
/*styling options*/
.left {
border-left-style:none;
}
.right {
border-right-style:none;
}
.top {
border-top-style:none;
}
.bottom {
border-bottom-style:none;
}
JavaScript
//---------------------------
//------GENERATE TEMPLATE----
//---------------------------
var source = document.getElementById("board").innerHTML;
var template = Handlebars.compile(source);
//-----------------------------
//---------Groups for internal logic
//-----------------------------
var Group = function(operator) {
this.members = [];
this.operator = operator;
this.getTotal = function() {
var total = 0;
for(var i = 0; i < this.members.length; i++) {
if(this.operator === 1){
total += this.emembers[i];
}
}
return total;
};
}
//--------------------------
//Generate board.
//--------------------------
var generate = function(width) {
var board = [];
//Generate column availability.
var available = []; var r_av;
for(var i = 1; i <= width; i++) {
r_av = [];
for(var j = 1; j <= width; j++) {
r_av.push(j);
}
available.push(r_av);
}
//Fill in across each row.
for(i = 0; i < width; i++){
board.push([]); //Add new row.
//Build available for across (always 1-6).
var across = [];
for(var k = 1; k <= width; k++) {
across.push(k);
}
//Head across and fill in the final board.
var toPick;
for(j = 0; j < width; j++){
//Get intersection of two available arrays and pick a random index.
toPick = _.intersection(available[j], across);
if(toPick.length === 0) { return -1; }
//alert(toPick);
num = toPick[Math.floor(Math.random()*toPick.length)];
board[i].push(num);
//Delete from both.
available[j] = _.without(available[j], num);
across = _.without(across, num);
}
}
return board;
}
//
var...