fix GameInner::new error handling

This commit is contained in:
Simon Gardling 2025-04-18 23:55:11 -04:00
parent 625b02c13a
commit 5e4e240e33
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
3 changed files with 12 additions and 12 deletions

View File

@ -295,6 +295,7 @@ impl PlayerArena {
false, false,
Board::random(rand::random_range(3..=7)), Board::random(rand::random_range(3..=7)),
) )
.expect("unable to create game")
.loop_until_result(); .loop_until_result();
match result { match result {

View File

@ -8,7 +8,8 @@ pub struct Game {
impl Game { impl Game {
pub fn new(player1: Box<dyn Agent>, player2: Box<dyn Agent>) -> Self { pub fn new(player1: Box<dyn Agent>, player2: Box<dyn Agent>) -> Self {
Self { Self {
inner: GameInner::new(player1, player2, true, Board::STARTING_POSITION), inner: GameInner::new(player1, player2, true, Board::STARTING_POSITION)
.expect("unable to create game"),
} }
} }

View File

@ -41,26 +41,24 @@ impl GameInner {
player2: Box<dyn Agent>, player2: Box<dyn Agent>,
do_print: bool, do_print: bool,
board: Board, board: Board,
) -> Self { ) -> Result<Self, &'static str> {
let player1_color = player1.color(); let player1_color = player1.color();
let player2_color = player2.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"); if player1_color != Piece::Black {
assert_eq!(player2_color, Piece::White, "player 2 must play white"); return Err("player 1 must play black");
}
if player2_color != Piece::White {
return Err("player 2 must play white");
}
Self { Ok(Self {
players: [player1, player2], players: [player1, player2],
board, board,
do_print, do_print,
move_log: Vec::new(), move_log: Vec::new(),
first_board: board, first_board: board,
} })
} }
// Handle when a move is made // Handle when a move is made