Created
January 15, 2017 19:44
-
-
Save inmatarian/25fff39f1f9a3b4027f51b8b3a12575e to your computer and use it in GitHub Desktop.
Tic Tac Toe in Love2d, 1k Challenge, Minified from 1347 to 948 bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
L=love;G=L.graphics;W=267;H=200;F=math.floor;L.load=function()K={0,0,0,0,0,0,0,0,0}end;B={1,3,7,9,5,2,4,6,8}L.mousepressed=function(x,y,a)if C(K,1)or C(K,2)then L.load()else i=F(y/H)*3+F(x/W)+1;if K[i]<1 then K[i]=1;if C(K,1)then return end;for i=1,9 do if K[i]<1 and C(K,2,i)then K[i]=2;return end end;for i=1,9 do if K[i]<1 and C(K,1,i)then K[i]=2;return end end;for i=1,9 do if K[B[i]]<1 then K[B[i]]=2;return end end else for i=1,9 do if K[i]<1 then return end end;L.load()end end end;R=function(b,c,x,y,d)return b[x]==c and b[y]==c and b[d]==c end;C=function(b,c,e)if e then b={unpack(b)}b[e]=c end;return R(b,c,1,2,3)or R(b,c,4,5,6)or R(b,c,7,8,9)or R(b,c,1,4,7)or R(b,c,2,5,8)or R(b,c,3,6,9)or R(b,c,1,5,9)or R(b,c,3,5,7)end;L.draw=function()for i=1,9 do x=(i-1)%3*W;y=F((i-1)/3)*H;G.line(x+W,0,x+W,y+H)G.line(0,y+H,x+W,y+H)if K[i]==1 then G.line(x,y,x+W,y+H)G.line(x,y+H,x+W,y)elseif K[i]==2 then G.circle('line',x+W/2,y+H/2,H/2)end end end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Rather than waiting around for a 1k challenge, I just did one on my own. Took about an hour. | |
-- The AI isn't a min-max perfect, it'll only take wins, block wins, or grab open corners. | |
-- Uses the mouse. Doesn't display who won, but will wait for an extra click before resetting | |
-- so the win can be verified through manual inspection. | |
L=love | |
G=L.graphics | |
W=267 | |
H=200 | |
F=math.floor | |
L.load = function() | |
K = {0,0,0,0,0,0,0,0,0} | |
end | |
B = {1, 3, 7, 9, 5, 2, 4, 6, 8} | |
L.mousepressed=function(x,y,b) | |
if C(K,1) or C(K,2) then | |
L.load() | |
else | |
i = F(y/H)*3 + F(x/W) + 1 | |
if K[i]<1 then | |
K[i]=1 | |
if C(K,1) then return end | |
for i=1,9 do | |
if K[i]<1 and C(K,2,i) then | |
K[i]=2 | |
return | |
end | |
end | |
for i=1,9 do | |
if K[i]<1 and C(K,1,i) then | |
K[i]=2 | |
return | |
end | |
end | |
for i=1,9 do | |
if K[B[i]]<1 then | |
K[B[i]]=2 | |
return | |
end | |
end | |
else | |
for i=1,9 do | |
if K[i]<1 then return end | |
end | |
L.load() | |
end | |
end | |
end | |
R = function(k,l,x,y,z) | |
return k[x]==l and k[y]==l and k[z]==l | |
end | |
C = function(k,l,q) | |
if q then k={unpack(k)};k[q]=l end | |
return R(k,l,1,2,3) or | |
R(k,l,4,5,6) or | |
R(k,l,7,8,9) or | |
R(k,l,1,4,7) or | |
R(k,l,2,5,8) or | |
R(k,l,3,6,9) or | |
R(k,l,1,5,9) or | |
R(k,l,3,5,7) | |
end | |
L.draw = function() | |
for i=1,9 do | |
x=(i-1)%3*W | |
y=F((i-1)/3)*H | |
G.line(x+W,0,x+W,y+H) | |
G.line(0,y+H,x+W,y+H) | |
if K[i] == 1 then | |
G.line(x,y,x+W,y+H) | |
G.line(x,y+H,x+W,y) | |
elseif K[i] == 2 then | |
G.circle('line',x+W/2,y+H/2,H/2) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment