2024-03-24 18:37:35 +01:00
|
|
|
use bot::Bot;
|
2024-02-18 18:40:45 +01:00
|
|
|
use pretty_assertions::assert_eq;
|
2024-03-24 18:37:35 +01:00
|
|
|
use store::{CheckerMove, Color, Dice, DiceRoller, GameEvent, GameState, PlayerId};
|
2024-02-17 12:55:36 +01:00
|
|
|
|
2024-02-09 17:47:34 +01:00
|
|
|
#[derive(Debug, Default)]
|
2024-03-11 20:45:36 +01:00
|
|
|
pub struct AppArgs {
|
|
|
|
|
pub seed: Option<u32>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Application Game
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
pub struct Game {
|
|
|
|
|
pub state: GameState,
|
|
|
|
|
pub dice_roller: DiceRoller,
|
2024-03-09 22:20:11 +01:00
|
|
|
first_move: Option<CheckerMove>,
|
|
|
|
|
player_id: Option<PlayerId>,
|
2024-03-24 18:37:35 +01:00
|
|
|
bot: Bot,
|
2024-02-09 17:47:34 +01:00
|
|
|
}
|
|
|
|
|
|
2024-03-11 20:45:36 +01:00
|
|
|
impl Game {
|
2024-02-09 17:47:34 +01:00
|
|
|
// Constructs a new instance of [`App`].
|
2024-03-11 20:45:36 +01:00
|
|
|
pub fn new(seed: Option<u64>) -> Self {
|
2024-03-09 22:20:11 +01:00
|
|
|
let mut state = GameState::default();
|
|
|
|
|
// local : player
|
|
|
|
|
let player_id: Option<PlayerId> = state.init_player("myself");
|
2024-03-24 18:37:35 +01:00
|
|
|
// bot
|
|
|
|
|
let bot_id: PlayerId = state.init_player("bot").unwrap();
|
|
|
|
|
let bot_color = state.player_color_by_id(&bot_id).unwrap();
|
|
|
|
|
let bot: Bot = Bot::new(bot_color);
|
|
|
|
|
|
|
|
|
|
let mut game = Self {
|
2024-03-11 20:45:36 +01:00
|
|
|
state,
|
|
|
|
|
dice_roller: DiceRoller::new(seed),
|
2024-03-09 22:20:11 +01:00
|
|
|
first_move: None,
|
|
|
|
|
player_id,
|
2024-03-24 18:37:35 +01:00
|
|
|
bot,
|
|
|
|
|
};
|
|
|
|
|
game.consume(&GameEvent::BeginGame {
|
|
|
|
|
goes_first: player_id.unwrap(),
|
|
|
|
|
});
|
|
|
|
|
game
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn consume(&mut self, event: &GameEvent) -> Option<GameEvent> {
|
|
|
|
|
self.state.consume(&event);
|
|
|
|
|
self.bot.consume(&event)
|
2024-02-09 17:47:34 +01:00
|
|
|
}
|
2024-03-11 20:45:36 +01:00
|
|
|
}
|
2024-02-09 17:47:34 +01:00
|
|
|
|
2024-03-11 20:45:36 +01:00
|
|
|
// Application.
|
|
|
|
|
#[derive(Debug, Default)]
|
|
|
|
|
pub struct App {
|
|
|
|
|
// should the application exit?
|
|
|
|
|
pub should_quit: bool,
|
|
|
|
|
pub game: Game,
|
|
|
|
|
}
|
2024-03-09 22:20:11 +01:00
|
|
|
|
2024-03-11 20:45:36 +01:00
|
|
|
impl App {
|
2024-02-17 12:55:36 +01:00
|
|
|
// Constructs a new instance of [`App`].
|
2024-03-11 20:45:36 +01:00
|
|
|
pub fn new(args: AppArgs) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
game: Game::new(args.seed.map(|s| s as u64)),
|
|
|
|
|
should_quit: false,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_my_player(&mut self) {}
|
|
|
|
|
|
2024-02-17 12:55:36 +01:00
|
|
|
pub fn start(&mut self) {
|
2024-03-11 20:45:36 +01:00
|
|
|
self.game.state = GameState::new();
|
2024-02-17 12:55:36 +01:00
|
|
|
}
|
|
|
|
|
|
2024-02-09 17:47:34 +01:00
|
|
|
pub fn input(&mut self, input: &str) {
|
|
|
|
|
println!("'{}'", input);
|
2024-03-09 22:20:11 +01:00
|
|
|
match input {
|
|
|
|
|
"quit" => self.quit(),
|
|
|
|
|
"roll" => self.roll_dice(),
|
|
|
|
|
_ => self.add_move(input),
|
2024-02-09 17:47:34 +01:00
|
|
|
}
|
2024-03-09 22:20:11 +01:00
|
|
|
println!("{}", self.display());
|
2024-02-09 17:47:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set running to false to quit the application.
|
|
|
|
|
pub fn quit(&mut self) {
|
|
|
|
|
self.should_quit = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-10 11:49:23 +01:00
|
|
|
fn roll_dice(&mut self) {
|
2024-03-11 20:45:36 +01:00
|
|
|
if self.game.player_id.is_none() {
|
2024-03-10 11:49:23 +01:00
|
|
|
println!("player_id not set ");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-03-11 20:45:36 +01:00
|
|
|
let dice = self.game.dice_roller.roll();
|
2024-03-24 18:37:35 +01:00
|
|
|
self.game.consume(&GameEvent::RollResult {
|
2024-03-11 20:45:36 +01:00
|
|
|
player_id: self.game.player_id.unwrap(),
|
|
|
|
|
dice,
|
2024-03-10 11:49:23 +01:00
|
|
|
});
|
|
|
|
|
}
|
2024-03-09 22:20:11 +01:00
|
|
|
|
|
|
|
|
fn add_move(&mut self, input: &str) {
|
2024-03-11 20:45:36 +01:00
|
|
|
if self.game.player_id.is_none() {
|
2024-03-09 22:20:11 +01:00
|
|
|
println!("player_id not set ");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
let positions: Vec<usize> = input
|
|
|
|
|
.split(' ')
|
|
|
|
|
.map(|str| str.parse().unwrap_or(0))
|
|
|
|
|
.collect();
|
|
|
|
|
if positions.len() == 2 && positions[0] != 0 && positions[1] != 0 {
|
|
|
|
|
let checker_move = CheckerMove::new(positions[0], positions[1]);
|
|
|
|
|
if checker_move.is_ok() {
|
2024-03-11 20:45:36 +01:00
|
|
|
if self.game.first_move.is_some() {
|
2024-03-10 11:49:23 +01:00
|
|
|
let move_event = GameEvent::Move {
|
2024-03-11 20:45:36 +01:00
|
|
|
player_id: self.game.player_id.unwrap(),
|
|
|
|
|
moves: (self.game.first_move.unwrap(), checker_move.unwrap()),
|
2024-03-10 11:49:23 +01:00
|
|
|
};
|
2024-03-11 20:45:36 +01:00
|
|
|
if !self.game.state.validate(&move_event) {
|
2024-03-10 11:49:23 +01:00
|
|
|
println!("Move invalid");
|
2024-03-11 20:45:36 +01:00
|
|
|
self.game.first_move = None;
|
2024-03-10 11:49:23 +01:00
|
|
|
return;
|
|
|
|
|
}
|
2024-03-24 18:37:35 +01:00
|
|
|
self.game.consume(&move_event);
|
2024-03-11 20:45:36 +01:00
|
|
|
self.game.first_move = None;
|
2024-03-09 22:20:11 +01:00
|
|
|
} else {
|
2024-03-11 20:45:36 +01:00
|
|
|
self.game.first_move = Some(checker_move.unwrap());
|
2024-03-09 22:20:11 +01:00
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
println!("invalid move : {}", input);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-17 12:55:36 +01:00
|
|
|
pub fn display(&mut self) -> String {
|
2024-03-10 11:49:23 +01:00
|
|
|
let mut output = "-------------------------------".to_owned();
|
2024-03-11 20:45:36 +01:00
|
|
|
output = output + "\nRolled dice : " + &self.game.state.dice.to_display_string();
|
2024-03-10 11:49:23 +01:00
|
|
|
output = output + "\n-------------------------------";
|
2024-03-11 20:45:36 +01:00
|
|
|
output = output + "\n" + &self.game.state.board.to_display_grid(9);
|
2024-03-10 11:49:23 +01:00
|
|
|
output
|
2024-02-09 17:47:34 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
2024-02-17 12:55:36 +01:00
|
|
|
fn test_display() {
|
2024-03-10 11:49:23 +01:00
|
|
|
let expected = "-------------------------------
|
|
|
|
|
Rolled dice : 0 & 0
|
|
|
|
|
-------------------------------
|
|
|
|
|
|
2024-02-18 18:40:45 +01:00
|
|
|
13 14 15 16 17 18 19 20 21 22 23 24
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | 15 |
|
|
|
|
|
|----------------------------- | | ------------------------------|
|
|
|
|
|
| | | 15 |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
12 11 10 9 8 7 6 5 4 3 2 1
|
|
|
|
|
";
|
2024-02-09 17:47:34 +01:00
|
|
|
let mut app = App::default();
|
2024-02-18 18:40:45 +01:00
|
|
|
self::assert_eq!(app.display(), expected);
|
2024-02-09 17:47:34 +01:00
|
|
|
}
|
2024-03-09 22:20:11 +01:00
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_move() {
|
2024-03-10 11:49:23 +01:00
|
|
|
let expected = "-------------------------------
|
2024-03-11 20:45:36 +01:00
|
|
|
Rolled dice : 2 & 3
|
2024-03-10 11:49:23 +01:00
|
|
|
-------------------------------
|
|
|
|
|
|
2024-03-09 22:20:11 +01:00
|
|
|
13 14 15 16 17 18 19 20 21 22 23 24
|
|
|
|
|
----------------------------------------------------------------
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | X |
|
|
|
|
|
| | | 15 |
|
|
|
|
|
|----------------------------- | | ------------------------------|
|
|
|
|
|
| | | 13 |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
|
|
|
|
| | | O |
|
2024-03-11 20:45:36 +01:00
|
|
|
| | | O O O |
|
2024-03-09 22:20:11 +01:00
|
|
|
----------------------------------------------------------------
|
|
|
|
|
12 11 10 9 8 7 6 5 4 3 2 1
|
|
|
|
|
";
|
2024-03-11 20:45:36 +01:00
|
|
|
let mut app = App::new(AppArgs { seed: Some(1327) });
|
|
|
|
|
app.input("roll");
|
|
|
|
|
app.input("1 3");
|
2024-03-09 22:20:11 +01:00
|
|
|
app.input("1 4");
|
|
|
|
|
self::assert_eq!(app.display(), expected);
|
|
|
|
|
}
|
2024-02-09 17:47:34 +01:00
|
|
|
}
|