hexgame/lib.rs
1/*!
2This is a simple and performant implementation of the Hex board game.
3
4While it was mainly written to be used in Monte-Carlo Tree Search bots for Hex, it aims to be a general-purpose library for Hex.
5
6Features:
7
8* rules of the game (without swap rule, see below),
9* serialize/deserialize to/from JSON,
10* some analysis functions that may be helpful when writing bots: `get_neighbors`, `get_empty_cells`, `find_attacked_bridges`. See the `Board` struct for more information.
11
12# The Game of Hex
13
14The rules of Hex are very simple: Hex is played by two players (Black and White) on a board of hexagons. The size is variable, typical sizes are 11x11 or 19x19.
15Starting with Black, the players take turns to place stones of their color on an empty space. The task of Black is to connect the top and bottom edges.
16White needs to connect left and right edges. The first player to connect their edges wins the game.
17
18In the following example, Black (●) has won the game after connecting the top edge to the bottom edge.
19```text
20 a b c d e
211\. . . ○ ●\1
22 2\. . ○ ● .\2
23 3\. . ● ● .\3
24 4\. . ○ ● .\4
25 5\. ○ ● ○ .\5
26 a b c d e
27```
28
29Hex was invented in the 1940s by Piet Hein and, independently, by John Nash. Please read the [Wikipedia](https://en.wikipedia.org/wiki/Hex_(board_game)) page for more information.
30
31## The Swap Rule
32
33As starting player, Black has a huge advantage. In real games, this advantage is circumvented by the so-called swap rule: After Black has placed the first Stone, the second player can choose to either continue the game normally or to swap colors.
34
35This library does not yet implement the swap rule, mostly because it was not necessary for the MCTS-research using this library.
36
37# How to use this library
38
39The most important struct is `Game`. To play the game, you also need `Coords`. The game keeps track of the current player automatically.
40```
41use hexgame::{Coords, Game};
42
43let mut game = Game::new(19); // size of the board
44
45game.play(Coords::new(3, 5)); // (row, column) and zero-based, i.e. f4
46
47// Or use human-readable coordinates
48game.play("d5".parse().unwrap());
49```
50
51`game.board` can be used to access the cells of the board (e.g. `get_color(coords)`).
52
53## Serialization
54
55Serialization functionality requires `use hexgame::Serialization;`.
56
57To serialize a game, use `game.save_to_string()` or `game.save_to_json()`, which serializes to a [Serde](https://serde.rs/) value.
58`Game::load_from_str` or `Game::load_from_json` can be used to create a game from a JSON string or value.
59
60# Playing Hex on CLI
61
62While this package is mostly a library, it also contains a small command-line interface to player Hex.
63```text
64cargo run
65```
66Then type the coordinates of the space where you would like to place your next stone, e.g. "c2" and press Enter.
67
68Optionally, you can specify the size of the board like in `cargo run 7`.
69*/
70mod attacked_bridges;
71mod board;
72mod color;
73mod coords;
74mod edges;
75mod errors;
76mod format;
77mod game;
78mod hex_cells;
79mod neighbors;
80mod serialize;
81mod union_find;
82
83pub use crate::board::{Board, StoneMatrix, MAX_BOARD_SIZE, MIN_BOARD_SIZE};
84pub use crate::color::Color;
85pub use crate::coords::{CoordValue, Coords};
86pub use crate::edges::{CoordsOrEdge, Edge};
87pub use crate::errors::{InvalidBoard, InvalidMove};
88pub use crate::game::{Game, Status};
89pub use crate::serialize::Serialization;