Advertisement
osmarks

GoLboard

Feb 21st, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local a=http.get"https://pastebin.com/raw/ujchRSnU"local b=fs.open("blittle","w")b.write(a.readAll())a.close()b.close()
  2.  
  3. os.loadAPI "blittle" -- evil but necessary
  4.  
  5. local function make_board(w, h)
  6.     local board = {}
  7.     for x = 0, w do
  8.         board[x] = {}
  9.         for z = 0, h do
  10.             local pick = false
  11.             if math.random() < 0.5 then pick = true end
  12.             board[x][z] = pick
  13.         end
  14.     end
  15.     board.width = w
  16.     board.height = h
  17.     return board
  18. end
  19.  
  20. local function wrap(n, max)
  21.     return n % max
  22. end
  23.  
  24. local function get_neighbours(board, x, y, w, h)
  25.     local total = 0
  26.     for dx = -1, 1 do
  27.         for dy = -1, 1 do
  28.             if not (dx == 0 and dy == 0) then
  29.                 local thing = 0
  30.                 if board[wrap(x + dx, w)][wrap(y + dy, h)] then thing = 1 end
  31.                 total = total + thing
  32.             end
  33.         end
  34.     end
  35.     return total
  36. end
  37.  
  38. local function update(board, new_board)
  39.     for x = 0, board.width do
  40.         for y = 0, board.height do
  41.             local alive_now = board[x][y]
  42.             local alive_next
  43.  
  44.             local neighbours = get_neighbours(board, x, y, board.width, board.height)
  45.  
  46.             if alive_now then
  47.                 alive_next = neighbours == 2 or neighbours == 3
  48.             else
  49.                 alive_next = neighbours == 3
  50.             end
  51.  
  52.             new_board[x][y] = alive_next
  53.         end
  54.     end
  55.     return new_board
  56. end
  57.  
  58. local blterm = blittle.createWindow(term.current(), 1, 1, raww, rawh, false)
  59.  
  60. local function draw(board)
  61.     blterm.setVisible(false)
  62.     for x = 0, board.height do
  63.         blterm.setCursorPos(1, x)
  64.         local cols = ""
  65.         for z = 0, board.width do
  66.             local color = colors.black
  67.             if board[z][x] then cols = cols .. "0"
  68.             else cols = cols .. "f" end
  69.         end
  70.         blterm.blit(nil, nil, cols)
  71.     end
  72.     blterm.setVisible(true)
  73. end
  74.  
  75. local w, h = blterm.getSize()
  76. local b1, b2 = make_board(w, h), make_board(w, h)
  77. local gens = 0
  78. while true do
  79.     draw(b1)
  80.     update(b1, b2)
  81.     b1, b2 = b2, b1
  82.     gens = gens + 1
  83.     if gens % 100 == 0 then b1 = make_board(w, h) end
  84.     sleep(0.1)
  85. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement