cambridge/tetris/randomizers/bag.lua
Ishaan Bhardwaj 6f4adf5aad Refactored component API for rulesets with an arbitary...
...number of pieces (fixes #31)
2021-10-16 20:35:47 -04:00

21 lines
480 B
Lua

local Randomizer = require 'tetris.randomizers.randomizer'
local BagRandomizer = Randomizer:extend()
function BagRandomizer:new(piece_table)
self.bag = {}
self.possible_pieces = piece_table
end
function BagRandomizer:generatePiece()
if next(self.bag) == nil then
for _, v in pairs(self.possible_pieces) do
table.insert(self.bag, v)
end
end
local x = math.random(table.getn(self.bag))
return table.remove(self.bag, x)
end
return BagRandomizer