diff --git a/src/game.rs b/src/game.rs index b023f8a..fbcd3ac 100644 --- a/src/game.rs +++ b/src/game.rs @@ -199,7 +199,7 @@ impl Game { &self.dictionary } - pub fn receive_play(&mut self, tray_tile_locations: Vec>, commit_move: bool) -> Result { + pub fn receive_play(&mut self, tray_tile_locations: Vec>, commit_move: bool) -> Result { let player = self.current_player_name(); @@ -230,7 +230,7 @@ impl Game { .collect(); if commit_move { - let mut player_state = self.player_states.get_player_state_mut(&player).unwrap(); + let player_state = self.player_states.get_player_state_mut(&player).unwrap(); player_state.score += total_score; player_state.tray = tray; @@ -241,13 +241,15 @@ impl Game { self.increment_turn(); } - Ok(ScoreResult { - words, - total: total_score, + Ok(TurnAction::PlayTiles { + result: ScoreResult { + words, + total: total_score, + }, }) } - pub fn exchange_tiles(&mut self, tray_tile_locations: Vec) -> Result { + pub fn exchange_tiles(&mut self, tray_tile_locations: Vec) -> Result<(Tray, TurnAction), String> { let player = self.current_player_name(); let tray = match self.player_states.get_tray_mut(&player) { None => {return Err(format!("Player {} not found", player))} @@ -259,6 +261,7 @@ impl Game { } let tile_pool = &mut self.tile_pool; + let mut tiles_exchanged = 0; for (i, played_tile) in tray_tile_locations.iter().enumerate() { if *played_tile { @@ -266,6 +269,7 @@ impl Game { if letter.is_some() { tile_pool.push(letter.unwrap().clone()); *letter = None; + tiles_exchanged += 1; } } } @@ -277,7 +281,7 @@ impl Game { self.increment_turn(); - Ok(tray) + Ok((tray, TurnAction::ExchangeTiles { tiles_exchanged })) } @@ -308,7 +312,6 @@ impl Game { None => { // no available moves; exchange all tiles let mut to_exchange = Vec::with_capacity(TRAY_LENGTH as usize); - let mut tiles_exchanged = 0; for tile_spot in tray.letters.iter() { match tile_spot { None => { @@ -316,21 +319,21 @@ impl Game { }, Some(_) => { to_exchange.push(true); - tiles_exchanged += 1; } } } if self.tile_pool.is_empty(){ + self.increment_turn(); Ok(TurnAdvanceResult::AIMove { name: current_player, action: TurnAction::Pass, }) } else { - self.exchange_tiles(to_exchange)?; + let (_, action) = self.exchange_tiles(to_exchange)?; Ok(TurnAdvanceResult::AIMove { name: current_player, - action: TurnAction::ExchangeTiles{tiles_exchanged}, + action, }) } @@ -338,16 +341,15 @@ impl Game { } Some(best_move) => { let play = best_move.convert_to_play(tray); - let score_result = self.receive_play(play, true)?; + let action = self.receive_play(play, true)?; Ok(TurnAdvanceResult::AIMove { name: current_player, - action: TurnAction::PlayTiles{result: score_result}, + action, }) } } } else { Ok(TurnAdvanceResult::HumanInputRequired{name: self.current_player_name()}) - } @@ -407,6 +409,8 @@ mod tests { let result = game.advance_turn(); println!("AI move is {result:?}"); + assert_eq!(game.current_player_name(), "Player"); + } } \ No newline at end of file diff --git a/src/wasm.rs b/src/wasm.rs index 1b128e6..186b456 100644 --- a/src/wasm.rs +++ b/src/wasm.rs @@ -113,10 +113,10 @@ impl GameWasm { let tray_tile_locations: Vec = serde_wasm_bindgen::from_value(tray_tile_locations)?; match self.0.exchange_tiles(tray_tile_locations) { - Ok(tray) => { + Ok((_, turn_action)) => { serde_wasm_bindgen::to_value(&MyResult { response_type: ResponseType::OK, - value: tray + value: turn_action }) }, Err(e) => { diff --git a/ui/src/Game.tsx b/ui/src/Game.tsx index f12d74c..04c8d08 100644 --- a/ui/src/Game.tsx +++ b/ui/src/Game.tsx @@ -6,7 +6,7 @@ import { PlayedTile, PlayerAndScore, ScoreResult, - Tray, TurnAdvanceResult + Tray, TurnAction, TurnAdvanceResult } from "word_grid"; import { LocationType, @@ -117,20 +117,14 @@ export function Game(props: { function exchangeFunction(selectedArray: Array) { - let numSelected = 0; - selectedArray.forEach((x) => { - if (x){ - numSelected++; - } - }) - - const result: MyResult = props.wasm.exchange_tiles(selectedArray); + const result: MyResult = props.wasm.exchange_tiles(selectedArray); console.log({result}); if(result.response_type === "ERR") { logDispatch(
{(result.value as string)}
); } else { - logDispatch(
You exchanged {numSelected} tiles.
); + const action = result.value as {type: "ExchangeTiles"; tiles_exchanged: number;}; + logDispatch(
You exchanged {action.tiles_exchanged} tiles.
); setTurnCount(turnCount + 1); } @@ -181,7 +175,7 @@ export function Game(props: { return null; }); - const result: MyResult = props.wasm.receive_play(playedTiles, confirmedScorePoints > -1); + const result: MyResult<{ type: "PlayTiles"; result: ScoreResult } | string> = props.wasm.receive_play(playedTiles, confirmedScorePoints > -1); console.log({result}); if(result.response_type === "ERR") { @@ -196,7 +190,7 @@ export function Game(props: { } else { - const score_result = result.value as ScoreResult; + const score_result = (result.value as { type: "PlayTiles"; result: ScoreResult }).result; const total_points = score_result.total; let msg: string;