Made with Computer Craft Mod, with allows write lua scripts in minecraft.
Last active
November 8, 2023 23:12
-
-
Save DanielAugusto191/91c68c70be2709607184a23e523aea1c to your computer and use it in GitHub Desktop.
Conway's Game of Life on Minecraft in lua with ComputerCraft
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
| local monitor = peripheral.wrap("right") | |
| monitor.clear() | |
| monitor.setCursorPos(1,1) | |
| monitor.setBackgroundColor(colors.black) | |
| local w,h = monitor.getSize() | |
| local sleepTime = 0.1 -- time between gen | |
| local M = {} -- Matrix of gen | |
| function draw() | |
| local oldMonitor = term.redirect(monitor) | |
| monitor.clear() | |
| monitor.setCursorPos(1,1) | |
| monitor.setBackgroundColor(colors.black) | |
| for i=1,h do | |
| for j=1,w do | |
| term.write((M[i][j][1] and "O" or "-")) | |
| M[i][j][0] = M[i][j][1] | |
| end | |
| term.setCursorPos(1, i+1) | |
| end | |
| term.redirect(oldMonitor) | |
| end | |
| function initial() | |
| math.randomseed(os.time()*1000) | |
| print("RNG seed: ", os.time()*1000) | |
| for i=1,h do | |
| M[i] = {} | |
| for j=1,w do | |
| M[i][j] = {} -- [0] current state, [1] next state | |
| M[i][j][0] = (math.random(2)%2==0) | |
| M[i][j][1] = M[i][j][0] | |
| end | |
| end | |
| draw() | |
| end | |
| function check(i, j) | |
| if i < 1 or j < 1 then | |
| return false | |
| end | |
| if i > h or j > w then | |
| return false | |
| end | |
| return true | |
| end | |
| function play() | |
| local gen = 1 | |
| while true do | |
| print("generation: ", gen) | |
| gen = gen + 1 | |
| for i=1,h do | |
| for j=1,w do | |
| local c = 0 | |
| local v = {-1, 0, 1} | |
| for k=1,3 do | |
| for l=1,3 do | |
| if check(i+v[k], j+v[l]) and (v[k] ~= 0 or v[l] ~= 0) then | |
| c = c + (M[i+v[k]][j+v[l]][0] and 1 or 0) | |
| end | |
| end | |
| end | |
| if M[i][j][0] then | |
| if c < 2 then | |
| M[i][j][1] = false | |
| end | |
| if c > 3 then | |
| M[i][j][1] = false | |
| end | |
| else | |
| if c == 3 then | |
| M[i][j][1] = true | |
| end | |
| end | |
| end | |
| end | |
| draw() | |
| os.sleep(0.1) | |
| end | |
| end | |
| initial() | |
| draw() | |
| print() | |
| print("Matrix of cells") | |
| function fprint() | |
| for i=1,h do | |
| for j=1,w do | |
| term.write((M[i][j][1] and 1 or 0)) | |
| end | |
| print() | |
| end | |
| end | |
| fprint() | |
| draw() | |
| local e = os.pullEvent( "redstone" ) | |
| if redstone.getInput( "left" ) then | |
| print("wait 2 sec...") | |
| os.sleep(2) | |
| play() | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment



