View difference between Paste ID: wAwrD3MQ and dQUVTjft
SHOW: | | - or go back to the newest paste.
1-
--tic tac toe
1+
--tic tac toe
2-
tex={
2+
----SETTINGS----
3-
 x={"▄   ▄",
3+
size=9 --size of field
4-
    " ▀▄▀ ",
4+
len=5  --length of line which you must solve
5-
    "▄▀ ▀▄",
5+
----------------
6-
 },
6+
7-
 o={"  ▄  ",
7+
tex={
8-
    "▄▀ ▀▄",
8+
 x={"▄   ▄",
9-
    " ▀▄▀ "
9+
    " ▀▄▀ ",
10-
 }
10+
    "▄▀ ▀▄",
11-
}
11+
 },
12-
isEnded=false
12+
 o={"  ▄  ",
13-
turn="x"
13+
    "▄▀ ▀▄",
14-
nextTurn={x="o",o="x"}
14+
    " ▀▄▀ "
15-
local event=require("event")
15+
 }
16-
local gpu=require("component").gpu
16+
}
17-
map={}
17+
isEnded=false
18-
size=9
18+
turn="x"
19-
len=5
19+
nextTurn={x="o",o="x"}
20-
for i=1,size*size do
20+
local event=require("event")
21-
  map[i]=" "
21+
local gpu=require("component").gpu
22-
end
22+
map={}
23-
function toXY(num)
23+
24-
  local y=math.floor((num-1)/size)+1
24+
for i=1,size*size do
25-
  return num-(y-1)*size,y
25+
  map[i]=" "
26-
end
26+
end
27-
function fromXY(x,y)
27+
function toXY(num)
28-
  return (y-1)*size+x
28+
  local y=math.floor((num-1)/size)+1
29-
end
29+
  return num-(y-1)*size,y
30-
function win(s)
30+
end
31-
  gpu.set(90,1,"WIN: "..s)
31+
function fromXY(x,y)
32-
  isEnded=true
32+
  return (y-1)*size+x
33-
end
33+
end
34-
function draw(name,x,y)
34+
function win(s)
35-
  if name=="x" then
35+
  gpu.set(90,1,"WIN: "..s)
36-
    gpu.setForeground(0x0000ff)
36+
  isEnded=true
37-
  elseif name=="o" then
37+
end
38-
    gpu.setForeground(0xff0000)
38+
function draw(name,x,y)
39-
  else
39+
  if name=="x" then
40-
    return
40+
    gpu.setForeground(0x0000ff)
41-
  end
41+
  elseif name=="o" then
42-
  for i=1,3 do
42+
    gpu.setForeground(0xff0000)
43-
    gpu.set(x,y+i-1,tex[name][i])
43+
  else
44-
  end
44+
    return
45-
  gpu.setForeground(0xffffff)
45+
  end
46-
end
46+
  for i=1,3 do
47-
function drawField()
47+
    gpu.set(x,y+i-1,tex[name][i])
48-
  for y=1,size do
48+
  end
49-
    if y~=size then
49+
  gpu.setForeground(0xffffff)
50-
      gpu.set(1,y*4,string.rep("▄",8*size-1))
50+
end
51-
--      gpu.set(y*9,1,string.rep("█",4*size),true)
51+
function drawField()
52-
    end
52+
  for y=1,size do
53-
    for x=1,size do
53+
    if y~=size then
54-
      draw(map[fromXY(x,y)],(x-1)*8+2,(y-1)*4+1)
54+
      gpu.set(1,y*4,string.rep("▄",8*size-1))
55-
    end
55+
--      gpu.set(y*9,1,string.rep("█",4*size),true)
56-
  end
56+
    end
57-
  for y=1,size-1 do
57+
    for x=1,size do
58-
      gpu.set(y*8,1,string.rep("█",4*size),true)
58+
      draw(map[fromXY(x,y)],(x-1)*8+2,(y-1)*4+1)
59-
  end
59+
    end
60-
end
60+
  end
61-
require("term").clear()
61+
  for y=1,size-1 do
62-
drawField()
62+
      gpu.set(y*8,1,string.rep("█",4*size),true)
63-
--os.exit()
63+
  end
64-
64+
end
65-
function onClick(_,_,x,y,_,name)
65+
require("term").clear()
66-
  if isEnded then
66+
drawField()
67-
    return false
67+
--os.exit()
68-
  end
68+
69-
  xC=math.floor(x/8)+1
69+
function onClick(_,_,x,y,_,name)
70-
  yC=math.floor((y-1)/4)+1
70+
  if isEnded then
71-
--  gpu.set(1,1,xC.." "..yC)
71+
    return false
72-
  if (xC>size)or(yC>size) then
72+
  end
73-
    return
73+
  xC=math.floor(x/8)+1
74-
  end
74+
  yC=math.floor((y-1)/4)+1
75-
  num=fromXY(xC,yC)
75+
--  gpu.set(1,1,xC.." "..yC)
76-
  if map[num]~=" " then
76+
  if (xC>size)or(yC>size) then
77-
    return
77+
    return
78-
  end
78+
  end
79-
  map[num]=turn
79+
  num=fromXY(xC,yC)
80-
  for i=1,size do
80+
  if map[num]~=" " then
81-
    for j=1,size do
81+
    return
82-
      if (i<=size-len+1)and(j<=size-len+1) then
82+
  end
83-
        bool1,bool2=true,true
83+
  map[num]=turn
84-
        s=map[fromXY(i,j)]
84+
  for i=1,size do
85-
        s2=map[fromXY(i+len-1,j)]
85+
    for j=1,size do
86-
        if s==" " then
86+
      if (i<=size-len+1)and(j<=size-len+1) then
87-
          bool1=false
87+
        bool1,bool2=true,true
88-
        end
88+
        s=map[fromXY(i,j)]
89-
        if s2==" " then
89+
        s2=map[fromXY(i+len-1,j)]
90-
          bool2=false
90+
        if s==" " then
91-
        end
91+
          bool1=false
92-
        for x=0,len-1 do
92+
        end
93-
          if map[fromXY(i+x,j+x)]~=s then
93+
        if s2==" " then
94-
            bool1=false
94+
          bool2=false
95-
          end
95+
        end
96-
--          io.write(i+len-x-1,j+x,map[fromXY(i+len-x-1,j+x)]," ")
96+
        for x=0,len-1 do
97-
          if map[fromXY(i+len-x-1,j+x)]~=s2 then
97+
          if map[fromXY(i+x,j+x)]~=s then
98-
            bool2=false
98+
            bool1=false
99-
          end
99+
          end
100-
        end
100+
--          io.write(i+len-x-1,j+x,map[fromXY(i+len-x-1,j+x)]," ")
101-
        if (bool1)or(bool2) then
101+
          if map[fromXY(i+len-x-1,j+x)]~=s2 then
102-
          win((bool1 and s)or(bool2 and s2))
102+
            bool2=false
103-
          break
103+
          end
104-
        end
104+
        end
105-
      end
105+
        if (bool1)or(bool2) then
106-
      if i<=size-len+1 then
106+
          win((bool1 and s)or(bool2 and s2))
107-
        bool=true
107+
          break
108-
        s=map[fromXY(i,j)]
108+
        end
109-
        if s==" " then
109+
      end
110-
          bool=false
110+
      if i<=size-len+1 then
111-
        end
111+
        bool=true
112-
        for x=0,len-1 do
112+
        s=map[fromXY(i,j)]
113-
          if map[fromXY(i+x,j)]~=s then
113+
        if s==" " then
114-
            bool=false
114+
          bool=false
115-
            break
115+
        end
116-
          end
116+
        for x=0,len-1 do
117-
        end
117+
          if map[fromXY(i+x,j)]~=s then
118-
        if bool then
118+
            bool=false
119-
          win(s)
119+
            break
120-
          break
120+
          end
121-
        end
121+
        end
122-
      end
122+
        if bool then
123-
      if j<=size-len+1 then
123+
          win(s)
124-
        bool=true
124+
          break
125-
        s=map[fromXY(i,j)]
125+
        end
126-
        if s==" " then
126+
      end
127-
          bool=false
127+
      if j<=size-len+1 then
128-
        end
128+
        bool=true
129-
        for x=0,len-1 do
129+
        s=map[fromXY(i,j)]
130-
          if map[fromXY(i,j+x)]~=s then
130+
        if s==" " then
131-
            bool=false
131+
          bool=false
132-
            break
132+
        end
133-
          end
133+
        for x=0,len-1 do
134-
        end
134+
          if map[fromXY(i,j+x)]~=s then
135-
        if bool then
135+
            bool=false
136-
          win(s)
136+
            break
137-
          break
137+
          end
138-
        end
138+
        end
139-
      end
139+
        if bool then
140-
    end
140+
          win(s)
141-
  end
141+
          break
142-
  turn=nextTurn[turn]
142+
        end
143-
  drawField()
143+
      end
144-
--  gpu.set(1,1,turn)
144+
    end
145-
end
145+
  end
146-
146+
  turn=nextTurn[turn]
147-
event.listen("touch",onClick)
147+
  drawField()
148-
while true do
148+
--  gpu.set(1,1,turn)
149-
  _,_,_,_,name=event.pull("key_down")
149+
end
150-
  if name=="astral17" then
150+
151-
    break
151+
event.listen("touch",onClick)
152-
  end
152+
while true do
153-
end
153+
  _,_,_,_,name=event.pull("key_down")
154
  if name=="astral17" then
155
    break
156
  end
157
end
158
event.ignore("touch",onClick)