Created
January 19, 2017 07:55
-
-
Save pablomayobre/83619777c3202c815d525a6b1b151839 to your computer and use it in GitHub Desktop.
1K - Tic Tac Toe in LÖVE (multi player)
This file contains hidden or 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
--Note that when I performed the minification | |
--I changed a lots of parts of the code | |
--In order to get into the 1024 characters | |
--Some of those reductions are not performed in this code | |
--This is only a guide to understand how the game works | |
Y = require 'bit' --Bitops | |
a,b,c = Y.bnot,Y.bor,Y.band --Operations | |
m = a(0) --Max integer | |
F = math.floor | |
w = {7, 56, 73, 84, 146, 273, 292, 448} --Win conditions | |
C = {255,255,255} --Colors | |
A = {9,4,8,3,5,2,7,1,6} --Score for tiles | |
p = {0,0} --Players | |
t = 1 --Turn (Bool) | |
X = nil --Winner (1 - player 1, 2 - player 2, 3 - tie, false - still playing) | |
--Love | |
L = love | |
G = L.graphics | |
--Width and Height | |
D = 200 | |
B, E, H = D*3, D/2, D*.4 | |
L.window.setMode(B,B) | |
--Flashing | |
v = 0 | |
M = 1 | |
L.mousepressed = function(x, y) | |
if X then --Someone won so after a click we reset the game | |
p = {0,0} | |
X = nil | |
t = 1 | |
return | |
end | |
--K = 1--t and 1 or 2 --We turn the bool into numbers | |
T = 2^(F(y/D)*3 + F(x/D)) --Tile | |
if b(a(T), p[1], p[2]) ~= m then --Empty tile | |
p[1] = b(p[1], T) --Mark player | |
t = not t --Change the turn | |
for i=1, 8 do | |
f = a(w[i]) --Win condition | |
--If the win condition is matched then player wins | |
X = (not X and b(f, p[K])==m) and K or X | |
end | |
--Tie is when no one has one and board is full | |
X = (not X and (b(p[1], p[2]) + 1 == 2^9)) and 3 or X | |
end | |
end | |
L.update = function (dt) | |
v = v + dt --We just make M flash every .3 seconds | |
if v > .3 then | |
M = not M | |
v = 0 | |
end | |
end | |
L.draw = function () | |
for i=0, 8 do | |
G.setColor(255,255,255) --White | |
x, y, n = (i%3)*D, F(i/3)*D, a(2^i) | |
--Draw board | |
G.rectangle('line', x, y, D, D) --9 rectangles | |
G.push('all') | |
G.translate(x+E,y+E) --The center of the tile | |
if b(n, p[1]) == m then | |
--M is flash, so if player 1 wins or there is a tie we flash the pieces | |
G.setColor((not M and(X==1 or X==3))and C or{255,0,0}) | |
--Draw player 1 | |
G.line(-H, -H, H, H) --Cross | |
G.line(-H, H, H, -H) | |
elseif b(n, p[2]) == m then | |
--M is flash, so if player 2 wins or there is a tie we flash the pieces | |
G.setColor((not M and(X==2 or X==3))and C or{0,0,255}) | |
--Draw player 2 | |
G.circle('line', 0, 0, H) --Circle | |
end | |
G.pop() | |
end | |
end |
This file contains hidden or 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
Y,F,w,C,A,p,t,L,D,v,M=require'bit',math.floor,{7,56,73,84,146,273,292,448},{255,255,255},{9,4,8,3,5,2,7,1,6},{0,0},1,love,200,0,1;q,m,b,G,B,L.mousepressed,L.update,L.draw='line',Y.bnot(0),Y.bor,L.graphics,600,function(x,y)if X then p,t,X={0,0},1;return end;K,T=t and 1 or 2,2^(F(y/D)*3+F(x/D))if m~=b(m-T,p[1],p[2])then p[K],t=b(p[K],T),not t;for i=1,8 do X=not X and m==b(m-w[i],p[K])and K or X end;X=not X and 2^9-1==b(p[1],p[2])and 3 or X end end,function(t)v=v+t;if v>.3 then v,M=0,not M end end,function()for i=0,8 do n,x,y=m-2^i,D*(i%3),D*F(i/3)o(C)G.rectangle(q,x,y,D,D)if m==b(n,p[1])then o((M and(X==1 or X==3))and C or{255,0,0})J,H,r,s=20+x,180+x,20+y,180+y;e(J,r,H,s)e(J,s,H,r)elseif m==b(n,p[2])then o((not M and(X==2 or X==3))and C or{0,0,255})G.circle(q,x+100,y+100,90)end end end;L.window.setMode(B,B)e,o=G.line,G.setColor |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment