Add game of life example
This commit is contained in:
parent
cf2e5348bb
commit
e7b67d85a9
118
examples/game_of_life.nek
Normal file
118
examples/game_of_life.nek
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
width <- 10;
|
||||||
|
height <- 10;
|
||||||
|
|
||||||
|
field <- [width*height];
|
||||||
|
field2 <- [width*height];
|
||||||
|
|
||||||
|
field[1] = 1;
|
||||||
|
field[12] = 1;
|
||||||
|
field[20] = 1;
|
||||||
|
field[21] = 1;
|
||||||
|
field[22] = 1;
|
||||||
|
|
||||||
|
|
||||||
|
runs <- 0;
|
||||||
|
loop runs < 20; runs = runs + 1 {
|
||||||
|
// Print the field
|
||||||
|
y <- 0;
|
||||||
|
loop y < height; y = y+1 {
|
||||||
|
x <- 0;
|
||||||
|
loop x < width; x = x+1 {
|
||||||
|
if field[y*height + x] {
|
||||||
|
print "# ";
|
||||||
|
} else {
|
||||||
|
print ". ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
print "\n";
|
||||||
|
}
|
||||||
|
print "\n";
|
||||||
|
|
||||||
|
// Update from field into field2
|
||||||
|
y <- 0;
|
||||||
|
loop y < height; y = y+1 {
|
||||||
|
x <- 0;
|
||||||
|
loop x < width; x = x+1 {
|
||||||
|
|
||||||
|
neighbours <- 0;
|
||||||
|
|
||||||
|
if y > 0 {
|
||||||
|
if x > 0 {
|
||||||
|
// Top left
|
||||||
|
if field[(y-1)*width + (x-1)] {
|
||||||
|
neighbours = neighbours + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Top
|
||||||
|
if field[(y-1)*width + x] {
|
||||||
|
neighbours = neighbours + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if x < width-1 {
|
||||||
|
// Top right
|
||||||
|
if field[(y-1)*width + (x+1)] {
|
||||||
|
neighbours = neighbours + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if x > 0 {
|
||||||
|
// Left
|
||||||
|
if field[y*width + (x-1)] {
|
||||||
|
neighbours = neighbours + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if x < width-1 {
|
||||||
|
// Right
|
||||||
|
if field[y*width + (x+1)] {
|
||||||
|
neighbours = neighbours + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if y < height-1 {
|
||||||
|
if x > 0 {
|
||||||
|
// Bottom left
|
||||||
|
if field[(y+1)*width + (x-1)] {
|
||||||
|
neighbours = neighbours + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Bottom
|
||||||
|
if field[(y+1)*width + x] {
|
||||||
|
neighbours = neighbours + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if x < width-1 {
|
||||||
|
// Bottom right
|
||||||
|
if field[(y+1)*width + (x+1)] {
|
||||||
|
neighbours = neighbours + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if neighbours < 2 || neighbours > 3 {
|
||||||
|
field2[y*width + x] = 0;
|
||||||
|
} else {
|
||||||
|
if neighbours == 3 {
|
||||||
|
field2[y*width + x] = 1;
|
||||||
|
} else {
|
||||||
|
field2[y*width + x] = field[y*width + x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transfer from field2 to field
|
||||||
|
y <- 0;
|
||||||
|
loop y < height; y = y+1 {
|
||||||
|
x <- 0;
|
||||||
|
loop x < width; x = x+1 {
|
||||||
|
field[y*width + x] = field2[y*width + x];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user