most of step function

This commit is contained in:
Dan Ballard 2011-06-19 22:39:10 -07:00
parent f84c84fc1a
commit 4b134d7334
1 changed files with 31 additions and 6 deletions

View File

@ -3,7 +3,7 @@
<title>The Game of Life: HTML Canvas Demo</title>
<script language="javascript">
var size;
var speed;
var hertz;
var height;
var width;
var cells;
@ -14,18 +14,23 @@
btn.value = name;
}
function init_cells() {
function gen_cells() {
var hcells = height/size;
var wcells = width/size;
cells = new Array(hcells);
var new_cells = new Array(hcells);
for(var i = 0; i < hcells; i++) {
cells[i] = new Array(wcells);
new_cells[i] = new Array(wcells);
}
for (var y = 0; y < hcells; y++) {
for (var x = 0; x < wcells; x++) {
cells[y][x] = false;
new_cells[y][x] = false;
}
}
return new_cells;
}
function init_cells() {
cells = gen_cells();
}
function board_init() {
@ -34,7 +39,7 @@
height = canvas.height;
btn_control_set_name('Start');
size = 10;
speed = 50;
hertz = 4;
init_cells();
redraw();
}
@ -87,6 +92,26 @@
window.onload = board_init;
function step() {
var new_cells = gen_cells();
for(var y=0; y <= height; y++) {
for(var x =0 ; x <= width; x++) {
var n = num_neighbours(x, y);
if (cells[y][x] == true) {
if (n < 2 || n > 3) {
new_cells[y][x] = false;
} else {
new_cells[y][x] = true;
}
} else if (n == 3) {
new_cells[y][x] = true;
} else {
new_cells[y][x] = false;
}
}
}
}
</script>
</head>
<body>