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