2020-12-18 20:57:36 -06:00
|
|
|
local Randomizer = require 'tetris.randomizers.randomizer'
|
|
|
|
|
|
|
|
local HistoryRandomizer = Randomizer:extend()
|
|
|
|
|
|
|
|
function HistoryRandomizer:new(history_length, rolls, allowed_pieces)
|
2021-01-06 21:55:43 -06:00
|
|
|
self.super:new()
|
|
|
|
self.history = {}
|
2020-12-18 20:57:36 -06:00
|
|
|
for i = 1, history_length do
|
|
|
|
table.insert(self.history, '')
|
|
|
|
end
|
|
|
|
self.rolls = rolls
|
|
|
|
self.allowed_pieces = allowed_pieces
|
|
|
|
end
|
|
|
|
|
|
|
|
function HistoryRandomizer:generatePiece()
|
|
|
|
for i = 1, self.rolls do
|
2023-06-14 00:06:16 -05:00
|
|
|
local x = love.math.random(table.getn(self.allowed_pieces))
|
2020-12-18 20:57:36 -06:00
|
|
|
if not inHistory(self.allowed_pieces[x], self.history) or i == self.rolls then
|
|
|
|
return self:updateHistory(self.allowed_pieces[x])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function HistoryRandomizer:updateHistory(shape)
|
|
|
|
table.remove(self.history, 1)
|
|
|
|
table.insert(self.history, shape)
|
|
|
|
return shape
|
|
|
|
end
|
|
|
|
|
|
|
|
function inHistory(piece, history)
|
|
|
|
for idx, entry in pairs(history) do
|
|
|
|
if entry == piece then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
return HistoryRandomizer
|