Randomizer

class tetris_gymnasium.components.tetromino_randomizer.Randomizer(size: int)[source]

Abstract class for tetromino randomizers.

A randomizer is an object that can be used to generate the order of tetrominoes in a game of Tetris. When it’s called via get_next_tetromino(), it returns the index of the next tetromino to be used in the game. This information can be used by the caller to get the actual tetromino object from a list of tetrominoes.

Parameters:

size – The number of tetrominoes to choose from.

Methods

abstract Randomizer.get_next_tetromino() int[source]

Get the index of the next tetromino to be used in the game.

Returns: The index of the next tetromino to be used in the game.

abstract Randomizer.reset(seed=None)[source]

Resets the randomizer.

This function is implemented after the usage pattern in Gymnasium, where seed is passed to the reset function only for the very first call after initialization. In all other cases, seed=None and the RNG is not reset.

Implementations

In Tetris Gymnasium, there are different randomizers available by default. The default randomizer is the BagRandomizer, which is the same as the one used in the most Tetris games. The TrueRandomizer is a randomizer that generates tetrominoes with a uniform distribution.

If these randomizers do not fit your needs, you can easily implement your own randomizer by subclassing the Randomizer.

class tetris_gymnasium.components.tetromino_randomizer.BagRandomizer(size)[source]

Randomly selects tetrominoes from a bag, ensuring that each tetromino is used once before reshuffling.

The bag randomizer is a common and popular approach in Tetris. It ensures that each tetromino is used once before reshuffling the bag, thus avoiding long sequences of the same tetromino. The functionality is explained on the tetris wiki page: https://tetris.fandom.com/wiki/Random_Generator

Parameters:

size – The number of tetrominoes to choose from.

BagRandomizer.get_next_tetromino() int[source]

Samples a new tetromino from the bag.

Once the bag has been fully exploited, it is reshuffled and the process starts over.

Returns: The index of the next tetromino to be used in the game.

class tetris_gymnasium.components.tetromino_randomizer.TrueRandomizer(size)[source]

Randomly selects tetrominoes.

This is the simplest form of randomizer, where each tetromino is chosen randomly. This approach can lead to sequences of the same tetromino, which may or may not be desired.

Parameters:

size – The number of tetrominoes to choose from.

TrueRandomizer.get_next_tetromino() int[source]

Samples a new tetromino randomly.