Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --tic tac toe
- ----SETTINGS----
- size=9 --size of field
- len=5 --length of line which you must solve
- ----------------
- tex={
- x={"▄ ▄",
- " ▀▄▀ ",
- "▄▀ ▀▄",
- },
- o={" ▄ ",
- "▄▀ ▀▄",
- " ▀▄▀ "
- }
- }
- isEnded=false
- turn="x"
- nextTurn={x="o",o="x"}
- local event=require("event")
- local gpu=require("component").gpu
- map={}
- for i=1,size*size do
- map[i]=" "
- end
- function toXY(num)
- local y=math.floor((num-1)/size)+1
- return num-(y-1)*size,y
- end
- function fromXY(x,y)
- return (y-1)*size+x
- end
- function win(s)
- gpu.set(90,1,"WIN: "..s)
- isEnded=true
- end
- function draw(name,x,y)
- if name=="x" then
- gpu.setForeground(0x0000ff)
- elseif name=="o" then
- gpu.setForeground(0xff0000)
- else
- return
- end
- for i=1,3 do
- gpu.set(x,y+i-1,tex[name][i])
- end
- gpu.setForeground(0xffffff)
- end
- function drawField()
- for y=1,size do
- if y~=size then
- gpu.set(1,y*4,string.rep("▄",8*size-1))
- -- gpu.set(y*9,1,string.rep("█",4*size),true)
- end
- for x=1,size do
- draw(map[fromXY(x,y)],(x-1)*8+2,(y-1)*4+1)
- end
- end
- for y=1,size-1 do
- gpu.set(y*8,1,string.rep("█",4*size),true)
- end
- end
- require("term").clear()
- drawField()
- --os.exit()
- function onClick(_,_,x,y,_,name)
- if isEnded then
- return false
- end
- xC=math.floor(x/8)+1
- yC=math.floor((y-1)/4)+1
- -- gpu.set(1,1,xC.." "..yC)
- if (xC>size)or(yC>size) then
- return
- end
- num=fromXY(xC,yC)
- if map[num]~=" " then
- return
- end
- map[num]=turn
- for i=1,size do
- for j=1,size do
- if (i<=size-len+1)and(j<=size-len+1) then
- bool1,bool2=true,true
- s=map[fromXY(i,j)]
- s2=map[fromXY(i+len-1,j)]
- if s==" " then
- bool1=false
- end
- if s2==" " then
- bool2=false
- end
- for x=0,len-1 do
- if map[fromXY(i+x,j+x)]~=s then
- bool1=false
- end
- -- io.write(i+len-x-1,j+x,map[fromXY(i+len-x-1,j+x)]," ")
- if map[fromXY(i+len-x-1,j+x)]~=s2 then
- bool2=false
- end
- end
- if (bool1)or(bool2) then
- win((bool1 and s)or(bool2 and s2))
- break
- end
- end
- if i<=size-len+1 then
- bool=true
- s=map[fromXY(i,j)]
- if s==" " then
- bool=false
- end
- for x=0,len-1 do
- if map[fromXY(i+x,j)]~=s then
- bool=false
- break
- end
- end
- if bool then
- win(s)
- break
- end
- end
- if j<=size-len+1 then
- bool=true
- s=map[fromXY(i,j)]
- if s==" " then
- bool=false
- end
- for x=0,len-1 do
- if map[fromXY(i,j+x)]~=s then
- bool=false
- break
- end
- end
- if bool then
- win(s)
- break
- end
- end
- end
- end
- turn=nextTurn[turn]
- drawField()
- -- gpu.set(1,1,turn)
- end
- event.listen("touch",onClick)
- while true do
- _,_,_,_,name=event.pull("key_down")
- if name=="astral17" then
- break
- end
- end
- event.ignore("touch",onClick)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement