pub struct Position {
pub position: u64,
pub mask: u64,
/* private fields */
}
Expand description
Represents a Connect Four position compactly as a bitboard.
The standard, 6x7 Connect Four board can be represented unambiguously using 49 bits in the following bit order:
6 13 20 27 34 41 48
---------------------
| 5 12 19 26 33 40 47 |
| 4 11 18 25 32 39 46 |
| 3 10 17 24 31 38 45 |
| 2 9 16 23 30 37 44 |
| 1 8 15 22 29 36 43 |
| 0 7 14 21 28 35 42 |
---------------------
The extra row of bits at the top identifies full columns and prevents bits from overflowing
into the next column. For computational efficiency, positions are stored in practice using two
u64
numbers: one to store a mask of all occupied tiles, and the other to store a mask of the
current player’s tiles.
Fields§
§position: u64
A mask of the current player’s tiles.
mask: u64
A mask of all occupied tiles.
Implementations§
Source§impl Position
impl Position
pub const WIDTH: usize = 7usize
pub const HEIGHT: usize = 6usize
pub const BOARD_SIZE: usize = 42usize
pub const CENTRE: usize = 3usize
pub const MIN_SCORE: i8 = -18i8
pub const MAX_SCORE: i8 = 18i8
Sourcepub fn from_board_string(
board_string: &str,
) -> Result<Position, PositionParsingError>
pub fn from_board_string( board_string: &str, ) -> Result<Position, PositionParsingError>
Parses a Position
from a string representation of the Connect Four board.
The input string should contain exactly 42 characters from the set ['.', 'o', 'x']
,
representing the board row by row from the top-left to the bottom-right. All other
characters are ignored. ‘x’ is treated as the current player, and ‘o’ as the opponent.
This method assumes that a correctly formatted board string is a valid game position.
Invalid game positions will lead to undefined behaviour.
§Arguments
board_string
: A string slice representing the board state.
§Returns
On success, returns a Result
containing the parsed Position
.
§Errors
Returns a PositionParsingError
if the input string is invalid.
§Example
use connect_four_ai::Position;
// A typical board state, represented as a string
let board_string = "\
.......
...o...
..xx...
..ox...
..oox..
..oxxo.
";
// Parses the string as a `Position` instance
let pos = Position::from_board_string(board_string).unwrap();
assert_eq!(pos.get_moves(), 12)
Sourcepub fn from_moves(move_sequence: &str) -> Result<Position, PositionParsingError>
pub fn from_moves(move_sequence: &str) -> Result<Position, PositionParsingError>
Parses a Position
from a string of 1-indexed moves.
The input string should contain a sequence of columns played, indexed from 1.
§Arguments
moves
: A string slice containing the move sequence.
§Returns
On success, returns a Result
containing the parsed Position
.
§Errors
Returns a PositionParsingError
if the move sequence is invalid.
§Example
use connect_four_ai::Position;
// A typical board state, represented as a sequence of moves
let moves = "444343533654";
// Parses the sequence as a `Position` instance
let pos = Position::from_moves(moves).unwrap();
assert_eq!(pos.get_moves(), 12)
Sourcepub fn get_moves(&self) -> usize
pub fn get_moves(&self) -> usize
Returns the number of moves played to reach the current position.
Sourcepub fn get_key(&self) -> u64
pub fn get_key(&self) -> u64
Returns the unique key for the current position.
This key is unique to each pair of horizontally symmetrical positions, as these positions will always have the same solution.
Sourcepub fn is_playable(&self, col: usize) -> bool
pub fn is_playable(&self, col: usize) -> bool
Sourcepub fn is_winning_move(&self, col: usize) -> bool
pub fn is_winning_move(&self, col: usize) -> bool
Sourcepub fn can_win_next(&self) -> bool
pub fn can_win_next(&self) -> bool
Indicates whether the current player can win with their next move.
Sourcepub fn possible(&self) -> u64
pub fn possible(&self) -> u64
Returns a mask for the possible moves the current player can make.
Sourcepub fn possible_non_losing_moves(&self) -> u64
pub fn possible_non_losing_moves(&self) -> u64
Returns a mask for the possible non-losing moves the current player can make.
Sourcepub fn score_move(&self, move_bit: u64) -> u8
pub fn score_move(&self, move_bit: u64) -> u8
Sourcepub fn is_won_position(&self) -> bool
pub fn is_won_position(&self) -> bool
Indicates whether the current position has been won by either player.
Trait Implementations§
impl Copy for Position
Auto Trait Implementations§
impl Freeze for Position
impl RefUnwindSafe for Position
impl Send for Position
impl Sync for Position
impl Unpin for Position
impl UnwindSafe for Position
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more