diff options
Diffstat (limited to 'v_windows/v/old/vlib/v/gen/js/tests/life.v')
-rw-r--r-- | v_windows/v/old/vlib/v/gen/js/tests/life.v | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/v/gen/js/tests/life.v b/v_windows/v/old/vlib/v/gen/js/tests/life.v new file mode 100644 index 0000000..a89da5a --- /dev/null +++ b/v_windows/v/old/vlib/v/gen/js/tests/life.v @@ -0,0 +1,98 @@ +fn clear() { + JS.console.clear() +} + +const ( + w = 30 + h = 30 +) + +fn get(game [][]bool, x int, y int) bool { + if y < 0 || x < 0 { + return false + } + if y >= h || x >= w { + return false + } + + return game[y][x] +} + +fn neighbours(game [][]bool, x int, y int) int { + mut count := 0 + if get(game, x - 1, y - 1) { + count++ + } + if get(game, x, y - 1) { + count++ + } + if get(game, x + 1, y - 1) { + count++ + } + if get(game, x - 1, y) { + count++ + } + if get(game, x + 1, y) { + count++ + } + if get(game, x - 1, y + 1) { + count++ + } + if get(game, x, y + 1) { + count++ + } + if get(game, x + 1, y + 1) { + count++ + } + return count +} + +fn step(game [][]bool) [][]bool { + mut new_game := [][]bool{} + for y, row in game { + mut new_row := []bool{} + new_game[y] = new_row + for x, cell in row { + count := neighbours(game, x, y) + new_row[x] = (cell && count in [2, 3]) || count == 3 + } + } + return new_game +} + +fn row_str(row []bool) string { + mut str := '' + for cell in row { + if cell { + str += '◼ ' + } else { + str += '◻ ' + } + } + return str +} + +fn show(game [][]bool) { + clear() + for row in game { + println(row_str(row)) + } +} + +// TODO Remove `fn main` once vet supports scripts +fn main() { + mut game := [][]bool{len: h, init: []bool{len: w}} + + game[11][15] = true + game[11][16] = true + game[12][16] = true + game[10][21] = true + game[12][20] = true + game[12][21] = true + game[12][22] = true + + JS.setInterval(fn () { + show(game) + game = step(game) + }, 500) +} |