colors = [color.black, color.yellow, color.aqua, color.red] deltas = [[-1,-1], [-1,0], [-1,1], [ 0,-1], [ 0,1], [ 1,-1], [ 1,0], [ 1,1]] displayGrid = function(grid, td) for y in range(0, grid.len - 1) for x in range(0, grid[0].len - 1) td.setCell x,y, grid[y][x] end for end for end function buildGrid = function(s) lines = s.split(char(13)) nRows = lines.len nCols = 0 for line in lines if line.len > nCols then nCols = line.len end for grid = [] emptyRow = [] for c in range(1,nCols) emptyRow.push(0) end for for line in lines row = emptyRow[:] for i in range(0, line.len - 1) row[i] = " .Ht".indexOf(line[i]) end for grid.push(row) end for return grid end function getNewState = function(td, x, y) cellState = td.cell(x, y) if cellState == 3 then return 1 else if cellState == 2 then return 3 else if cellState == 1 then sum = 0 for delta in deltas x1 = x + delta[0] y1 = y + delta[1] if td.cell(x1, y1) == 2 then sum += 1 end for if sum == 1 or sum == 2 then return 2 else return 1 end if end if return cellState end function clear wireWorldProgram = "tH........." + char(13) wireWorldProgram += ". ." + char(13) wireWorldProgram += " ..." + char(13) wireWorldProgram += ". ." + char(13) wireWorldProgram += "Ht.. ......" grid = buildGrid(wireWorldProgram) // Prepare a tile display // Generate image used for the tiles from the colors defined above. img = Image.create(colors.len, 1); for i in range(0, colors.len - 1) img.setPixel(i, 0, colors[i]) end for cols = grid[0].len rows = grid.len display(4).mode = displayMode.tile td = display(4) cSize = 25 td.cellSize = cSize // size of cells on screen td.scrollX = -(960 - cols * (cSize + 1)) / 2 td.scrollY = -(640 - rows * (cSize + 1)) / 2 td.extent = [cols, rows] td.overlap = -1 // adds a small gap between cells td.tileSet = img; td.tileSetTileSize = 1 td.clear 0 while true displayGrid(grid, td) for y in range(0, rows - 1) for x in range(0, cols - 1) grid[y][x] = getNewState(td, x, y) end for end for wait 0.5 end while