-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
111 lines (96 loc) · 2.67 KB
/
main.lua
File metadata and controls
111 lines (96 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
local grid = {}
local currentPlayer = "miku"
local winner = nil
local imageX
local imageY
function love.load()
windowWidth = 600
windowHeight = 600
love.window.setMode(windowWidth, windowHeight)
cellSize = windowWidth / 3
imageX = love.graphics.newImage("miku.png")
imageY = love.graphics.newImage("nino2.png")
for i = 1, 3
do
grid[i] = {"", "", ""}
end
end
function resetGame()
for i = 1, 3
do
grid[i] = {"", "", ""}
end
currentPlayer = "miku"
winner = nil
end
function love.mousepressed(x, y, button)
if button == 1
then
local row = math.ceil(y / cellSize)
local col = math.ceil(x / cellSize)
if grid[row][col] == "" and winner == nil
then
grid[row][col] = currentPlayer
currentPlayer = (currentPlayer == "miku") and "nino" or "miku"
checkWinner()
end
end
end
function love.keypressed(key)
if key == "r"
then
resetGame()
end
end
function checkWinner()
for row = 1, 3
do
if grid[row][1] ~= "" and grid[row][1] == grid[row][2] and grid[row][2] == grid[row][3]
then
winner = grid[row][1]
return
end
end
for col = 1, 3
do
if grid[1][col] ~= "" and grid[1][col] == grid[2][col] and grid[2][col] == grid[3][col]
then
winner = grid[1][col]
return
end
end
if grid[1][1] ~= "" and grid[1][1] == grid[2][2] and grid[2][2] == grid[3][3]
then
winner = grid[1][1]
return
end
if grid[1][3] ~= "" and grid[1][3] == grid[2][2] and grid[2][2] == grid[3][1]
then
winner = grid[1][3]
return
end
end
function love.draw()
love.graphics.line(cellSize, 0, cellSize, windowHeight)
love.graphics.line(2 * cellSize, 0, 2 * cellSize, windowHeight)
love.graphics.line(0, cellSize, windowWidth, cellSize)
love.graphics.line(0, 2 * cellSize, windowWidth, 2 * cellSize)
for row = 1, 3
do
for col = 1, 3
do
local mark = grid[row][col]
if mark == "miku"
then
love.graphics.draw(imageX, (col - 1) * cellSize + 10, (row - 1) * cellSize, 0, cellSize / imageX:getWidth(), cellSize / imageX:getHeight())
elseif mark == "nino"
then
love.graphics.draw(imageY, (col - 1) * cellSize + 10, (row - 1) * cellSize + 10, 0, cellSize / imageY:getWidth(), cellSize / imageY:getHeight())
end
end
end
if winner
then
love.graphics.print(winner .. " ha ganado!", windowWidth / 2 - 50, windowHeight - 25)
end
end