-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.lua
293 lines (241 loc) · 6.68 KB
/
game.lua
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
--
-- Gameplay controller
--
local Game = Class:new()
--
-- Grid defaults
--
Game.gridSize = 4 -- Must be 2 or greater
Game.margin = 8 -- Margin size
--
-- Dependencies
--
local Square = require("square")
local Font = require("font")
local color = require("colors")
Game.init = function (self, gamedata, size, margin)
--
-- Initialize the grid
--
-- Set window background
love.graphics.setBackgroundColor(color("bg"))
-- Load pixel font
self.font = Font:new()
self.font:set()
if not gamedata then
-- Start a new game with provided params or game defaults
self.size = size or Game.gridSize
self.margin = margin or Game.margin
-- Start a new game
self:restart()
else
-- TODO Load previous gamedata from file
end
end
Game.getX = function (self, x)
--
-- Get square x coordinate for rendering
--
return (x-1) * self.sqSize + self.oX
end
Game.getY = function (self, y)
--
-- Get square y coordinate for rendering
--
return (y-1) * self.sqSize + self.oY
end
Game.refresh = function (self)
--
-- Refresh centerpoint, render offset, and square size
--
local wW,wH = love.graphics.getDimensions()
self.sqSize = math.floor(math.min(wW, wH) / self.size)
self.cX = math.floor(wW / 2)
self.cY = math.floor(wH / 2)
self.oX = self.cX - math.floor((self.sqSize * self.size) / 2)
self.oY = self.cY - math.floor((self.sqSize * self.size) / 2)
end
Game.restart = function (self)
--
-- Completely restart the game
--
local g = {}
local s = self.size
for y=1, s do
g[y] = {}
for x=1, s do g[y][x] = Square:new() end
end
self:refresh()
-- Reset grid
self.grid = g
-- Reset score
self.score = 0
-- Spawn two random squares
local x1,y1 = math.random(1, s), math.random(1, s)
local x2,y2 = math.random(1, s), math.random(1, s)
repeat x2,y2 = math.random(1, s), math.random(1, s) until x1 ~= x2 or y1 ~= y2
self:spawn(x1, y1)
self:spawn(x2, y2)
end
Game.spawn = function (self, x, y, n)
--
-- Spawn a new number onto the grid at the specified coordinates
--
local x = x
local y = y
local n = n or 2 * math.random(1, 2)
local g = self.grid
local s = self.size
g[y][x] = Square:new(n)
-- Reset grid
self.grid = g
end
Game.shift = function (self, dir)
--
-- Shift the tiles to fill any empty space
--
local g = self.grid
local s = self.size
-- Utilities
local fill = function(t, rev)
--
-- Fill in space with empty squares
--
while #t < s do
if rev then table.insert(t, 1, Square:new())
else
table.insert(t, Square:new())
end
end
return t
end
local combine = function(t, rev)
--
-- Combine matching squares
--
if #t > 1 then
local start,stop,d = 1, #t, 1
if rev then start,stop,d = #t, 1, -1 end
for i=start, stop, d do
if (not rev and i + d <= #t) or (rev and i+d >= 1) then
local nextSq = t[i+d]
if t[i].n == nextSq.n then
self.score = self.score + t[i].n + t[i+d].n
t[i].n = t[i].n + table.remove(t, i+d).n
end
end
end
end
return t
end
-- Copy the original grid
local og = {}
for y=1, s do
og[y] = {}
for x=1, s do
og[y][x] = g[y][x]
end
end
-- Move and combine squares
local rev = false
if dir == "left" or dir == "right" then
if dir == "right" then rev = true end
for y=1, s do
local t = {}
for x=1, s do
local sq = g[y][x]
if sq.n ~= 0 then table.insert(t, sq) end
end
-- Combine squares and set movement flag
t = combine(t, rev)
t = fill(t, rev)
g[y] = t
end
elseif dir == "up" or dir == "down" then
if dir == "down" then rev = true end
for x=1, s do
local t = {}
for y=1, s do
local sq = g[y][x]
if sq.n ~= 0 then table.insert(t, sq) end
end
-- Combine squares and set movement flag
t = combine(t, rev)
t = fill(t, rev)
for y=1, s do g[y][x] = t[y] end
end
end
-- Reset grid and compare it to the original
self.grid = g
-- Return true if grids are not identical
for y=1, s do
for x=1, s do
if g[y][x].n ~= og[y][x].n then
return true
end
end
end
return false
end
Game.move = function (self, dir)
--
-- 1) Shift tiles in the specified direction, combining as necessary
-- 2) Spawn a new tile if there is empty space
--
local g = self.grid
local s = self.size
-- Shift squares
local mv = self:shift(dir)
-- Add empty squares to list if any movements made
if mv then
local empty = {}
for y=1, s do
for x=1, s do
if g[y][x].n == 0 then
table.insert(empty, {["x"]=x, ["y"]=y})
end
end
end
-- Spawn a new square in empty space
if #empty > 0 then
local sq = empty[math.random(1, #empty)]
self:spawn(sq.x, sq.y)
end
end
end
Game.update = function (self, dt)
--
-- Update square size
--
self:refresh()
end
Game.draw = function (self)
--
-- Draw the grid
--
for y=1, self.size do
for x=1, self.size do
-- Get coords and dimensions
local oX,oY = self:getX(x), self:getY(y)
local w,h = self.sqSize - 1, self.sqSize - 1
-- Draw squares
local sq = self.grid[y][x]
if sq then
sq:draw(oX, oY, w, h)
-- Print values centered within square
local n = sq.n
if n ~= 0 then
n = tostring(n)
local fw = #n * (self.font.w + self.font.k)
local fh = self.font.h
local fx = math.floor(oX + (w / 2)) - math.floor(fw / 2) - 1
local fy = math.floor(oY + (h / 2)) - math.floor(fh / 2)
-- Print values
love.graphics.setColor(color("fg"))
love.graphics.print(n, fx, fy)
end
end
end
end
end
return Game