From b47d0f36b932bdbf54bd9e64e7817b9a9ca4121f Mon Sep 17 00:00:00 2001 From: Ishaan Bhardwaj Date: Wed, 14 Oct 2020 13:43:28 -0400 Subject: [PATCH] oshi forced me to add bags --- tetris/randomizers/bag8.lua | 24 ++++++++++++++++++++++ tetris/randomizers/recursive_bag.lua | 30 ++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 tetris/randomizers/bag8.lua create mode 100644 tetris/randomizers/recursive_bag.lua diff --git a/tetris/randomizers/bag8.lua b/tetris/randomizers/bag8.lua new file mode 100644 index 0000000..7c91791 --- /dev/null +++ b/tetris/randomizers/bag8.lua @@ -0,0 +1,24 @@ +local Randomizer = require 'tetris.randomizers.randomizer' + +local Bag7Randomizer = Randomizer:extend() + +function Bag7Randomizer:initialize() + self.bag = {"I", "J", "L", "O", "S", "T", "Z"} + self.extra = {"I", "J", "L", "O", "S", "T", "Z"} + table.insert(self.bag, table.remove(self.extra, math.random(table.getn(self.extra)))) +end + +function Bag7Randomizer:generatePiece() + if next(self.extra) == nil then + self.extra = {"I", "J", "L", "O", "S", "T", "Z"} + end + if next(self.bag) == nil then + self.bag = {"I", "J", "L", "O", "S", "T", "Z"} + table.insert(self.bag, table.remove(self.extra, math.random(table.getn(self.extra)))) + end + local x = math.random(table.getn(self.bag)) + --print("Bag: "..table.concat(self.bag, ", ").." | Extra: "..table.concat(self.extra, ", ")) + return table.remove(self.bag, x) +end + +return Bag7Randomizer diff --git a/tetris/randomizers/recursive_bag.lua b/tetris/randomizers/recursive_bag.lua new file mode 100644 index 0000000..4f7fdf1 --- /dev/null +++ b/tetris/randomizers/recursive_bag.lua @@ -0,0 +1,30 @@ +local Randomizer = require 'tetris.randomizers.randomizer' + +local RecursiveRandomizer = Randomizer:extend() + +function RecursiveRandomizer:initialize() + self.bag = {"I", "J", "L", "O", "S", "T", "Z"} +end + +function RecursiveRandomizer:generatePiece() + --if next(self.bag) == nil then + -- self.bag = {"I", "J", "L", "O", "S", "T", "Z"} + --end + local x = math.random(table.getn(self.bag) + 1) + while x == table.getn(self.bag) + 1 do + print("Refill piece pulled") + table.insert(self.bag, "I") + table.insert(self.bag, "J") + table.insert(self.bag, "L") + table.insert(self.bag, "O") + table.insert(self.bag, "S") + table.insert(self.bag, "T") + table.insert(self.bag, "Z") + x = math.random(table.getn(self.bag) + 1) + end + --print("Number of pieces in bag: "..table.getn(self.bag)) + --print("Bag: "..table.concat(self.bag, ", ")) + return table.remove(self.bag, x) +end + +return RecursiveRandomizer