A complete implementation of the ancient board game Go (Weiqi/Baduk) in Python, featuring Abstract Data Types (ADTs) and functional programming principles. This project was developed as part of the Fundamentals of Programming course at Instituto Superior Técnico.
This implementation allows two players to play a full game of Go on boards of different sizes (9×9, 13×13, or 19×19). The project demonstrates advanced programming concepts including:
- Abstract Data Types for clean code organization
- Functional programming paradigms
- Game logic implementation including complex rules like Ko and suicide prevention
- Territory calculation and scoring system
- ✅ Support for multiple board sizes (9×9, 13×13, 19×19)
- ✅ Complete rule implementation:
- Stone placement and capture
- Suicide rule prevention
- Ko rule (repetition prevention)
- Territory scoring
- Pass functionality
- ✅ Turn-based gameplay for two players
- ✅ Automatic win condition detection
- TAD Intersection: Represents board positions with column-row notation
- TAD Stone: Represents black, white, and neutral stones
- TAD Goban: Complete board representation with all game logic
- High-level functions: Territory calculation, legal move validation, scoring system
- Python 3.x
- No external dependencies required (pure Python implementation)
- Clone the repository:
git clone https://github.com/yourusername/go-game-python.git
cd go-game-python- Run the game:
python go.py# Start a new 9x9 game with empty board
go(9, (), ())
# Start a game with initial stone positions
white_positions = ('C2', 'D2', 'E2')
black_positions = ('C3', 'D3', 'E3')
go(9, white_positions, black_positions)During gameplay:
- Enter intersection coordinates (e.g.,
A1,B5) to place a stone - Enter
Pto pass your turn - Game ends when both players pass consecutively
A B C D E F G H I
9 . . . . . . . . . 9
8 . . O O O O O O O 8
7 . . O X X X X X O 7
6 . O X O O O O X O 6
5 . O X O X . O X O 5
4 . O X O O O X X . 4
3 . O X X X X X . O 3
2 . . O O O O O O . 2
1 . . . . . . . . . 1
A B C D E F G H I
Represents a position on the board.
Key Functions:
cria_intersecao(col, lin)- Creates an intersectionobtem_col(i)/obtem_lin(i)- Gets column/lineobtem_intersecoes_adjacentes(i, l)- Gets adjacent intersectionsordena_intersecoes(t)- Sorts intersections in reading order
Represents game pieces.
Key Functions:
cria_pedra_branca()/cria_pedra_preta()- Creates stoneseh_pedra_branca(p)/eh_pedra_preta(p)- Checks stone colorpedra_para_str(p)- Converts to string ('O', 'X', or '.')
Represents the complete game board.
Key Functions:
cria_goban_vazio(n)- Creates empty boardcria_goban(n, ib, ip)- Creates board with initial stonesobtem_cadeia(g, i)- Gets stone chain at positionobtem_territorios(g)- Calculates all territoriesjogada(g, i, p)- Executes a move with capture logic
calcula_pontos(g)- Calculates both players' scoreseh_jogada_legal(g, i, p, l)- Validates move legalityturno_jogador(g, p, l)- Handles player turngo(n, tb, tn)- Main game loop
- Placement: Players alternate placing stones on empty intersections
- Capture: Stones without liberties (adjacent empty spaces) are captured
- Suicide Rule: Cannot place a stone that would have no liberties (unless it captures opponent stones)
- Ko Rule: Cannot create a board position that occurred previously
- Scoring: Points = controlled territory + stones on board
- Victory: Highest score wins (white wins on tie)
The project includes comprehensive testing:
# Run public tests
pytest test_public.py
# Run abstraction barrier tests
pytest test_abstraction.py- Language: Python 3
- Programming Paradigm: Functional programming with ADTs
- No external libraries: Pure Python implementation (except
reducefromfunctools) - Input validation: All constructors validate arguments
- Immutable types: Intersections and stones are immutable
- Destructive modification: Board operations modify in place
Note: This implementation follows the Chinese rules variant of Go with specific scoring and Ko interpretation as defined in the project specification.