nits
This commit is contained in:
parent
a5aed91392
commit
7ec23f1bab
60
src/board.rs
60
src/board.rs
@ -114,11 +114,11 @@ impl fmt::Display for Board {
|
|||||||
writeln!(f, " {}", horiz_sep_line)?;
|
writeln!(f, " {}", horiz_sep_line)?;
|
||||||
|
|
||||||
// Print the current score
|
// Print the current score
|
||||||
let (white_score, black_score) = self.get_score();
|
|
||||||
writeln!(
|
writeln!(
|
||||||
f,
|
f,
|
||||||
"White Score: {}\nBlack Score: {}",
|
"White Score: {}\nBlack Score: {}",
|
||||||
white_score, black_score
|
self.count(Piece::White),
|
||||||
|
self.count(Piece::Black)
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
@ -159,11 +159,31 @@ impl Board {
|
|||||||
Self::all_positions().filter(move |&(i, j)| self.would_prop(i, j, color))
|
Self::all_positions().filter(move |&(i, j)| self.would_prop(i, j, color))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get a reference to a backing [`BitBoard`]
|
||||||
|
pub fn board(&self, color: Piece) -> &BitBoard {
|
||||||
|
match color {
|
||||||
|
Piece::Black => &self.black_board,
|
||||||
|
Piece::White => &self.white_board,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get a mutable reference to a backing [`BitBoard`]
|
||||||
|
pub fn board_mut(&mut self, color: Piece) -> &mut BitBoard {
|
||||||
|
match color {
|
||||||
|
Piece::Black => &mut self.black_board,
|
||||||
|
Piece::White => &mut self.white_board,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_piece(&self, i: usize, j: usize, color: Piece) -> bool {
|
||||||
|
self.board(color).get(i, j)
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the color of a place on the [`Board`] at a position
|
/// Returns the color of a place on the [`Board`] at a position
|
||||||
pub fn get(&self, i: usize, j: usize) -> Option<Piece> {
|
pub fn get(&self, i: usize, j: usize) -> Option<Piece> {
|
||||||
if self.white_board.get(i, j) {
|
if self.get_piece(i, j, Piece::White) {
|
||||||
Some(Piece::White)
|
Some(Piece::White)
|
||||||
} else if self.black_board.get(i, j) {
|
} else if self.get_piece(i, j, Piece::Black) {
|
||||||
Some(Piece::Black)
|
Some(Piece::Black)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
@ -172,16 +192,8 @@ impl Board {
|
|||||||
|
|
||||||
/// Place a piece without checking for propegation of validity
|
/// Place a piece without checking for propegation of validity
|
||||||
fn place_unchecked(&mut self, i: usize, j: usize, piece: Piece) {
|
fn place_unchecked(&mut self, i: usize, j: usize, piece: Piece) {
|
||||||
match piece {
|
self.board_mut(piece).set(i, j, true);
|
||||||
Piece::Black => {
|
self.board_mut(!piece).set(i, j, false);
|
||||||
self.black_board.set(i, j, true);
|
|
||||||
self.white_board.set(i, j, false);
|
|
||||||
}
|
|
||||||
Piece::White => {
|
|
||||||
self.black_board.set(i, j, false);
|
|
||||||
self.white_board.set(i, j, true);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a modified [`Board`] with the piece placed at a position
|
/// Return a modified [`Board`] with the piece placed at a position
|
||||||
@ -242,14 +254,14 @@ impl Board {
|
|||||||
.iter()
|
.iter()
|
||||||
.filter_map(move |chain| {
|
.filter_map(move |chain| {
|
||||||
let mut end_idx = None;
|
let mut end_idx = None;
|
||||||
for (idx, &(new_i, new_j)) in chain.iter().enumerate() {
|
for (idx, &(new_i, new_j)) in chain.into_iter().enumerate() {
|
||||||
let piece = self.get(new_i, new_j)?;
|
let piece = self.get(new_i, new_j)?;
|
||||||
if piece == starting_color {
|
if piece == starting_color {
|
||||||
end_idx = Some(idx);
|
end_idx = Some(idx);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end_idx.map(|idx| &chain[..idx])
|
end_idx.and_then(|idx| chain.get(..idx))
|
||||||
})
|
})
|
||||||
.flatten()
|
.flatten()
|
||||||
.cloned()
|
.cloned()
|
||||||
@ -257,22 +269,15 @@ impl Board {
|
|||||||
|
|
||||||
/// Count the number of a type of [`Piece`] on the board
|
/// Count the number of a type of [`Piece`] on the board
|
||||||
pub fn count(&self, piece: Piece) -> usize {
|
pub fn count(&self, piece: Piece) -> usize {
|
||||||
match piece {
|
self.board(piece).count()
|
||||||
Piece::Black => self.black_board,
|
|
||||||
Piece::White => self.white_board,
|
|
||||||
}
|
|
||||||
.count()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Get the "net score" of a player
|
||||||
|
/// Formula: `net_score = Score_player - Score_opponent`
|
||||||
pub fn net_score(&self, piece: Piece) -> isize {
|
pub fn net_score(&self, piece: Piece) -> isize {
|
||||||
self.count(piece) as isize - self.count(!piece) as isize
|
self.count(piece) as isize - self.count(!piece) as isize
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns (White score, Black score)
|
|
||||||
pub fn get_score(&self) -> (usize, usize) {
|
|
||||||
(self.count(Piece::White), self.count(Piece::Black))
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns the winner of the board (if any)
|
/// Returns the winner of the board (if any)
|
||||||
pub fn game_winner(&self, turn: Piece) -> Winner {
|
pub fn game_winner(&self, turn: Piece) -> Winner {
|
||||||
// Wikipedia: `Players take alternate turns. If one player cannot make a valid move, play passes back to the other player. The game ends when the grid has filled up or if neither player can make a valid move.`
|
// Wikipedia: `Players take alternate turns. If one player cannot make a valid move, play passes back to the other player. The game ends when the grid has filled up or if neither player can make a valid move.`
|
||||||
@ -283,8 +288,7 @@ impl Board {
|
|||||||
return Winner::None;
|
return Winner::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (white_score, black_score) = self.get_score();
|
match self.count(Piece::White).cmp(&self.count(Piece::Black)) {
|
||||||
match white_score.cmp(&black_score) {
|
|
||||||
Ordering::Greater => Winner::Player(Piece::White), // White win
|
Ordering::Greater => Winner::Player(Piece::White), // White win
|
||||||
Ordering::Less => Winner::Player(Piece::Black), // Black win
|
Ordering::Less => Winner::Player(Piece::Black), // Black win
|
||||||
Ordering::Equal => Winner::Tie,
|
Ordering::Equal => Winner::Tie,
|
||||||
|
|||||||
@ -363,7 +363,9 @@ impl Agent for ComplexAgent {
|
|||||||
println!("# of moves stored: {}", self.future_moves.arena.len());
|
println!("# of moves stored: {}", self.future_moves.arena.len());
|
||||||
|
|
||||||
self.future_moves.best_move().inspect(|&(i, j)| {
|
self.future_moves.best_move().inspect(|&(i, j)| {
|
||||||
self.future_moves.update_root_coord(i, j);
|
if !self.future_moves.update_root_coord(i, j) {
|
||||||
|
panic!("update_root_coord failed");
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -12,8 +12,8 @@ mod piece;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let player1 = complexagent::ComplexAgent::new(Piece::Black);
|
let player1 = complexagent::ComplexAgent::new(Piece::Black);
|
||||||
// let player2 = complexagent::ComplexAgent::new(Piece::White);
|
// let player2 = complexagent::ComplexAgent::new(Piece::White);
|
||||||
// let player2 = agent::ManualAgent::new(Piece::White);
|
let player2 = agent::ManualAgent::new(Piece::White);
|
||||||
let player2 = agent::RandomAgent::new(Piece::White);
|
// let player2 = agent::RandomAgent::new(Piece::White);
|
||||||
let mut game = Game::new(Box::new(player1), Box::new(player2));
|
let mut game = Game::new(Box::new(player1), Box::new(player2));
|
||||||
game.game_loop();
|
game.game_loop();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user