This repository contains the codebase and documentation for the Fast Tetris project, which aims to develop a Genetic Algorithm (GA) for evolving players in a Tetris-like game environment.
- Code Structure and Implementation
- GA Information
- Path / Move Stretching Algorithm and Strategy
- Version of the Code
- Future Implementation and Design
The project is built using Python, with Pygame being the major library used for game development. The code follows a structured approach to organization and implementation, with key components including classes for players, the game environment, rendering, and the Genetic Algorithm (GA).
The base-level classes include:
- Grid: Represents the game grid.
- Block: Defines the basic properties and behavior of a block.
- Block subclasses (e.g., LBlock, IBlock, JBlock, etc.): Inherit from the Block class and represent specific block shapes.
- Position: Manages the position of blocks on the grid.
- Color: Defines colors used for rendering.
Intermediate-level classes include:
- Player: Represents an individual player in the game, with attributes such as height, lines cleared, holes, blockades, and score.
- PathSearcher: Generates all possible moves for players based on game state and player weights.
Higher-level classes include:
- Game: Controls the game environment and logic, interacting with other classes such as HumanEmulator, GA, and PersistentGA.
- HumanEmulator: Facilitates human gameplay interaction.
- GA: Manages the Genetic Algorithm process for evolving players.
- PersistentGA: Resumes the evolution process from where it was last left off and stores generation data in files.
All data related to generations is stored in the Generation folder, with each generation having its own subfolder named Generation_X.
The Genetic Algorithm (GA) implemented in this project aims to evolve players in a Tetris-like game environment. Through iterative refinement of a population of players, the GA selects the fittest individuals based on predefined criteria, applies genetic operators such as crossover and mutation to generate offspring, and continues the evolution process indefinitely until the GA reaches a mature state capable of playing endlessly.
Initialisation: The GA initiates players with random weights for attributes such as height, lines cleared, holes, and blockades. These weights are crucial in determining player behaviour and strategy during gameplay.
Each generation consists of 10 players, and the GA evolves them using mutation and crossover operators. The process involves selecting the top-performing players, performing crossover between parents, and introducing randomness through mutation.
Example of what a generation looks like:
The GA's environment is configured using parameters specified in the config.py file, allowing for flexibility and customisation of the evolutionary process.

The GA initialises the first generation with 10 players, assigning random weights for each player's attributes within specified ranges.
Lines Cleared -------------------------------------> positive
Height -------------------------------------> negative
Blocakdes -------------------------------------> negative
Holes/Gaps -------------------------------------> negative def initialize_population(self):
self.population = []
for _ in range(self.population_size):
height_weight = uniform(-15.0, 0.0)
lines_cleared_weight = uniform(0.0, 15.0)
holes_weight = uniform(-15.0, 0.0)
blockades_weight = uniform(-15.0, 0.0)
player =Player(height_weight,lines_cleared_weight,holes_weight,blockades_weight)
self.population.append(player)Each player in the generation is simulated in the game environment, making moves based on their assigned weights for attributes the moves calculation which will be discussed in detail afterwards.
The GA selects the top-scoring players from the current generation to proceed to the next generation.
Crossover is performed between selected parent players to generate offspring with combined traits. selects a random crossover point and then performs crossover between two best players in a population.

Random mutation is applied to introduce variability and prevent premature convergence. This is done by randomly changing weights of a player’s attributes.
After all players in the current generation have played, the GA initializes the next generation by selecting top players, performing crossover, and introducing random players.
After players of each generation play the game, the Genetic Algorithm (GA) proceeds to the player class, where it utilizes the path searcher class to generate all possible moves.
The path searcher class takes the game instance and player weights as input. It then calculates all possible moves based on the current game state and logic.
what we have input form GA
After applying a move, the path searcher calculates various attributes, or genes, of the player. These include:
according to game analytics and it plays a major role so by some research and break down we came up to 3 type of holes
any cell of the block if acts to assist in creation of a block it is termed as a blockade
the maximum height of any tower in a grid after placing the block
lines cleared in the grid if that move is placed
The path searcher then computes the rank of each move based on the player weights, assigning a score to each move. It returns a list of paths ranked by score, allowing the player to choose the optimal move.
The current implementation performs well, achieving an average score of approximately 250 to 300. After around 10 generations, scores begin to exceed 800 to 1000, indicating the effectiveness of the GA in improving player performance over time.

we look forward to implement following in the code
In an ideal scenario, the GA would implement the A* algorithm to calculate all possible moves of the grid, treating it as a tree structure. This approach would provide more accurate and efficient path searching.
Future enhancements could involve extending the path searcher to calculate moves for both the current block and the next block. This improvement would enhance strategic planning and overall gameplay.








