fix player order
This commit is contained in:
parent
993af2a191
commit
dbd0a97602
30
src/game.rs
30
src/game.rs
@ -1,4 +1,4 @@
|
||||
use crate::{agent::Agent, board::Board};
|
||||
use crate::{agent::Agent, board::Board, piece::Piece};
|
||||
|
||||
pub struct Game {
|
||||
players: [Box<dyn Agent>; 2],
|
||||
@ -21,7 +21,19 @@ impl std::fmt::Display for Game {
|
||||
}
|
||||
|
||||
impl Game {
|
||||
pub const fn new(player1: Box<dyn Agent>, player2: Box<dyn Agent>) -> Self {
|
||||
pub fn new(player1: Box<dyn Agent>, player2: Box<dyn Agent>) -> Self {
|
||||
let player1_color = player1.color();
|
||||
let player2_color = player2.color();
|
||||
assert_ne!(
|
||||
player1_color,
|
||||
player2_color,
|
||||
"Both players cannot have the same color {}",
|
||||
player1_color.text()
|
||||
);
|
||||
|
||||
assert_eq!(player1_color, Piece::Black, "player 1 must playing black");
|
||||
assert_eq!(player2_color, Piece::White, "player 2 must play white");
|
||||
|
||||
Self {
|
||||
players: [player1, player2],
|
||||
board: Board::new().starting_pos(),
|
||||
@ -54,10 +66,13 @@ impl Game {
|
||||
} else {
|
||||
println!("Player {} did not make a move!", player_i);
|
||||
// players are able to skip a move if they have no valid moves to make
|
||||
assert!(
|
||||
!self.board.possible_moves(player_color).any(|_| true),
|
||||
"Player skipped a move, but they had a possible move to make"
|
||||
);
|
||||
if self.board.possible_moves(player_color).any(|_| true) {
|
||||
println!(
|
||||
"Player {} has possible moves, but skipped (invalid)",
|
||||
player_i
|
||||
);
|
||||
continue;
|
||||
}
|
||||
return; // No valid move available
|
||||
}
|
||||
}
|
||||
@ -69,7 +84,7 @@ impl Game {
|
||||
|
||||
loop {
|
||||
// alternate which player plays
|
||||
current_player += 1;
|
||||
|
||||
current_player %= self.players.len();
|
||||
|
||||
println!("{}", self);
|
||||
@ -82,6 +97,7 @@ impl Game {
|
||||
}
|
||||
|
||||
self.step(current_player);
|
||||
current_player += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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