let array'width = 256 /* cells across the board */ let array'height = 256 /* cells down the board */ let radius = 1 /* 'sphere of influence' */ let diameter = 2 * radius + 1 let neighbours = diameter * diameter - 1 let alive = true let dead = !alive type BOARD context [array'width][array'height] single BOOL let board := BOARD let pause := INT'SINGLE'CONTEXT procedure next'state (x, y, nx, ny) let living := b let neighbours'state := _ -> BOOL : { { i for neighbours :/* count the number living */ { board[nx[i]][ny[i]] ? neighbours'state test neighbours'state = alive : ++living neighbours'state = dead : skip . } } test /* death from isolation */ living < 2 : board[x][y] ! dead /* cell is stable */ living = 2 : skip /* stable if alive, birth if dead */ living = 3 : board[x][y] ! alive /* death from overcrowding */ living > 3 : board[x][y] ! dead . } procedure cell(x, y) let left = (x - 1 + array'width) % array'width let right = (x + 1) % array'width let up = (y + 1) % array'height let down = (y - 1 + array'height) % array'height let nx = [right, x, left, left, left, x, right, right] let ny = [down, down, down, y, up, up, up, y] let running := true let any := _ -> INT : { board[x][y] ! dead while running : { next'state(x, y, nx, ny) screen[x][y] := board[x][y] pause ? any } } procedure edit'board () /* start cursor in mid screen */ let x := array'width / 2 let y := array'height / 2 let editing := true let key := _ -> char function min (a, b) = (if a <= b : a else b) function max (a, b) = (if a >= b : a else b) : while editing : { screen ! move screen ! x screen ! y keyboard ? key select key : /* change state */ ' ' : { board[x][y] ? state board[x][y] ! !state } /* move up if possible */ 'U', 'u' : y := max(y-1, 0) /* move down if possible */ 'D', 'd' : y := min(y+1, array'height-1) /* move right if possible */ 'R', 'r' : x := min(x+1, array'width-1) /* move left if possible */ 'L', 'l' : x := max(x-1, 0) /* quit */ 'Q', 'q' : editing := false /* ignore other input */ else skip . } procedure control() let key := _ -> char let running := true let any := 0 : while running : { keyboard ? key pause ! any select key : 'E', 'e' : { pause ? any edit'board() pause ! any } 'Q', 'q' : running := false else skip . } : /* body of program */ /* Synchronized Evolution */ { screen ! clear edit'board() || control() || x for array'width : || y for array'height : cell(x, y) . . } /* * Non-synchronized Evolution * We achieve a non-synchronized evolution by replacing the cooperation with a subordination. { screen ! clear edit'board() // x for array'width // y for array'height cell(x, y);; control() } */