Refactor Conway's Game of Life toward the Liskov Substitution Principle without changing its behavior. Setup is complete when all five starter tests pass.
This kata complements Clean Code: SOLID, Ep. 11 - Liskov Substitution Principle.
This repository contains two exercises designed to improve your skills in code refactoring, with a focus on the Liskov Substitution Principle (LSP).
The Game of Life is a cellular automaton devised by the British mathematician John Horton Conway in 1970. The game is a zero-player game, meaning that its evolution is determined by its initial state, requiring no further input.
You have a code base that simulates the Game of Life, but it does not adhere to the LSP. Your task is to refactor the code to align with the LSP. This means, among other things, that you should be able to replace any instance of a parent class with an instance of one of its child classes without altering the correctness of the program.
Instructions:
- Review the current code base and identify parts that violate the LSP.
- Refactor the violating code to align with the LSP.
- Make sure all the tests still pass after your refactoring.
Be sure to run the tests before and after your refactoring to make sure you haven't changed the game's behavior.
For those who complete the refactoring in the first part of the class, a second exercise is available. Your task is to extend the original code with new types of cells.
Here are the new cell types to implement:
- Immortal Cell: This cell type never dies. Once born, it stays alive through all the subsequent generations.
- Reproductive Cell: This cell type reproduces faster than a normal cell. It can make a new cell in the neighborhood with two or three neighbors instead of exactly three.
- Lazy Cell: This cell type requires more neighbors to survive. It stays alive only if it has exactly three neighbors.
Setup is complete when the existing test suite passes.
Required:
Optional:
- GNU Make, for shorter commands. Every required task also
has a direct
uvcommand.
You do not need to install Python or pytest separately. uv installs a compatible Python version
and the locked project dependencies when needed.
-
Clone the repository:
git clone https://github.com/Coding-Cuddles/game-of-life-refactoring-python-kata.git -
Enter the repository directory:
cd game-of-life-refactoring-python-kata -
Run the existing tests. Use Make when it is installed:
make testOtherwise, run pytest through
uvdirectly:uv run pytestThe first run may install Python and the project dependencies. Setup is complete when pytest reports
5 passed.If the command fails with
uv: command not found, install uv and repeat this step.
Start in game.py. The existing behavior is covered by test_game.py.
Run the tests after each change. Use Make when it is installed:
make testOtherwise, run pytest through uv directly:
uv run pytestContinue when the test run passes.
Make is optional. Run make or make help to list these commands in the terminal.
| Command | Result |
|---|---|
make all |
Run the test suite |
make help |
Show the command reference |
make test |
Run the test suite |
make format |
Format tracked Python files |
make format-check |
Check formatting without changing files |
make clean |
Remove generated caches |