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

Puzzle Game

Started by Conn332, 10 June 2013 - 04:36 PM
Conn332 #1
Posted 10 June 2013 - 06:36 PM
Hello Hackers!


I recently had the urge to make a puzzle/ game thingy and here it is!




How it works:
Spoiler

When you first start up the program it should prompt you what difficulty you want like this:


The next thing you would need to is enter a number
NOTE: When you enter a difficulty it is the size of the board and effects how many O's you have (3 = 3x3 1 O, 4 = 4x4 2 O's, etc)

The next thing that should happen is the board should appear on the screen like so:


When you click on an X or an O all the "tiles" next to it switch (X's to O's and vis versa), and the goal of the game is to make every "tile" an O


When this happens It means you've won! To exit click the screen.

Code:
Spoiler


local lights = {}
local function clear()
  term.clear()
  term.setCursorPos(1,1)
end
local function getNum(num)
  return math.floor((num+1)/2)
end
clear()
write "Enter Difficulty (minimum of 3): "
d = tonumber(read())
for i=1, d*d do
  lights[i] = "false"
end
for i=1, d/3+d%3 do
  lights[math.random(d*d)] = "true"
end
clear()
function drawLights()
  for i=1, d do
	for a=1, d do
	  term.setCursorPos((a*2)-1,i)
	  if lights[(i*d)-(d-a)] == "false" then
		write("X")
	  else
		write"O"
	  end
	end
  end
end
drawLights()
local function getCoor()
  return (y*d)-(d-x)
end
local function setLights(lightNum)
  if lights[lightNum] == "true" then
	lights[lightNum] = "false"
  else
	lights[lightNum] = "true"
  end
end
while true do
  event, mouse, x, y = os.pullEvent("mouse_click")
  term.setCursorPos(1,7)
  x = getNum(x)
  setLights(getCoor())
  if y ~= 1 then
	setLights(getCoor()-d)
  end
  if y < d then
	setLights(getCoor()+d)
  end
  if x < d then
	setLights(getCoor()+1)
  end
  if x ~= 1 then
	setLights(getCoor()-1)
  end
  drawLights()
  local test = false
  for i=1, #lights do
	if lights[i] == "false" then
	  test = true
	end
  end
  if test == false then
	break
  end
end
term.setTextColor(colors.yellow)
drawLights()
os.pullEvent()
clear()

Download:
Spoiler

Type this into a computer:

pastebin get 5DckaFqd<filename>
or here is the link to pastebin
ETHANATOR360 #2
Posted 10 June 2013 - 06:53 PM
looks cool keep up the great work!