Skip to content

Commit

Permalink
add GameOfTransferAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
guanlisheng committed Dec 24, 2014
1 parent c791786 commit 58781a6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions GameOfTransferAccount/description.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Inspired by Conway's Game of Life.
5 changes: 5 additions & 0 deletions GameOfTransferAccount/luacontent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function handle_record(record)
end

function complete(result)
end
4 changes: 4 additions & 0 deletions GameOfTransferAccount/sqlcontent.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

SELECT DISTINCT ACCOUNTID, TOACCOUNTID
FROM CHECKINGACCOUNT_V1
WHERE TRANSCODE = 'Transfer';
88 changes: 88 additions & 0 deletions GameOfTransferAccount/template.htt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<script>
var Game = function() {
this.set = {};

var gamestateFromSet = function(set) {
var gamestate = {};


for (var cell in set) {

var a = neighboursOf(cell);
a.forEach(function(n) {
if (!gamestate[n]) gamestate[n] = 0
gamestate[n] += 1
})
};

return gamestate;
}

var neighboursOf = function(cell) {
var cell = cell.split(','), x = parseInt(cell[0]), y = parseInt(cell[1]);
return [[x-1, y-1], [x-1, y], [x-1, y+1], [x, y-1], [x, y+1], [x+1, y-1], [x+1, y], [x+1, y+1]]
}

this.add = function(cell) {
this.set[cell] = true;
}

this.isAlive = function(cell) {
return this.set[cell];
}

this.tick = function() {
nextGen = {};

var gamestate = gamestateFromSet(this.set);

for (cell in gamestate) {

var count = gamestate[cell];

if (count == 3) {
nextGen[cell] = true;
}
else if (count == 2 && this.set[cell]) {
nextGen[cell] = true;
}
}

this.set = nextGen;
}
}
</script>


<pre id="board">

</pre>

<script type="text/javascript">

game = new Game()

<TMPL_LOOP NAME=CONTENTS>
game.add([<TMPL_VAR ACCOUNTID>, <TMPL_VAR TOACCOUNTID>])
game.add([<TMPL_VAR TOACCOUNTID>, <TMPL_VAR ACCOUNTID>])
</TMPL_LOOP>

game.tick()


var board = document.getElementsByTagName("pre")[0];

function draw() {
var s = ''
for (var x = 0; x < 30; x++) {
for (var y = 0; y < 100; y++) {
s += game.isAlive([x,y]) ? 'o' : '.';
}
s += "\n";
}
board.innerHTML = s;
}

setInterval(function() { game.tick(); draw(); } , 500);

</script>

0 comments on commit 58781a6

Please sign in to comment.