This repository has been archived on 2023-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
Vatu/src/node.rs

60 lines
1.6 KiB
Rust
Raw Normal View History

use std::fmt;
use crate::board;
2020-06-19 19:29:10 +02:00
use crate::movement::Move;
use crate::rules;
use crate::stats;
/// Analysis node: a board along with the game state.
2020-06-19 02:11:01 +02:00
#[derive(Clone, PartialEq)]
pub struct Node {
/// Board for this node.
pub board: board::Board,
/// Game state.
pub game_state: rules::GameState,
}
impl Node {
/// Create a new node for an empty board and a new game state.
pub fn new() -> Node {
Node {
2020-06-19 02:11:01 +02:00
board: board::Board::new_empty(),
game_state: rules::GameState::new(),
}
}
/// Apply a move to this node.
pub fn apply_move(&mut self, m: &Move) {
2020-06-19 19:29:10 +02:00
m.apply_to(&mut self.board, &mut self.game_state);
}
/// Return player moves from this node.
2020-06-21 00:33:05 +02:00
pub fn get_player_legal_moves(&self) -> Vec<Move> {
rules::get_player_moves(&self.board, &self.game_state, false)
}
/// Compute stats for both players for this node.
pub fn compute_stats(&self) -> (stats::BoardStats, stats::BoardStats) {
stats::BoardStats::new_from(&self.board, &self.game_state)
}
}
impl fmt::Debug for Node {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"Node {{ board: [...], game_state: {:?} }}",
self.game_state
)
}
}
impl fmt::Display for Node {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut s = vec!();
2020-06-21 15:57:58 +02:00
self.board.draw_to(&mut s);
let board_drawing = String::from_utf8_lossy(&s).to_string();
2020-06-20 21:05:54 +02:00
write!(f, "* Board:\n{}\nGame state: {}", board_drawing, self.game_state)
}
}