fn populate() [Int] { # Initialize life vector var life: [Int] = [0]; vpopi(life); # Fill the vector with random values var i: Int = 0; while i < 2500 { var n: Int = randGen(0,1); vpushi(life, n); i = i+1; } return life; } fn coord(x: Int, y: Int) Int { var res: Int = -1; # Calculate index based on coordinates if x >= 0 && y >= 0 { res = y*50 + x; } # if coordinate is invalid, return -1 return res; } 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); if c >= 0 { res = vgeti(life, c); } return res; } fn update(life: [Int], new: [Int]) { # Iterate through life var x: Int = 0; while x < 50 { var y: Int = 0; while y < 50 { # Calculate total score around selected cell var total: Int = cval(x-1, y-1, life) + # Top Left cval(x-1, y , life) + cval(x-1, y+1, life) + cval(x , y-1, life) + cval(x , y+1, life) + cval(x+1, y-1, life) + cval(x+1, y , life) + 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); } } y = y+1; } x = x+1; } } fn display(life: [Int]) { # Iterate through life #termclear(); var x: Int = 0; while x < 50 { var y: Int = 0; while y < 50 { # if the cell is alive, print termpos(x, y); if cval(x, y, life) == 1{ print("#"); } else { print(" "); } y = y+1; } x = x+1; } } fn main() Int { # Populate var life: [Int] = populate(); display(life); # Play forever while true { var new: [Int] = populate(); update(life, new); display(new); life = new; wait(1.0); } return 0; }