Last active
March 7, 2019 02:21
-
-
Save zetashift/7f9c180f3025341a18483688d88ddca9 to your computer and use it in GitHub Desktop.
Implementation of the business card generator in Nim
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
| # A quick Nim translation of https://gist.github.com/munificent/b1bcd969063da3e6c298be070a22b604 | |
| # With some help of the JS version | |
| import random | |
| const | |
| HEIGHT = 40 | |
| WIDTH = 80 | |
| PLAYER = '@' | |
| TREASURE = '$' | |
| VOID = ' ' | |
| CORNER = '!' | |
| WALL = '#' | |
| FLOOR = '.' | |
| DOOR1 = '+' | |
| DOOR2 = '\\' | |
| var | |
| map: seq[seq[char]] = newSeq[seq[char]](HEIGHT) | |
| proc startMap() = | |
| for y in 0 ..< map.len: | |
| map[y] = newSeq[char](WIDTH) | |
| for x in 0 ..< map.len: | |
| map[y][x] = VOID | |
| proc cave(start: bool) = | |
| randomize() | |
| let | |
| width = rand(10 ..< 15) | |
| height = rand(4 ..< 12) | |
| left = rand(WIDTH - width - 3) + 1 | |
| top = rand(HEIGHT - height - 3) + 1 | |
| for y in top-1 ..< top+height+2: | |
| for x in left-1 ..< left+width+2: | |
| if map[y][x] == FLOOR: return | |
| var | |
| doors = 0 | |
| doorX: int | |
| doorY: int | |
| if not start: | |
| for y in top-1 ..< top+height+2: | |
| for x in left-1 ..< left+width+2: | |
| let s = x < left or x > left + width | |
| let t = y < top or y > top + height | |
| let q = s != t | |
| if q and map[y][x] == WALL: | |
| inc(doors) | |
| if rand(0 ..< doors) == 0: | |
| doorX = x | |
| doorY = y | |
| if doors == 0: return | |
| for y in top-1 ..< top + height + 2: | |
| for x in left-1 ..< left + width + 2: | |
| let s = x < left or x > left + width | |
| let t = y < top or y > top + height | |
| map[y][x] = if s and t: | |
| CORNER | |
| elif s xor t: | |
| WALL | |
| else: FLOOR | |
| if doors > 0: | |
| let typeDoor = rand(10) | |
| let kind = if typeDoor > 5: DOOR2 else: DOOR1 | |
| map[doorY][doorX] = kind | |
| let go = if start: 1 else: rand(6)+1 | |
| for j in 0 ..< go: | |
| map[rand(height) + top][rand(width)+left] = if start: PLAYER | |
| else: | |
| if rand(4) == 0: | |
| TREASURE | |
| else: | |
| (rand(62) + 65).char | |
| proc generate() = | |
| for j in 0 ..< 1000000: | |
| cave(j == 0) | |
| when isMainModule: | |
| startMap() | |
| generate() | |
| for y in 0 ..< HEIGHT: | |
| for x in 0 ..< WIDTH: | |
| let c = map[y][x] | |
| let dungeon = if c == CORNER: WALL else: c | |
| stdout.write(dungeon) | |
| if x == WIDTH-1: | |
| echo("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment