Refactored component API for rulesets with an arbitary...

...number of pieces (fixes #31)
This commit is contained in:
Ishaan Bhardwaj 2021-10-16 20:35:47 -04:00
parent 42f872a557
commit 6f4adf5aad
5 changed files with 44 additions and 24 deletions

View File

@ -113,6 +113,31 @@ function table.numkeys(table)
return count
end
function equals(x, y)
if type(x) ~= "table" or type(y) ~= "table" then
return x == y
else
for k in pairs(x) do
if not equals(x[k], y[k]) then return false end
end
for k in pairs(y) do
if not equals(x[k], y[k]) then return false end
end
return true
end
end
function table.equalvalues(t1, t2)
if table.numkeys(t1) ~= table.numkeys(t2) then
return false
else
for _, v in pairs(t2) do
if not table.contains(t1, v) then return false end
end
return true
end
end
function clamp(x, min, max)
if max < min then
min, max = max, min

View File

@ -5,7 +5,7 @@ local playedReadySE = false
local playedGoSE = false
local Grid = require 'tetris.components.grid'
local Randomizer = require 'tetris.randomizers.bag7'
local Randomizer = require 'tetris.randomizers.randomizer'
local BagRandomizer = require 'tetris.randomizers.bag'
local GameMode = Object:extend()
@ -100,13 +100,11 @@ end
function GameMode:initialize(ruleset)
-- generate next queue
self.used_randomizer = (
ruleset.pieces == self.randomizer.possible_pieces and
self.randomizer or
(
ruleset.pieces == 7 and
Randomizer() or
BagRandomizer(ruleset.pieces)
)
table.equalvalues(
table.keys(ruleset.colourscheme),
self.randomizer.possible_pieces
) and
self.randomizer or BagRandomizer(table.keys(ruleset.colourscheme))
)
self.ruleset = ruleset
for i = 1, math.max(self.next_queue_length, 1) do
@ -700,7 +698,10 @@ end
function GameMode:drawNextQueue(ruleset)
local colourscheme
if ruleset.pieces == 7 then
if table.equalvalues(
self.used_randomizer.possible_pieces,
{"I", "J", "L", "O", "S", "T", "Z"}
) then
colourscheme = ({ruleset.colourscheme, ColourSchemes.Arika, ColourSchemes.TTC})[config.gamesettings.piece_colour]
else
colourscheme = ruleset.colourscheme

View File

@ -2,19 +2,15 @@ local Randomizer = require 'tetris.randomizers.randomizer'
local BagRandomizer = Randomizer:extend()
function BagRandomizer:new(pieces)
function BagRandomizer:new(piece_table)
self.bag = {}
self.possible_pieces = pieces
self.pieces = pieces
for i = 1, self.pieces do
table.insert(self.bag, i)
end
self.possible_pieces = piece_table
end
function BagRandomizer:generatePiece()
if next(self.bag) == nil then
for i = 1, self.pieces do
table.insert(self.bag, i)
for _, v in pairs(self.possible_pieces) do
table.insert(self.bag, v)
end
end
local x = math.random(table.getn(self.bag))

View File

@ -3,7 +3,7 @@ local Object = require 'libs.classic'
local Randomizer = Object:extend()
function Randomizer:new()
self.possible_pieces = 7
self.possible_pieces = {"I", "J", "L", "O", "S", "T", "Z"}
self:initialize()
end
@ -15,10 +15,8 @@ function Randomizer:initialize()
-- do nothing
end
local shapes = {"I", "J", "L", "O", "S", "T", "Z"}
function Randomizer:generatePiece()
return shapes[math.random(7)]
return self.possible_pieces[math.random(7)]
end
return Randomizer

View File

@ -35,8 +35,6 @@ Ruleset.next_sounds = {
T = "T"
}
Ruleset.pieces = 7
-- Component functions.
function Ruleset:new(game_mode)
@ -208,7 +206,9 @@ function Ruleset:initializePiece(
end
local colours
if self.pieces == 7 then
if table.equalvalues(
self.colourscheme, {"I", "J", "L", "O", "S", "T", "Z"}
) then
colours = ({self.colourscheme, ColourSchemes.Arika, ColourSchemes.TTC})[config.gamesettings.piece_colour]
else
colours = self.colourscheme