-
-
Save dom96/31f17b06874569a00108311a4c7a9139 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 terminal, os | |
| type Board = array[-1 .. 40, array[-1 .. 20, bool]] | |
| proc initBoard: Board = | |
| result[20][8] = true | |
| result[21][9] = true | |
| result[19][10] = true | |
| result[20][10] = true | |
| result[21][10] = true | |
| proc countLives(board: Board, x, y: int): int = | |
| result = if board[x][y]: -1 else: 0 | |
| for nx in (x - 1) .. (x + 1): | |
| for ny in (y - 1) .. (y + 1): | |
| if board[nx][ny]: inc result | |
| proc tick(board: var Board) = | |
| var copy = board | |
| deepCopy(copy, board) # REMOVE THIS TO GET A WORKING VERSION | |
| for x in 0..<board.high: | |
| for y in 0..<board[0].high: | |
| let neighbours = countLives(copy, x, y) | |
| let alive = copy[x][y] | |
| if not alive and neighbours == 3: | |
| board[x][y] = true | |
| elif alive and neighbours in {0 .. 8} - {2, 3}: | |
| board[x][y] = false | |
| proc draw(board: Board) = | |
| stdout.eraseScreen() | |
| for x in 0..<board.high: | |
| for y in 0..<board[0].high: | |
| setCursorPos(stdout, x, y) | |
| if board[x][y]: | |
| stdout.write "●" | |
| else: | |
| stdout.write " " | |
| stdout.flushFile | |
| var board = initBoard() | |
| while true: | |
| board.draw() | |
| board.tick() | |
| sleep(200) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment