This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
bobmandude9889's profile picture

Conways game of life

Started by bobmandude9889, 06 March 2014 - 04:48 AM
bobmandude9889 #1
Posted 06 March 2014 - 05:48 AM
I started making conways game of life in CC but I have a problem. When I click the pixel at cords (11,1) it also puts a pixel at (1,11) and same for every pixel down that column. It also does it every 11 pixels on the x axis but instead of (22,1) going to (1,22) it goes to (2,11) and (33,1) goes to (3,11) etc. Please help! Thank you! :D/>


cells = {}
function alive(x,y)
if cells[tostring(x)..tostring(y)] == "alive" or cells[tostring(x)..tostring(y)] == "will_die" then
  return true
elseif cells[tostring(x)..tostring(y)] == "dead" or cells[tostring(x)..tostring(y)] == "will_live" then
  return false
end
end
function drawScreen()
for y = 1,19 do
  for x = 1,51 do
   term.setCursorPos(x,y)
   if alive(x,y) then
	term.setBackgroundColor(colors.white)
   else
	term.setBackgroundColor(colors.black)
   end
   write(" ")
  end
end
end
local running = false
while true do
os.startTimer(1)
local e,p1,p2,p3 = os.pullEvent()
if e == "mouse_click" and not running then
  if p1 == 2 then
   cells[tostring(p2)..tostring(p3)] = "dead"
  else
   cells[tostring(p2)..tostring(p3)] = "alive"
  end
  drawScreen()
elseif e == "key" and keys.getName(p1) == "space" then
  if running then
   running = false
  else
   running = true
  end
elseif e == "timer" and running then
  term.setCursorPos(1,1)
  print("test")
end
end
Lignum #2
Posted 06 March 2014 - 10:18 AM
The only thing that I see is wrong on the first look is how you're storing the cells (which might actually cause the problem).
You should use a two-dimensional table for this. Here's an example:

local cells = {}
local width,height = term.getSize()

for x=1,width do
	cells[x] = {}
	for y=1,height do
		cells[x][y] = false
	end
end

-- All cells are now initialized to 'dead' (false).

-- We've got a 2D table of cells now.
-- You can access one with cells[xCoord][yCoord].
-- It'll return true is the cell is alive and false if it's dead.
-- Example of a mouse event:

local e,p1,p2,p3,p4,p5 = os.pullEvent()
if e == "mouse_click" then
	cells[p2][p3] = true -- Sets the cell at p2, p3 as alive.
end

-- Checking whether a certain cell is alive:
if cells[5][3] then
	print("Cell at x: 5 and y: 3 is alive!")
end