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

TicTacToe

Started by ZKlack, 23 May 2015 - 08:06 PM
ZKlack #1
Posted 23 May 2015 - 10:06 PM
sorry if you found my English bad, I'm Saudi

what will my program do?
it will simply draw TicTacToe and allow you to play with your friend
V1.2 fixed the a bug that if there is no winner you can't do any thing
but now it will end the game if there is no winner
and every time the game ends it will ask you if you want to play or not

setup:
put a computer
put an advanced monitor on the right side (V1.2 allow you to place the monitor any side you want)
type pastebin run VreVCci8

pastebin:
http://pastebin.com/VreVCci8

program:

mon=peripheral.find("monitor")
function DrawTTT()
  mon.setBackgroundColor(colors.blue)
  mon.clear()
  mon.setBackgroundColor(colors.white)
  for pos=1,5 do
	mon.setCursorPos(2,pos)
	mon.write("	 ")
  end
  mon.setBackgroundColor(colors.black)
  for pos=1,5 do
	mon.setCursorPos(3,pos)
	mon.write(" ")
  end
  for pos=1,5 do
	mon.setCursorPos(5,pos)
	mon.write(" ")
  end
  mon.setCursorPos(2,2)
  mon.write("	 ")
  mon.setCursorPos(2,4)
  mon.write("	 ")
end
function play()
  Z=true
  mon.setBackgroundColor(colors.white)
  mon.setTextColor(colors.black)
  while Z do
	event, Ts, Tx, Ty=os.pullEvent(monitor_touch)
	if Tx==2 and Ty==1 and L1==" " then
	  L1=player
	  mon.setCursorPos(2,1)
	  mon.write(player)
	  Z=false
	elseif Tx==4 and Ty==1 and L2==" " then
	  L2=player
	  mon.setCursorPos(4,1)
	  mon.write(player)
	  Z=false
	elseif Tx==6 and Ty==1 and L3==" " then
	  L3=player
	  mon.setCursorPos(6,1)
	  mon.write(player)
	  Z=false
	elseif Tx==2 and Ty==3 and L4==" " then
	  L4=player
	  mon.setCursorPos(2,3)
	  mon.write(player)
	  Z=false
	elseif Tx==4 and Ty==3 and L5==" " then
	  L5=player
	  mon.setCursorPos(4,3)
	  mon.write(player)
	  Z=false
	elseif Tx==6 and Ty==3 and L6==" " then
	  L6=player
	  mon.setCursorPos(6,3)
	  mon.write(player)
	  Z=false
	elseif Tx==2 and Ty==5 and L7==" " then
	  L7=player
	  mon.setCursorPos(2,5)
	  mon.write(player)
	  Z=false
	elseif Tx==4 and Ty==5 and L8==" " then
	  L8=player
	  mon.setCursorPos(4,5)
	  mon.write(player)
	  Z=false
	elseif Tx==6 and Ty==5 and L9==" " then
	  L9=player
	  mon.setCursorPos(6,5)
	  mon.write(player)
	  Z=false
	end
	os.sleep(1)
  end
end
function check()
  mon.setBackgroundColor(colors.lime)
  Won=true
  if L1==player and L2==player and L3==player then
	mon.setCursorPos(2,1)
	mon.write("	 ")
	playing=false
  elseif L4==player and L5==player and L6==player then
	mon.setCursorPos(2,3)
	mon.write("	 ")
	playing=false
  elseif L7==player and L8==player and L9==player then
	mon.setCusorPos(2,5)
	mon.write("	 ")
	playing=false
  elseif L1==player and L4==player and L7==player then
	for pos=1,5 do
	  mon.setCursorPos(2,pos)
	  mon.write(" ")
	end
	playing=false
  elseif L2==player and L5==player and L8==player then
	for pos=1,5 do
	  mon.setCursorPos(4,pos)
	  mon.write(" ")
	end
	playing=false
  elseif L3==player and L6==player and L9==player then
	for pos=1,5 do
	  mon.setCursorPos(6,pos)
	  mon.write(" ")
	end
	playing=false
  elseif L1==player and L5==player and L9==player then
	for pos=1,5 do
	  mon.setCursorPos(pos+1,pos)
	  mon.write(" ")
	end
	playing=false
  elseif L3==player and L5==player and L7==player then
	for pos=0,4 do
	  mon.setCursorPos(6-pos,pos+1)
	  mon.write(" ")
	end
	playing=false
  else
	Won=false
  end
  if Won then
	os.pullEvent(monitor_touch)
  end
end
CP=true
while CP do
  mon.setTextScale(1)
  DrawTTT()
  player="X"
  L1=" "
  L2=" "
  L3=" "
  L4=" "
  L5=" "
  L6=" "
  L7=" "
  L8=" "
  L9=" "
  playing=true
  while playing do
	play()
	check()
	if player=="X" then
	  player="O"
	elseif player=="O" then
	  player="X"
	end
	if L1==" " or L2==" " or L3==" " or L4==" " or L5==" " or L6==" " or L7==" " or L8==" " or L9==" " then
	elseif not Won then
	  playing=false
	end
  end
  if Won then
	if player=="X" then
	  player="O"
	elseif player=="O" then
	  player="X"
	end
	mon.setBackgroundColor(colors.black)
	mon.setTextColor(colors.white)
	mon.clear()
	mon.setCursorPos(1,1)
	mon.write("player")
	mon.write(player)
	mon.setCursorPos(3,3)
	mon.write("WON")
	mon.setCursorPos(1,5)
	mon.write("TheGame")
  end
  os.pullEvent(monitor_touch)
  mon.setBackgroundColor(colors.black)
  mon.setTextColor(colors.white)
  mon.clear()
  mon.setCursorPos(2,1)
  mon.write("Play?")
  mon.setCursorPos(1,3)
  mon.write("yes")
  mon.setCursorPos(1,5)
  mon.write("no")
  mon.setBackgroundColor(colors.lime)
  mon.setCursorPos(5,3)
  mon.write("  ")
  mon.setBackgroundColor(colors.red)
  mon.setCursorPos(5,5)
  mon.write("  ")
  click=false
  while not click do
	event, Ts, Tx, Ty=os.pullEvent(monitor_touch)
	if Ty==3 then
	  if Tx==5 or Tx==6 then
		click=true
		CP=true
	  end
	elseif Ty==5 then
	  if Tx==5 or Tx==6 then
		click=true
		CP=false
	  end
	end
  end
  if not CP then
	mon.setBackgroundColor(colors.black)
	mon.clear()
  end
end

special thanks to flaghacker * he helped me a lot in using the monitors
I don't know how mention his name because I'm new * please tell me how
this two weeks I have exams so I will upgrade that to play one player later
Edited on 24 May 2015 - 07:12 AM
Bomb Bloke #2
Posted 24 May 2015 - 05:57 AM
If you have at least ComputerCraft 1.6, instead of this:

mon=peripheral.wrap("right")

… you could use peripheral.find():

mon=peripheral.find("monitor")

This would look for and wrap the first available monitor, so it doesn't "have" to be on the right side.

You could, if you wanted to, shrink your script down a lot by using tables.

Kinda neat that it can run on a single monitor block.
TheOddByte #3
Posted 06 June 2015 - 06:42 PM
Pretty cool, I'd suggest you add so that it checks that a monitor is actually attached

local mon = peripheral.find( "monitor" )
if not mon then
    error( "No monitor attached", 0 )
end
And the suggestion by BombBloke about tables is a really good idea, it could save you from a lot of trouble.

I just had to
Kinda …
Kind of*
I know it's a slang, but I just had to harass you for it, as my teacher did to me :P/>