othello_agent/simulate/
history.rs

1use crate::gameplay::constants::CODE_CHARS;
2use crate::gameplay::types::IBoard;
3use crate::gameplay::encoding::string_from_board;
4use crate::gameplay::utils::augmented_score_for_player;
5
6pub struct GameHistory {
7    // board history.. vector of encoded boards
8    pub board_history: Vec<String>,
9    // total number of moves
10    pub total_moves: u16,
11    // scores for both players
12    pub agent0_score: i16,
13    pub agent1_score: i16,
14    // id of game... should be autoincremented
15    pub id: u32,
16}
17
18impl GameHistory {
19    pub fn new() -> Self {
20        GameHistory {
21            board_history: Vec::new(),
22            total_moves: 0,
23            agent0_score: 0,
24            agent1_score: 0,
25            id: 0,
26        }
27    }
28    pub fn set_scores(&mut self, agent0_score: i16, agent1_score: i16) {
29        self.agent0_score = agent0_score;
30        self.agent1_score = agent1_score;
31    }
32    pub fn set_id(&mut self, id: u32) {
33        self.id = id;
34    }
35    pub fn add_board(&mut self, board: IBoard, set_scores: bool) {
36        self.board_history.push(string_from_board(board, CODE_CHARS));
37        self.total_moves += 1;
38        if !set_scores {
39            return;
40        }
41        // compute scores for both players
42        let agent0_score = augmented_score_for_player(board, 0, 1, 1, 1);
43        let agent1_score = augmented_score_for_player(board, 1, 1, 1, 1);
44        self.set_scores(agent0_score, agent1_score);
45    }
46}
47
48// add method to print summary of game history
49// summary will include total moves, scores, and id
50impl std::fmt::Display for GameHistory {
51    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
52        write!(
53            f,
54            "Game History: id: {}, total moves: {}, agent0 score: {}, agent1 score: {}",
55            self.id,
56            self.total_moves,
57            self.agent0_score,
58            self.agent1_score
59        )
60    }
61}
62
63pub struct GameHistoryStore {
64    pub history: Vec<GameHistory>,
65    pub total_games: u32,
66}
67
68impl GameHistoryStore {
69    pub fn new() -> Self {
70        GameHistoryStore {
71            history: Vec::new(),
72            total_games: 0,
73        }
74    }
75    // print summary of all games in history
76    pub fn print_summary(&self) {
77        println!("Game History SUmmary");
78        println!("Total games: {}", self.total_games);
79        for game in self.history.iter() {
80            println!("{}", game);
81        }
82    }
83    // add game to history
84    pub fn add_game(&mut self, game: GameHistory) {
85        self.history.push(game);
86        self.total_games += 1;
87    }
88
89    // get last game in history
90    pub fn last_game(&self) -> Option<&GameHistory> {
91        self.history.last()
92    }
93}