Skip to content

Complete Python implementation of the Go board game with ADTs and functional programming. Features 9×9/13×13/19×19 boards, full rule enforcement (Ko, suicide), territory calculation, and scoring system. Academic project for IST's Fundamentals of Programming course.

Notifications You must be signed in to change notification settings

pedroMVicente/go-game-implementation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 

Repository files navigation

Go Game Implementation in Python 🎮

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.

📋 About the Project

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

🎯 Features

Game Mechanics

  • ✅ 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

Technical Implementation

  • 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

🚀 Getting Started

Prerequisites

  • Python 3.x
  • No external dependencies required (pure Python implementation)

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/go-game-python.git
cd go-game-python
  1. Run the game:
python go.py

💻 Usage

Starting a Game

# 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)

Game Commands

During gameplay:

  • Enter intersection coordinates (e.g., A1, B5) to place a stone
  • Enter P to pass your turn
  • Game ends when both players pass consecutively

Example Board Output

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

🏗️ Project Structure

Abstract Data Types

1. TAD Intersection (intersecao)

Represents a position on the board.

Key Functions:

  • cria_intersecao(col, lin) - Creates an intersection
  • obtem_col(i) / obtem_lin(i) - Gets column/line
  • obtem_intersecoes_adjacentes(i, l) - Gets adjacent intersections
  • ordena_intersecoes(t) - Sorts intersections in reading order

2. TAD Stone (pedra)

Represents game pieces.

Key Functions:

  • cria_pedra_branca() / cria_pedra_preta() - Creates stones
  • eh_pedra_branca(p) / eh_pedra_preta(p) - Checks stone color
  • pedra_para_str(p) - Converts to string ('O', 'X', or '.')

3. TAD Goban (goban)

Represents the complete game board.

Key Functions:

  • cria_goban_vazio(n) - Creates empty board
  • cria_goban(n, ib, ip) - Creates board with initial stones
  • obtem_cadeia(g, i) - Gets stone chain at position
  • obtem_territorios(g) - Calculates all territories
  • jogada(g, i, p) - Executes a move with capture logic

Additional Functions

  • calcula_pontos(g) - Calculates both players' scores
  • eh_jogada_legal(g, i, p, l) - Validates move legality
  • turno_jogador(g, p, l) - Handles player turn
  • go(n, tb, tn) - Main game loop

🎲 Game Rules

  1. Placement: Players alternate placing stones on empty intersections
  2. Capture: Stones without liberties (adjacent empty spaces) are captured
  3. Suicide Rule: Cannot place a stone that would have no liberties (unless it captures opponent stones)
  4. Ko Rule: Cannot create a board position that occurred previously
  5. Scoring: Points = controlled territory + stones on board
  6. Victory: Highest score wins (white wins on tie)

🧪 Testing

The project includes comprehensive testing:

# Run public tests
pytest test_public.py

# Run abstraction barrier tests
pytest test_abstraction.py

🛠️ Technical Specifications

  • Language: Python 3
  • Programming Paradigm: Functional programming with ADTs
  • No external libraries: Pure Python implementation (except reduce from functools)
  • 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.

About

Complete Python implementation of the Go board game with ADTs and functional programming. Features 9×9/13×13/19×19 boards, full rule enforcement (Ko, suicide), territory calculation, and scoring system. Academic project for IST's Fundamentals of Programming course.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages