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

Simon Says (with custom controller!)

Started by Jahmaican, 27 August 2012 - 10:00 AM
Jahmaican #1
Posted 27 August 2012 - 12:00 PM
Hi! Today I was experimenting with bundled redstone so I made a fun little game for you to enjoy. Safe to say the title says all about it.

This is how it looks:

(notice the lamps can't be placed side-by-side as the buttons would activate three at once)

And here's the connection:

(of course you can shorten the cables, I made it this way just to make it clear)

Now to the programming part. Start with saving this as api/devices.

function find(name,prior)
  local sides = {
  "top";
  "front";
  "left";
  "right";
  "back";
  "bottom" }

  if prior==nil then
	p=1
  else
	p=prior
  end

  for n=1,6 do
	if peripheral.getType(sides[n])==name then
	  if p==1 then
		return sides[n]
	  else
		p=p-1
	  end
	end
  end
  return "none"
end

And the game itself:

-- CONFIG --

lvs=10	-- number of levels to win
cside="back"  -- where the bundled cable is connected

-- END OF CONFIG --

os.loadAPI("api/devices")
level=1
order={}
answer={}

mside=devices.find("monitor")
mon=peripheral.wrap(mside)

function Main()
  term.clear()
  term.setCursorPos(1,1)

  print("Simon Says v1.1nby JahmaicannnThe goal of the game is to repeat every Simon's move.nMemorize lights order and repeat it using corresponding buttons when "your turn" message shows on the monitor.nPressing the buttons while "wait" message is shown doesn't count as this is used to compensate the input's inertion.nNumber of moves to repeat is equal to level number.nn[Press ESC to exit the terminal]")

  mon.clear()
  mon.setCursorPos(2,2)
  mon.write("SIMON SAYS by Jahmaican")
  mon.setCursorPos(2,4)
  mon.write("[Green] to start")
  mon.setCursorPos(2,5)
  mon.write("[Blue] to exit")

  os.pullEvent("redstone")
  local col=rs.getBundledInput(cside)

  if col==colors.green then
	Start()
  elseif col==colors.blue then
	Exit()
  else
	Main()
  end
end

function Start()
  mon.clear()
  mon.setCursorPos(2,2)
  mon.write("Level "..level)

  Randomize()
  Display()
  Repeat()
end

function Randomize()
  for i=1,level do
	order[i]=math.random(4)
  end
end

function Display()
  mon.setCursorPos(2,4)
  mon.clearLine()

  sleep(3)
  mon.write("Simon's turn...")

  sleep(0.5)
  for i=1,level do
	if order[i]==1 then
	  rs.setBundledOutput(cside,colors.green)
	elseif order[i]==2 then
	  rs.setBundledOutput(cside,colors.red)
	elseif order[i]==3 then
	  rs.setBundledOutput(cside,colors.yellow)
	elseif order[i]==4 then
	  rs.setBundledOutput(cside,colors.blue)
	end
  
	sleep(0.7)
	rs.setBundledOutput(cside,0)
	sleep(0.3)
  end
  sleep(0.1)	
end

function Repeat()  
  for i=1,level do
	mon.setCursorPos(2,4)
	mon.clearLine()
	mon.write("Your turn!")
    os.pullEvent("redstone")
	
	local col=rs.getBundledInput(cside)
	if col==colors.green then
	  answer[i]=1
	elseif col==colors.red then
	  answer[i]=2
	elseif col==colors.yellow then
	  answer[i]=3
	elseif col==colors.blue then
	  answer[i]=4
	end
  
	mon.setCursorPos(2,4)
	mon.clearLine()
	mon.write("Wait...")   -- this is a very important part as it makes the program avoid
	sleep(1)	  -- intercepting the "redstone" event when signal goes low after
		 -- the button is released plus the redstone itself has its own
		 -- inertion so just wait when it tells you before hitting the next button
  end
  
  mist=0
  for i=1,level do
	if answer[i]~=order[i] then
	  mist=mist+1
	end
  end

  if mist==0 then
	Correct()
  else
	Incorrect()
  end
end

function Correct()

  if level==lvs then
	mon.clear()
	mon.setCursorPos(2,2)
	mon.write("CONGRATS!")
	mon.setCursorPos(2,4)
	mon.write("Simon was beaten!")
	return 0
  else
	mon.setCursorPos(2,4)
	mon.clearLine()
	mon.write("OK!")
	sleep(0.5)
	level=level+1
	Start()
  end  
end

function Incorrect()
  mon.clear()
  mon.setCursorPos(2,2)
  mon.write("GAME OVER")
  mon.setCursorPos(2,4)
  mon.write("Simon beat you at lvl "..level.."!")
  return 0
end
function Exit()
  mon.clear()
  mon.setCursorPos(2,3)
  mon.write("kthxbye!")
  return 0
end

Main()

Once you run the program it tells you what to do next :D/>/>
As usual, I hope you like it. Sorry if I made any grammar mistakes but you probably already noticed English isn't my native language and also today I suffer from a bad hangover.

Edit: Minor changes
Edited on 11 September 2012 - 08:54 PM
PixelToast #2
Posted 05 December 2012 - 01:57 PM
id probably check it out but i dont have redpower .-.
i have a simon says in the basement somewhere
Cranium #3
Posted 06 December 2012 - 05:37 AM
One problem wit your redpower bundled cable. The cable does have to terminate at the computer, not run over it. You are getting no signal to/from the wires at the moment…