-
-
Save dom96/785c7d046e1035af4a65f04c0a0e82fd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os, strutils, terminal, random | |
| const | |
| w = 60 | |
| h = 30 | |
| # Initialize | |
| var univ, utmp = newSeq[seq[bool]](h) | |
| for y in 0..<h: | |
| univ[y].newSeq w | |
| utmp[y].newSeq w | |
| univ[15][28] = true | |
| univ[16][29] = true | |
| univ[17][27] = true | |
| univ[17][28] = true | |
| univ[17][29] = true | |
| while true: | |
| # Show | |
| stdout.eraseScreen() | |
| for y in 0..<h: | |
| for x in 0..<w: | |
| setCursorPos(stdout, x, y) | |
| if univ[y][x]: | |
| stdout.write "●" | |
| else: | |
| stdout.write " " | |
| stdout.flushFile | |
| # Evolve | |
| for y in 0..<h: | |
| for x in 0..<w: | |
| var n = 0 | |
| for y1 in y-1..y+1: | |
| for x1 in x-1..x+1: | |
| if univ[(y1+h) mod h][(x1 + w) mod w]: | |
| inc n | |
| if univ[y][x]: dec n | |
| utmp[y][x] = n == 3 or (n == 2 and univ[y][x]) | |
| swap(univ,utmp) | |
| sleep 200 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment