Skip to content

Commit d6b59bf

Browse files
committed
remove 'Game' impls, since they are no longer needed, and fix tests.
1 parent 2d4b3ea commit d6b59bf

File tree

8 files changed

+6
-267
lines changed

8 files changed

+6
-267
lines changed

Diff for: game_runner/src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lib_agents::{HumanAgent, MctsAgent, RandomAgent};
1+
use lib_agents::{HumanAgent, MctsAgent};
22
use lib_boardgame::{GameRunner, GeneralGameRunner, PlayerColor};
33
use lib_connect_four::ConnectFourState;
44
use lib_reversi::ReversiState;

Diff for: lib_agents/src/mcts_agent/agent.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -258,20 +258,12 @@ where
258258
mod tests {
259259
use super::*;
260260

261-
use lib_boardgame::{Game, GameState};
262-
use lib_tic_tac_toe::tic_tac_toe::TicTacToe;
263-
use lib_tic_tac_toe::tic_tac_toe_gamestate::{BoardPosition, TicTacToeAction};
261+
use lib_boardgame::GameState;
262+
use lib_tic_tac_toe::tic_tac_toe_gamestate::{BoardPosition, TicTacToeAction, TicTacToeState};
264263

265264
#[test]
266265
fn tree_search_always_picks_winning_move() {
267-
let black_agent: Box<MctsAgent<_, ArcNode<_>>> =
268-
Box::new(MctsAgent::new(PlayerColor::Black));
269-
let white_agent: Box<MctsAgent<_, ArcNode<_>>> =
270-
Box::new(MctsAgent::new(PlayerColor::White));
271-
272-
let mut game = TicTacToe::new(white_agent, black_agent);
273-
274-
let state = game.game_state_mut();
266+
let mut state = TicTacToeState::initial_state();
275267

276268
// Start with black's turn
277269
assert_eq!(state.current_player_turn(), PlayerColor::Black);
@@ -310,7 +302,7 @@ mod tests {
310302
let legal_moves = state.legal_moves(PlayerColor::Black);
311303

312304
let test_black_agent: MctsAgent<_, ArcNode<_>> = MctsAgent::new(PlayerColor::Black);
313-
let mcts_chosen_move = test_black_agent.pick_move(state, &legal_moves);
305+
let mcts_chosen_move = test_black_agent.pick_move(&state, &legal_moves);
314306

315307
// The agent MUST pick the winning move:
316308
// V

Diff for: lib_boardgame/src/lib.rs

-80
Original file line numberDiff line numberDiff line change
@@ -133,86 +133,6 @@ pub trait GameState: Clone + Display {
133133
}
134134
}
135135

136-
/// A trait that describes the high-level behavior of running a game,
137-
/// i.e. managing the underlying game state.
138-
pub trait Game {
139-
/// The underlying game state for this game.
140-
type State: GameState;
141-
142-
/// Returns a handle to the white player agent.
143-
fn white_agent(&self) -> &dyn GameAgent<Self::State>;
144-
145-
/// Returns a handle to the black player agent.
146-
fn black_agent(&self) -> &dyn GameAgent<Self::State>;
147-
148-
/// The game's current state.
149-
fn game_state(&self) -> &Self::State;
150-
151-
/// The game's current state, mutable.
152-
fn game_state_mut(&mut self) -> &mut Self::State;
153-
154-
/// True if the the game has ended, either due to a forced win,
155-
/// draw, or forfeit.
156-
fn is_game_over(&self) -> bool;
157-
158-
/// The GameResult, or None if the game is not yet over.
159-
fn game_result(&self) -> Option<GameResult>;
160-
161-
/// Invokes the current player agent to pick a move,
162-
/// then updates the game state with the result.
163-
fn player_take_turn(&mut self, player: PlayerColor) {
164-
let state = self.game_state();
165-
let legal_moves = state.legal_moves(player);
166-
167-
let picked_action = match player {
168-
PlayerColor::Black => self.black_agent().pick_move(state, legal_moves),
169-
PlayerColor::White => self.white_agent().pick_move(state, legal_moves),
170-
};
171-
172-
if legal_moves.iter().find(|&&m| m == picked_action).is_none() {
173-
panic!("Agent provided a move that is illegal.");
174-
}
175-
176-
if legal_moves.len() == 1 && legal_moves[0].is_forced_pass() {
177-
out!(
178-
"Player {:?} has no options, so they pass their turn.",
179-
player
180-
);
181-
}
182-
183-
out!("Player {:?} picked move {:?}", player, picked_action);
184-
185-
let state = self.game_state_mut();
186-
187-
state.apply_move(picked_action);
188-
189-
// Now both players get a chance to observe the selected action and resulting state.
190-
let state_copy = state.clone();
191-
self.white_agent()
192-
.observe_action(player, picked_action, &state_copy);
193-
self.black_agent()
194-
.observe_action(player, picked_action, &state_copy);
195-
}
196-
197-
/// Applies each player's turn one at a time until the game is over,
198-
/// and returns the game result.
199-
fn play_to_end(&mut self) -> GameResult {
200-
self.game_state_mut().initialize_board();
201-
out!("Board initialized.");
202-
203-
while !self.is_game_over() {
204-
out!("{}", self.game_state().human_friendly());
205-
let cur_player_color = self.game_state().current_player_turn();
206-
self.player_take_turn(cur_player_color);
207-
}
208-
209-
out!("{}", self.game_state().human_friendly());
210-
211-
self.game_result()
212-
.expect("The game is over, so there must be a game result.")
213-
}
214-
}
215-
216136
/// A trait representing the functionality of a GameAgent.
217137
/// Most importantly, given a GameState, a GameAgent must be able to decide a GameMove.
218138
pub trait GameAgent<TState: GameState> {

Diff for: lib_connect_four/src/connect_four.rs

+1-53
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use lib_boardgame::{Game, GameAgent, GameResult, GameState, PlayerColor};
1+
use lib_boardgame::{GameResult, GameState, PlayerColor};
22
use std::fmt::Display;
33

44
const GAME_WIDTH: usize = 7;
@@ -307,58 +307,6 @@ impl GameState for ConnectFourState {
307307
}
308308
}
309309

310-
pub struct ConnectFour {
311-
white_agent: Box<dyn GameAgent<ConnectFourState>>,
312-
black_agent: Box<dyn GameAgent<ConnectFourState>>,
313-
game_state: ConnectFourState,
314-
}
315-
316-
impl ConnectFour {
317-
pub fn new(
318-
white_agent: Box<dyn GameAgent<ConnectFourState>>,
319-
black_agent: Box<dyn GameAgent<ConnectFourState>>,
320-
) -> Self {
321-
Self {
322-
white_agent,
323-
black_agent,
324-
game_state: ConnectFourState::initial_state(),
325-
}
326-
}
327-
}
328-
329-
impl Game for ConnectFour {
330-
type State = ConnectFourState;
331-
332-
fn white_agent(&self) -> &dyn GameAgent<ConnectFourState> {
333-
&*self.white_agent
334-
}
335-
336-
fn black_agent(&self) -> &dyn GameAgent<ConnectFourState> {
337-
&*self.black_agent
338-
}
339-
340-
/// The game's current state.
341-
fn game_state(&self) -> &Self::State {
342-
&self.game_state
343-
}
344-
345-
/// The game's current state.
346-
fn game_state_mut(&mut self) -> &mut Self::State {
347-
&mut self.game_state
348-
}
349-
350-
/// True if the the game has ended, either due to a forced win,
351-
/// draw, or forfeit.
352-
fn is_game_over(&self) -> bool {
353-
self.game_state.is_game_over()
354-
}
355-
356-
/// The GameResult, or None if the game is not yet over.
357-
fn game_result(&self) -> Option<GameResult> {
358-
self.game_state.game_result
359-
}
360-
}
361-
362310
struct Position {
363311
x: usize,
364312
y: usize,

Diff for: lib_reversi/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
mod reversi_action;
22
mod reversi_board;
3-
mod reversi_game;
43
mod reversi_gamestate;
54
mod util;
65

76
use reversi_board::{Board, Directions, BOARD_SIZE};
87

98
pub use reversi_action::ReversiPlayerAction;
109
pub use reversi_board::{BoardPosition, ReversiPiece};
11-
pub use reversi_game::Reversi;
1210
pub use reversi_gamestate::ReversiState;

Diff for: lib_reversi/src/reversi_game.rs

-55
This file was deleted.

Diff for: lib_tic_tac_toe/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub mod tic_tac_toe;
21
pub mod tic_tac_toe_gamestate;
32

43
use lib_boardgame::PlayerColor;

Diff for: lib_tic_tac_toe/src/tic_tac_toe.rs

-63
This file was deleted.

0 commit comments

Comments
 (0)