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)?;
|
||||
|
||||
// Print the current score
|
||||
let (white_score, black_score) = self.get_score();
|
||||
writeln!(
|
||||
f,
|
||||
"White Score: {}\nBlack Score: {}",
|
||||
white_score, black_score
|
||||
self.count(Piece::White),
|
||||
self.count(Piece::Black)
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
@ -159,11 +159,31 @@ impl Board {
|
||||
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
|
||||
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)
|
||||
} else if self.black_board.get(i, j) {
|
||||
} else if self.get_piece(i, j, Piece::Black) {
|
||||
Some(Piece::Black)
|
||||
} else {
|
||||
None
|
||||
@ -172,16 +192,8 @@ impl Board {
|
||||
|
||||
/// Place a piece without checking for propegation of validity
|
||||
fn place_unchecked(&mut self, i: usize, j: usize, piece: Piece) {
|
||||
match piece {
|
||||
Piece::Black => {
|
||||
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);
|
||||
}
|
||||
}
|
||||
self.board_mut(piece).set(i, j, true);
|
||||
self.board_mut(!piece).set(i, j, false);
|
||||
}
|
||||
|
||||
/// Return a modified [`Board`] with the piece placed at a position
|
||||
@ -242,14 +254,14 @@ impl Board {
|
||||
.iter()
|
||||
.filter_map(move |chain| {
|
||||
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)?;
|
||||
if piece == starting_color {
|
||||
end_idx = Some(idx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
end_idx.map(|idx| &chain[..idx])
|
||||
end_idx.and_then(|idx| chain.get(..idx))
|
||||
})
|
||||
.flatten()
|
||||
.cloned()
|
||||
@ -257,22 +269,15 @@ impl Board {
|
||||
|
||||
/// Count the number of a type of [`Piece`] on the board
|
||||
pub fn count(&self, piece: Piece) -> usize {
|
||||
match piece {
|
||||
Piece::Black => self.black_board,
|
||||
Piece::White => self.white_board,
|
||||
}
|
||||
.count()
|
||||
self.board(piece).count()
|
||||
}
|
||||
|
||||
/// Get the "net score" of a player
|
||||
/// Formula: `net_score = Score_player - Score_opponent`
|
||||
pub fn net_score(&self, piece: Piece) -> 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)
|
||||
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.`
|
||||
@ -283,8 +288,7 @@ impl Board {
|
||||
return Winner::None;
|
||||
}
|
||||
|
||||
let (white_score, black_score) = self.get_score();
|
||||
match white_score.cmp(&black_score) {
|
||||
match self.count(Piece::White).cmp(&self.count(Piece::Black)) {
|
||||
Ordering::Greater => Winner::Player(Piece::White), // White win
|
||||
Ordering::Less => Winner::Player(Piece::Black), // Black win
|
||||
Ordering::Equal => Winner::Tie,
|
||||
|
||||
@ -363,7 +363,9 @@ impl Agent for ComplexAgent {
|
||||
println!("# of moves stored: {}", self.future_moves.arena.len());
|
||||
|
||||
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() {
|
||||
let player1 = complexagent::ComplexAgent::new(Piece::Black);
|
||||
// let player2 = complexagent::ComplexAgent::new(Piece::White);
|
||||
// let player2 = agent::ManualAgent::new(Piece::White);
|
||||
let player2 = agent::RandomAgent::new(Piece::White);
|
||||
let player2 = agent::ManualAgent::new(Piece::White);
|
||||
// let player2 = agent::RandomAgent::new(Piece::White);
|
||||
let mut game = Game::new(Box::new(player1), Box::new(player2));
|
||||
game.game_loop();
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user