Source code for tetris_gymnasium.components.tetromino_queue
"""Module for a queue of tetrominoes for use in a game of Tetris."""
from collections import deque
from tetris_gymnasium.components.tetromino_randomizer import Randomizer
[docs]
class TetrominoQueue:
"""The `TetrominoQueue` stores all incoming tetrominoes in a queue.
The sequence of pieces is generated by a :class:`Randomizer`, which can be customized by the user.
"""
def __init__(self, randomizer: Randomizer, size=4):
"""Create a new queue of tetrominoes with the given size.
Args:
randomizer: The randomizer that generates the tetrominoes sequence.
size: The number of tetrominoes to store. Defaults to 4.
"""
self.randomizer = randomizer
self.queue = deque(maxlen=size)
self.size = size
[docs]
def reset(self, seed=None):
"""Reset the queue to its initial state.
Args:
seed: The seed to use for the randomizer. Defaults to None.
"""
self.randomizer.reset(seed)
self.queue.clear()
for _ in range(self.size):
self.queue.append(self.randomizer.get_next_tetromino())
[docs]
def get_next_tetromino(self):
"""Gets the next tetromino from the queue and generates a new one.
Generating a new Tetromino makes sure that the queue will always be full.
"""
tetromino = self.queue.popleft()
self.queue.append(self.randomizer.get_next_tetromino())
return tetromino
[docs]
def get_queue(self):
"""Get all tetrominoes currently in the queue."""
return list(self.queue)
def copy(self, randomizer: Randomizer):
"""Create a copy of the queue.
Args:
randomizer: The randomizer to use for the new queue. Should be copied using its respective `copy` method.
"""
new_queue = TetrominoQueue(randomizer, self.size)
new_queue.queue = deque(self.queue)
return new_queue