From 5e4e240e3366b3093a1e941f7ef919d291a6f762 Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Fri, 18 Apr 2025 23:55:11 -0400 Subject: [PATCH] fix GameInner::new error handling --- src/elo.rs | 1 + src/game.rs | 3 ++- src/game_inner.rs | 20 +++++++++----------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/elo.rs b/src/elo.rs index bb6cdbd..8b5d5b2 100644 --- a/src/elo.rs +++ b/src/elo.rs @@ -295,6 +295,7 @@ impl PlayerArena { false, Board::random(rand::random_range(3..=7)), ) + .expect("unable to create game") .loop_until_result(); match result { diff --git a/src/game.rs b/src/game.rs index a86a214..e5462fb 100644 --- a/src/game.rs +++ b/src/game.rs @@ -8,7 +8,8 @@ pub struct Game { impl Game { pub fn new(player1: Box, player2: Box) -> 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"), } } diff --git a/src/game_inner.rs b/src/game_inner.rs index 627aa23..e31d624 100644 --- a/src/game_inner.rs +++ b/src/game_inner.rs @@ -41,26 +41,24 @@ impl GameInner { player2: Box, do_print: bool, board: Board, - ) -> Self { + ) -> Result { 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"); + if player1_color != Piece::Black { + 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], board, do_print, move_log: Vec::new(), first_board: board, - } + }) } // Handle when a move is made