Skip to content

Commit

Permalink
first commit, dot propagation algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
floriancargoet committed Mar 20, 2010
0 parents commit e701629
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 0 deletions.
Empty file added README
Empty file.
Empty file added css/cube.css
Empty file.
16 changes: 16 additions & 0 deletions index.html
@@ -0,0 +1,16 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>JSJumpingCube</title>
<link rel="stylesheet" href="css/cube.css" type="text/css" />
<script type="text/javascript" src="js/jquery/jquery.js"></script>
<script type="text/javascript" src="js/Cell.js"></script>
<script type="text/javascript" src="js/Grid.js"></script>
<script type="text/javascript" src="js/cube.js"></script>
</head>
<body>
<div id="grid"/>
</body>
</html>
17 changes: 17 additions & 0 deletions js/Cell.js
@@ -0,0 +1,17 @@
var Cell = function(options){
this.limit = options.limit;
this.dots = 1;
}
Cell.prototype = {
addDot : function(){
this.dots++;
},
getOverFlowDots : function(){
var of = (this.dots > this.limit) ? this.limit : 0;
this.dots -= of;
return of;
},
toString : function(){
return this.dots;
}
};
108 changes: 108 additions & 0 deletions js/Grid.js
@@ -0,0 +1,108 @@
var Grid = function(options){
this.col = options.col;
this.row = options.row;
this.target = options.target;

this.createCells();
this.computeMaxDots();
}

Grid.prototype = {
computeMaxDots : function(){
this.maxDots = 2*4 + 3*((this.row-2)*2 +(this.col-2)*2) +4*((this.row-2)*(this.col-2));
},
countDots : function(){
var dots = 0;
for(i=0, l=this.col;i!=l;i++){
var j,k;
for(j=0, k=this.row;j!=k;j++){
dots+=this.cells[i][j].dots;
}
}
return dots;
},
createCells : function(){
this.cells = [];
var i,l;
for(i=0, l=this.col;i!=l;i++){
this.cells.push([]);
var j,k;
for(j=0, k=this.row;j!=k;j++){
this.cells[i][j] = new Cell({
limit : (i==0 || i==this.col-1) ? (
(j==0 || j==this.row-1) ?
2 :
3
) :
(j==0 || j==this.row-1) ?
3 :
4
});
}
}
},
propagate : function(){
var changed = false;
var i,l;
for(i=0, l=this.col;i!=l;i++){
var j,k;
for(j=0, k=this.row;j!=k;j++){
var cell = this.cells[i][j];
var of = cell.getOverFlowDots();//return 'limit' dots if more than 'limit' is stored and unstore these dots
if(of>0){
var neighborCells = this.findNeighbors(i,j);
for(var nc=0, nl = neighborCells.length; nc!=nl; nc++){
if(this.countDots() >= this.maxDots){
//we diverge before that !
//but do we always win before divergence ?
//event
return;
}
changed = true;
neighborCells[nc].addDot();

}
}
}
}
//recursively
if(changed){
this.propagate();
}
},
findNeighbors : function(col, row){
var neighbors=[], neighbor,
c,r;
var trys = [
[-1,0],
[1,0],
[0,1],
[0,-1]
];
for(var i=0, l=trys.length;i!=l;i++){
c=trys[i][0];
r=trys[i][1];
try{
neighbor = this.cells[col+c][row+r];
}catch(e){
neighbor = null;
}
if(neighbor != null){
neighbors.push(neighbor);
}
}
return neighbors;
},
toString : function(){
var s="",
i,l;
for(i=0, l=this.col;i!=l;i++){
var j,k;
for(j=0, k=this.row;j!=k;j++){
s+=this.cells[i][j];
}
s+="\n";
}
return s;
}
};
8 changes: 8 additions & 0 deletions js/cube.js
@@ -0,0 +1,8 @@
$(function(){
theGrid = new Grid({
col : 5,
row : 5,
target : 'grid'
});

});
1 change: 1 addition & 0 deletions js/jquery

0 comments on commit e701629

Please sign in to comment.