Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first commit, dot propagation algorithm
- Loading branch information
0 parents
commit e701629
Showing
7 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
$(function(){ | ||
theGrid = new Grid({ | ||
col : 5, | ||
row : 5, | ||
target : 'grid' | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/opt/dev/lib/web/javascript/jquery |