mirror of
https://github.com/SashLilac/cambridge.git
synced 2024-11-22 07:19:02 -06:00
Refactored component API for rulesets with an arbitary...
...number of pieces (fixes #31)
This commit is contained in:
parent
42f872a557
commit
6f4adf5aad
25
funcs.lua
25
funcs.lua
@ -113,6 +113,31 @@ function table.numkeys(table)
|
|||||||
return count
|
return count
|
||||||
end
|
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)
|
function clamp(x, min, max)
|
||||||
if max < min then
|
if max < min then
|
||||||
min, max = max, min
|
min, max = max, min
|
||||||
|
@ -5,7 +5,7 @@ local playedReadySE = false
|
|||||||
local playedGoSE = false
|
local playedGoSE = false
|
||||||
|
|
||||||
local Grid = require 'tetris.components.grid'
|
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 BagRandomizer = require 'tetris.randomizers.bag'
|
||||||
|
|
||||||
local GameMode = Object:extend()
|
local GameMode = Object:extend()
|
||||||
@ -100,13 +100,11 @@ end
|
|||||||
function GameMode:initialize(ruleset)
|
function GameMode:initialize(ruleset)
|
||||||
-- generate next queue
|
-- generate next queue
|
||||||
self.used_randomizer = (
|
self.used_randomizer = (
|
||||||
ruleset.pieces == self.randomizer.possible_pieces and
|
table.equalvalues(
|
||||||
self.randomizer or
|
table.keys(ruleset.colourscheme),
|
||||||
(
|
self.randomizer.possible_pieces
|
||||||
ruleset.pieces == 7 and
|
) and
|
||||||
Randomizer() or
|
self.randomizer or BagRandomizer(table.keys(ruleset.colourscheme))
|
||||||
BagRandomizer(ruleset.pieces)
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
self.ruleset = ruleset
|
self.ruleset = ruleset
|
||||||
for i = 1, math.max(self.next_queue_length, 1) do
|
for i = 1, math.max(self.next_queue_length, 1) do
|
||||||
@ -700,7 +698,10 @@ end
|
|||||||
|
|
||||||
function GameMode:drawNextQueue(ruleset)
|
function GameMode:drawNextQueue(ruleset)
|
||||||
local colourscheme
|
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]
|
colourscheme = ({ruleset.colourscheme, ColourSchemes.Arika, ColourSchemes.TTC})[config.gamesettings.piece_colour]
|
||||||
else
|
else
|
||||||
colourscheme = ruleset.colourscheme
|
colourscheme = ruleset.colourscheme
|
||||||
|
@ -2,19 +2,15 @@ local Randomizer = require 'tetris.randomizers.randomizer'
|
|||||||
|
|
||||||
local BagRandomizer = Randomizer:extend()
|
local BagRandomizer = Randomizer:extend()
|
||||||
|
|
||||||
function BagRandomizer:new(pieces)
|
function BagRandomizer:new(piece_table)
|
||||||
self.bag = {}
|
self.bag = {}
|
||||||
self.possible_pieces = pieces
|
self.possible_pieces = piece_table
|
||||||
self.pieces = pieces
|
|
||||||
for i = 1, self.pieces do
|
|
||||||
table.insert(self.bag, i)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function BagRandomizer:generatePiece()
|
function BagRandomizer:generatePiece()
|
||||||
if next(self.bag) == nil then
|
if next(self.bag) == nil then
|
||||||
for i = 1, self.pieces do
|
for _, v in pairs(self.possible_pieces) do
|
||||||
table.insert(self.bag, i)
|
table.insert(self.bag, v)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local x = math.random(table.getn(self.bag))
|
local x = math.random(table.getn(self.bag))
|
||||||
|
@ -3,7 +3,7 @@ local Object = require 'libs.classic'
|
|||||||
local Randomizer = Object:extend()
|
local Randomizer = Object:extend()
|
||||||
|
|
||||||
function Randomizer:new()
|
function Randomizer:new()
|
||||||
self.possible_pieces = 7
|
self.possible_pieces = {"I", "J", "L", "O", "S", "T", "Z"}
|
||||||
self:initialize()
|
self:initialize()
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -15,10 +15,8 @@ function Randomizer:initialize()
|
|||||||
-- do nothing
|
-- do nothing
|
||||||
end
|
end
|
||||||
|
|
||||||
local shapes = {"I", "J", "L", "O", "S", "T", "Z"}
|
|
||||||
|
|
||||||
function Randomizer:generatePiece()
|
function Randomizer:generatePiece()
|
||||||
return shapes[math.random(7)]
|
return self.possible_pieces[math.random(7)]
|
||||||
end
|
end
|
||||||
|
|
||||||
return Randomizer
|
return Randomizer
|
||||||
|
@ -35,8 +35,6 @@ Ruleset.next_sounds = {
|
|||||||
T = "T"
|
T = "T"
|
||||||
}
|
}
|
||||||
|
|
||||||
Ruleset.pieces = 7
|
|
||||||
|
|
||||||
-- Component functions.
|
-- Component functions.
|
||||||
|
|
||||||
function Ruleset:new(game_mode)
|
function Ruleset:new(game_mode)
|
||||||
@ -208,7 +206,9 @@ function Ruleset:initializePiece(
|
|||||||
end
|
end
|
||||||
|
|
||||||
local colours
|
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]
|
colours = ({self.colourscheme, ColourSchemes.Arika, ColourSchemes.TTC})[config.gamesettings.piece_colour]
|
||||||
else
|
else
|
||||||
colours = self.colourscheme
|
colours = self.colourscheme
|
||||||
|
Loading…
Reference in New Issue
Block a user