summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorheav <hheav3@gmail.com>2023-03-26 11:06:54 +0000
committerheav <hheav3@gmail.com>2023-03-26 11:06:54 +0000
commit2d99cf4a8a6bf7d4cce822d6fd2a91774775dff5 (patch)
tree82be6edf372cb27079fd7d17f404795c28299bf3
parent9cd399d27319fb9e3eaee79fec59660446df10b5 (diff)
add a rudimentary 7-bag system
-rw-r--r--game/bag.lua46
-rw-r--r--game/init.lua6
2 files changed, 49 insertions, 3 deletions
diff --git a/game/bag.lua b/game/bag.lua
new file mode 100644
index 0000000..76e6308
--- /dev/null
+++ b/game/bag.lua
@@ -0,0 +1,46 @@
+local tetrominoes = require "game.tetrominoes"
+
+local M = {}
+M.__index = M
+
+M.new = function()
+ local new = setmetatable({}, M)
+ new.queue = {}
+ new.pool = {}
+ return new
+end
+
+function M:lookahead(amount)
+ local res = {}
+ for i=1, amount do
+ if self.queue[i] then
+ table.insert(res, self.queue[i])
+ else
+ if #self.pool == 0 then
+ self.pool = {
+ tetrominoes.i,
+ tetrominoes.j,
+ tetrominoes.l,
+ tetrominoes.o,
+ tetrominoes.s,
+ tetrominoes.t,
+ tetrominoes.z,
+ }
+ end
+ local choice = math.random(1, #self.pool)
+ local choice_tetr = self.pool[choice]
+ table.remove(self.pool, choice)
+ table.insert(self.queue, choice_tetr)
+ table.insert(res, choice_tetr)
+ end
+ end
+ return res
+end
+
+function M:next_piece()
+ local res = self:lookahead(1)[1]
+ table.remove(self.queue, 1)
+ return res
+end
+
+return M \ No newline at end of file
diff --git a/game/init.lua b/game/init.lua
index 0056ccb..48f9a15 100644
--- a/game/init.lua
+++ b/game/init.lua
@@ -2,6 +2,7 @@ local evloop = require "evloop"
local playfield = require "game.playfield"
local tetrominoes = require "game.tetrominoes"
local gfx = require "game.gfx"
+local bag = require "game.bag"
local M = {}
M.__index = M
@@ -14,6 +15,7 @@ function M.new(params)
new.can_hold = true
new.gfx = gfx.new(new)
new.gravity_delay = 0.5
+ new.bag = bag.new() -- TODO: bag should be seeded
return new
end
@@ -70,10 +72,8 @@ local pieces = {
}
function M:next_piece()
- -- TODO: interface with configurable random system (it should implement
- -- seeds, bags)
self.can_hold = true
- self.piece = pieces[love.math.random(#pieces)]:drop(self.field)
+ self.piece = self.bag:next_piece():drop(self.field)
return self.piece and true or false
end