1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//! Top-level object for managing [`Board`]s, applying and managing turns

use crate::board::Board;

extern crate wasm_bindgen;
use wasm_bindgen::prelude::*;

use crate::log;

use crate::board::{Square, BrdIdx};
use crate::board::enums::{SquareState, Moveable, Team};
use crate::paint::Painter;
use crate::comp::Computer;

use Team::*;
use SquareState::*;

use std::fmt::{Display};

#[cfg(test)] pub mod tests;

/// Root-level structure for managing the game as a collection of board states
#[wasm_bindgen]
#[derive(Debug)]
pub struct Game {
    current: Board,
    selected_piece: Option<BrdIdx>,
    previous_boards: Vec<Board>,
    painter: Option<Painter>,
    search_depth: usize,
    pub last_node_count: usize,
    pub perfect_chance: f64,
}

impl Game {
    /// Get a read-only copy of a previous turn's board
    pub fn previous_board(&self, turn: usize) -> &Board {
        &self.previous_boards[turn]
    }

    /// Set current board to given
    pub fn current_board(&self) -> &Board {
        &self.current
    }
}

#[wasm_bindgen]
impl Game {
    /// Get pointer to current board's squares
    pub fn current_board_cells(&self) -> *const Square {
        self.current.cells()
    }

    /// Get pointer to current board's squares
    pub fn current_board_len(&self) -> usize {
        self.current.num_cells()
    }

    /// Current turn's team
    pub fn current_turn(&self) -> Team {
        self.current.current_turn
    }

    /// Current board's score
    pub fn score(&self) -> isize {
        self.current.score()
    }

    /// Get currently winning player
    pub fn winning(&self) -> Option<Team> {
        let current_score = self.score();

        if current_score < 0 {
            Some(White)
        } else if current_score == 0 {
            None
        } else {
            Some(Black)
        }
    }

    /// Check if a player has won
    pub fn has_won(&self) -> Option<Team> {

        if self.current.num_player(White) == 0 {
            Some(Black)
        } else if self.current.num_player(Black) == 0 {
            Some(White)
        } else {
            None
        }
    }

    /// Get square on current board for given index
    pub fn current_cell_state(&self, idx: &BrdIdx) -> Square {
        self.current.cell(self.current.cell_idx(*idx))
    }

    /// Set tree depth for AI to search to
    pub fn set_search_depth(&mut self, search_depth: usize) {
        self.search_depth = search_depth;
    }

    /// Set given index as selected piece
    /// TODO: Check whether valid square?
    pub fn set_selected(&mut self, idx: &BrdIdx) {
        
        if self.current.cell(self.current.cell_idx(*idx)).state != Occupied {
            panic!("Tried to select an unoccupied or empty square");
        } 

        self.selected_piece = Some(*idx);
        match &mut self.painter {
            Some(p) => p.set_selected(&Some(*idx)),
            None => {},
        }
    }

    /// Set proportion of perfect moves from AI
    pub fn set_perfect_chance(&mut self, new_chance: f64) {
        self.perfect_chance = new_chance;
    }

    /// Clear currently selected piece
    pub fn clear_selected(&mut self) {
        self.selected_piece = None;
        match &mut self.painter {
            Some(p) => p.set_selected(&None),
            None => {},
        }
    }

    /// Attempt to make a move given a source and destination index
    pub fn make_move(&mut self, from: BrdIdx, to: BrdIdx) -> Moveable {
        let able = self.current.can_move(from, to);

        if let Moveable::Allowed = able {
            let (_, col_diff) = Board::idx_diffs(from, to);
            // MOVE
            if col_diff.abs() == 1 {
                self.execute_move(from, to);
            } 
            // JUMP
            else {
                self.execute_jump(from, to);
            }

        } else {
            log!("Unable to make move, {:?}", able);
        }

        able
    }

    /// Update board state with given move and push new board into current state
    pub fn execute_move(&mut self, from: BrdIdx, to: BrdIdx) {
        // set new board to current and push current to stack
        self.push_new_board(self.current.apply_move(from, to));
    }

    /// Update board state with given jump move and push new board into current state
    pub fn execute_jump(&mut self, from: BrdIdx, to: BrdIdx) {
        // set new board to current and push current to stack
        self.push_new_board(self.current.apply_jump(from, to));
    }

    /// Push current board into the previous turns and set given board to current
    pub fn push_new_board(&mut self, board: Board) {
        self.previous_boards.push(self.current.clone());
        self.set_current(board);
    }

    /// Set current board to given
    pub fn set_current(&mut self, board: Board) {
        self.current = board;
    }

    /// Get new game without board renderer
    #[wasm_bindgen(constructor)]
    pub fn new(width: usize, height: usize, piece_rows: usize, first_turn: Team, search_depth: usize) -> Game {
        Game {
            current: Board::init_game(
                Board::new(width, height, first_turn), piece_rows,
            ),
            selected_piece: None,
            previous_boards: Vec::with_capacity(10),
            painter: None,
            search_depth,
            last_node_count: 0,
            perfect_chance: 0.5,
        }
    }

    /// Get a new game with canvas ID and dimensions
    pub fn new_with_canvas(width: usize, height: usize, piece_rows: usize, first_turn: Team, search_depth: usize, canvas_id: &str, canvas_width: u32, canvas_height: u32) -> Game {
        Game {
            current: Board::init_game(
                Board::new(width, height, first_turn), piece_rows,
            ),
            selected_piece: None,
            previous_boards: Vec::with_capacity(10),
            painter: Some(
                Painter::new(canvas_width, canvas_height, canvas_id)
            ),
            search_depth,
            last_node_count: 0,
            perfect_chance: 0.5,
        }
    }

    /// Set painter for rendering boards
    pub fn set_painter(&mut self, value: Painter) {
        self.painter = Some(value);
    }

    /// Draw current board using painter if exists
    pub fn draw(&self) {
        match &self.painter {
            Some(p) => p.draw(&self.current),
            None => log!("No painter to draw board with")
        }
    }

    /// Create computer, get move from current board and update current board
    pub fn ai_move(&mut self) {
        
        let mut comp = Computer::new(self.search_depth, self.current.current_turn, self.perfect_chance);

        let new_brd = comp.get_move(self.current.clone());

        self.last_node_count = comp.last_node_count;

        match new_brd {
            Some(brd) => self.push_new_board(brd),
            None => {
                log!("No possible moves, re-pushing current board");

                let mut new_brd = self.current.clone();
                new_brd.current_turn = new_brd.current_turn.opponent();

                self.push_new_board(new_brd);
            },
        }
    }
}

impl Display for Game {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result{
        write!(f, "{}", self.current)
    }
}