Skip to content

spyn1k/GTP-Go-Engine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GOTEAM.C

The goteam program is a Go engine implementing the GTP(Go Text Protocol).

It manages the board state, validates moves, handles stone capturing via recursive flood-fill, and generates legal moves for the computer to play.

  • play <color> <coordinate> Places a stone of the specified color at the given coordinate

  • genmove <color> Generates a random legal move for the specified color

  • showboard Displays the current state of the board in the terminal

  • clear_board Resets the board to an empty state

                                                            COMPILE WITH
    

gcc -static -Wall -Wextra -Werror -pedantic -O3 -o goteam src/goteam.c src/board.c

Or if using Make:

make

                                                          COMMAND GUIDE

Interactive Mode

./goteam

Starts the engine. Type commands manually (e.g play black D4)

Play a Move

play black D4

Places a black stone at D4

Returns = on success or ? on failure

Generate AI Move

genmove white

The engine picks a random legal coordinate for White and returns the coordinate

Visualize Board

showboard

Prints the 19x19 grid with X (Black) and O (White) stones.

                                                           INPUT FORMAT

The program expects GTP commands via standard input (stdin):

<command_name> [arguments]

Example (Sequence of commands):

play black Q16
genmove white
showboard
quit

PRODUCTION CHOICE

Why Recursive Flood Fill was chosen for captures:

After analyzing the requirements for game rules, Recursive Flood Fill was selected as the core algorithm for capturing stones.

  • Accuracy: Go rules state that a group of stones is captured when it has no "liberties" (empty slots). Flood fill naturally traverses connected stones to count these liberties

  • Simplicity: Unlike iterative approaches which require complex stack management the recursive approach (has_liberties) is cleaner and easier to read

  • Suicide Safety: The same algorithm allows us to perform a check before finalizing a move, therefore ensuring the engine never plays an illegal self-capture move

    Example of Suicide Safety:

image
                                                       LOGIC TRANSLATION

Implementing Go Rules to C Code

The assignment required translating board coordinates and capture rules into C logic:

Coordinate Parsing

The GTP standard uses letters 'A' through 'T' (excluding 'I') for columns

parse_coord translates "D4" to:

  • Column: 'D' - 'A' = Index 3
  • Row: 4 - 1 = Index 3
  • Result: board[3][3]

The Capture Recursion

The logical relation for a stone having "air" (liberties) was implemented in has_liberties:

has_liberties(r, c) = is_empty(r,c) OR (is_same_color(r,c) AND has_liberties(neighbors))

That was implemented inside the board.c file using the visited array to prevent infinite loops during the recursive search

Visualizing the Capture Logic:

imageimage

Random Generation

The gen_move function uses a brute-force approach with a safety check

1.Pick a random coordinate (r, c) 2.Check if play_move(r, c) returns true (meaning the move is not suicide and the spot is empty) 3.If valid then return the move otherwise retry

SOURCES

[0] GTP – Go Text Protocol Specification

[1] GTP – Go Text Protocol Specification (Draft 2)

[2] Wikipedia – Flood Fill Algorithm

[3] Wikipedia – Go (Game) Rules

[4] Sensei's Library – Liberties

[5] GeeksforGeeks – Flood Fill Algorithm

About

No description or website provided.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors