aboutsummaryrefslogtreecommitdiff
path: root/examples/cgol.sloth
diff options
context:
space:
mode:
Diffstat (limited to 'examples/cgol.sloth')
-rw-r--r--examples/cgol.sloth81
1 files changed, 52 insertions, 29 deletions
diff --git a/examples/cgol.sloth b/examples/cgol.sloth
index 17ef31e..a0e1878 100644
--- a/examples/cgol.sloth
+++ b/examples/cgol.sloth
@@ -1,11 +1,13 @@
-fn populate() [Int] {
+fn populate() [Int]
+{
# Initialize life vector
var life: [Int] = [0];
vpopi(life);
# Fill the vector with random values
var i: Int = 0;
- while i < 57600 {
+ while i < 57600
+ {
var n: Int = randGen(0,1);
vpushi(life, n);
i = i+1;
@@ -14,17 +16,20 @@ fn populate() [Int] {
return life;
}
-fn coord(x: Int, y: Int) Int {
+fn coord(x: Int, y: Int) Int
+{
var res: Int = -1;
# Calculate index based on coordinates
- if x >= 0 && y >= 0 {
+ if x >= 0 && y >= 0
+ {
res = y*240+ x;
}
# if coordinate is invalid, return -1
return res;
}
-fn cval(x: Int, y: Int, life: [Int]) Int {
+fn cval(x: Int, y: Int, life: [Int]) Int
+{
# Check to make sure index exists before returning
var res: Int = 0;
var c: Int = coord(x, y);
@@ -34,12 +39,23 @@ fn cval(x: Int, y: Int, life: [Int]) Int {
return res;
}
-fn update(life: [Int], new: [Int]) {
+fn gol(total: Int, alive: Bool) Int
+{
+
+ if !alive && total == 3 {
+ return 1;
+ }
+ if alive && ()
+ return 0;
+}
+
+fn update(life: [Int], new: [Int])
+{
# Iterate through life
- var x: Int = 0;
- while x < 64 {
- var y: Int = 0;
- while y < 240 {
+ for x in 0..64
+ {
+ for y in 0..240
+ {
# Calculate total score around selected cell
var total: Int =
cval(x-1, y-1, life) + # Top Left
@@ -52,25 +68,32 @@ fn update(life: [Int], new: [Int]) {
cval(x+1, y+1, life);
# Apply game of life rules
- var idx: Int = coord(x, y);
-
- if cval(x, y, life) == 1 {
- if total < 2 || total > 3 {
- vseti(new, idx, 0);
- } else {
- vseti(new, idx, 1);
- }
- } else {
- if total == 3 {
- vseti(new, idx, 1);
- } else {
- vseti(new, idx, 0);
- }
- }
+
+ var idx: Int = coord(x, y);
- y = y+1;
+ if cval(x, y, life) == 1
+ {
+ if total < 2 || total > 3
+ {
+ vseti(new, idx, 0);
+ }
+ else
+ {
+ vseti(new, idx, 1);
+ }
+ }
+ else
+ {
+ if total == 3
+ {
+ vseti(new, idx, 1);
+ }
+ else
+ {
+ vseti(new, idx, 0);
+ }
+ }
}
- x = x+1;
}
}
@@ -95,10 +118,10 @@ fn main() Int {
curshide();
# Play forever
while true {
- var new: [Int] = populate();
+ var new: [Int] = populate();
update(life, new);
display(new);
- life = new;
+ life = new;
wait(100);
}
return 0;