secondbest/
lib.rs

1//! `secondbest` is a Rust library for implementing the "Second Best" strategy game. This library
2//! provides a complete implementation of the game rules, board representation, and game state management.
3//!
4//! # Game Overview
5//!
6//! "Second Best" is a strategic board game played on 8 positions arranged in a circle. Players take turns
7//! placing pieces of their color (Black or White) or moving pieces already on the board. The game features
8//! a unique mechanic where a player can declare "second best" after their opponent's move, forcing the opponent
9//! to choose a different action.
10//!
11//! ## Game Setup
12//!
13//! - The game starts with an empty board of 8 positions arranged in a circle (N, NE, E, SE, S, SW, W, NW)
14//! - Each position can hold up to 3 stacked pieces
15//! - Black plays first, then players alternate turns
16//! - On each turn, a player must either place a new piece or move an existing one of their color
17//!
18//! ## "Second Best" Mechanic
19//!
20//! The unique feature of this game is the "second best" declaration:
21//!
22//! - After an opponent makes their move (their first action in the turn), a player can declare "second best"
23//! - This forces the opponent to choose a different action (their "second best" choice)
24//! - A player can only declare "second best" after the opponent's first action in a turn, not after their second action
25//! - The "second best" declaration can only be used once per turn
26//!
27//! ## Winning Conditions
28//!
29//! A player wins when they achieve one of the following:
30//! - **Vertical Line-up**: Stack 3 of their pieces in a single position
31//! - **Horizontal Line-up**: Have their pieces at the top of 4 consecutive positions
32//! - **Priority Win**: If both players achieve a winning condition simultaneously, the player who made the last move wins
33//! - **No Moves**: If a player has no legal moves available, they lose
34//!
35//! Note: Winning conditions are only checked after all "second best" declarations have been used.
36//!
37//! # Architecture
38//!
39//! The library is organized into several modules:
40//!
41//! - `board`: Core board representation and operations
42//!   - `BitBoard`: Low-level bit manipulation for efficient board state representation
43//!   - `Board`: High-level API for board manipulation
44//!   - `Position`: Enumeration of the 8 board positions (N, NE, E, SE, S, SW, W, NW)
45//!   - `Color`: Player colors (B for Black, W for White)
46//!   - `Action`: Game actions (Put or Move)
47//!
48//! - `game`: Game state management
49//!   - `Game`: Main game state controller
50//!   - `GameResult`: Game outcome representation
51//!
52//! - `error`: Error handling
53//!   - Various error types for different operations
54//!
55//! # Basic Usage
56//!
57//! ```rust
58//! use secondbest::prelude::*;
59//!
60//! // Create a new game
61//! let mut game = Game::new();
62//!
63//! // Apply actions (place pieces and move them)
64//! game.apply_action(Action::Put(Position::N, Color::B)).unwrap();
65//! game.apply_action(Action::Put(Position::S, Color::W)).unwrap();
66//!
67//! // Check game state
68//! if game.can_declare_second_best() {
69//!     game.declare_second_best().unwrap();
70//! }
71//!
72//! // Check for a winner
73//! match game.result() {
74//!     GameResult::Finished { winner } => println!("Winner: {:?}", winner),
75//!     GameResult::InProgress => println!("Game still in progress"),
76//! }
77//! ```
78//!
79//! # Advanced Usage
80//!
81//! For lower-level control, you can work directly with the `Board` API:
82//!
83//! ```rust
84//! use secondbest::prelude::*;
85//!
86//! let mut board = Board::default();
87//!
88//! // Place pieces
89//! board.put(Position::N, Color::B).unwrap();
90//!
91//! // Check board state
92//! let pieces_at_north = board.get_pieces_at(Position::N);
93//! let black_piece_count = board.count_pieces(Color::B);
94//!
95//! // Check for winning conditions
96//! if board.lines_up(Color::B) {
97//!     println!("Black has won!");
98//! }
99//! ```
100//!
101//! # Error Handling
102//!
103//! The library provides detailed error types for various operations:
104//!
105//! ```rust
106//! use secondbest::prelude::*;
107//! use secondbest::error::GameError;
108//!
109//! let mut game = Game::new();
110//! let result = game.apply_action(Action::Put(Position::N, Color::B));
111//!
112//! match result {
113//!     Ok(()) => println!("Action applied successfully"),
114//!     Err(GameError::IllegalAction(_)) => println!("Illegal action"),
115//!     Err(GameError::GameAlreadyOver) => println!("Game is already over"),
116//!     Err(GameError::BoardError(_)) => println!("Board error occurred"),
117//!     Err(_) => println!("Other error occurred"),
118//! }
119//! ```
120
121// 公開するモジュール
122pub mod board;
123pub mod error;
124pub mod game;
125
126/// Prelude module that re-exports commonly used types and functions.
127///
128/// This module provides a convenient way to import the most frequently used
129/// components of the library with a single `use` statement.
130///
131/// # Examples
132///
133/// ```
134/// use secondbest::prelude::*;
135///
136/// // Now you can use Board, Color, Position, etc. directly
137/// let mut board = Board::default();
138/// let action = Action::Put(Position::N, Color::B);
139/// ```
140pub mod prelude {
141    pub use crate::board::{Action, Board, Color, Position};
142    pub use crate::error::Error;
143    pub use crate::game::{Game, GameResult};
144}