fix: 2 bots play
This commit is contained in:
parent
447ec8cc58
commit
a3bcdb8912
|
|
@ -10,6 +10,7 @@ pub trait BotStrategy: std::fmt::Debug {
|
|||
fn calculate_adv_points(&self) -> u8;
|
||||
fn choose_move(&self) -> (CheckerMove, CheckerMove);
|
||||
fn set_player_id(&mut self, player_id: PlayerId);
|
||||
fn set_color(&mut self, color: Color);
|
||||
fn init_players(&mut self) {
|
||||
self.get_mut_game().init_player("p1");
|
||||
self.get_mut_game().init_player("p2");
|
||||
|
|
@ -47,6 +48,7 @@ impl Bot {
|
|||
Color::Black => 2,
|
||||
};
|
||||
strategy.set_player_id(player_id);
|
||||
strategy.set_color(color);
|
||||
Self {
|
||||
player_id,
|
||||
strategy,
|
||||
|
|
@ -58,10 +60,6 @@ impl Bot {
|
|||
pub fn handle_event(&mut self, event: &GameEvent) -> Option<GameEvent> {
|
||||
let game = self.strategy.get_mut_game();
|
||||
game.consume(event);
|
||||
// println!(
|
||||
// "bot player_id {:?} (active player_id {:?})",
|
||||
// self.player_id, game.active_player_id
|
||||
// );
|
||||
if game.active_player_id == self.player_id {
|
||||
return match game.turn_stage {
|
||||
TurnStage::MarkAdvPoints => Some(GameEvent::Mark {
|
||||
|
|
|
|||
|
|
@ -30,6 +30,10 @@ impl BotStrategy for ClientStrategy {
|
|||
self.player_id = player_id;
|
||||
}
|
||||
|
||||
fn set_color(&mut self, color: Color) {
|
||||
self.color = color;
|
||||
}
|
||||
|
||||
fn calculate_points(&self) -> u8 {
|
||||
let dice_roll_count = self
|
||||
.get_game()
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use crate::{BotStrategy, CheckerMove, Color, GameState, PlayerId, PointsRules};
|
||||
use store::MoveRules;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DefaultStrategy {
|
||||
|
|
@ -26,6 +27,10 @@ impl BotStrategy for DefaultStrategy {
|
|||
&mut self.game
|
||||
}
|
||||
|
||||
fn set_color(&mut self, color: Color) {
|
||||
self.color = color;
|
||||
}
|
||||
|
||||
fn set_player_id(&mut self, player_id: PlayerId) {
|
||||
self.player_id = player_id;
|
||||
}
|
||||
|
|
@ -46,19 +51,30 @@ impl BotStrategy for DefaultStrategy {
|
|||
}
|
||||
|
||||
fn choose_move(&self) -> (CheckerMove, CheckerMove) {
|
||||
let (dice1, dice2) = match self.color {
|
||||
Color::White => (self.game.dice.values.0 as i8, self.game.dice.values.1 as i8),
|
||||
Color::Black => (
|
||||
0 - self.game.dice.values.0 as i8,
|
||||
0 - self.game.dice.values.1 as i8,
|
||||
),
|
||||
};
|
||||
let rules = MoveRules::new(&self.color, &self.game.board, self.game.dice);
|
||||
let possible_moves = rules.get_possible_moves_sequences(true);
|
||||
let choosen_move = *possible_moves
|
||||
.first()
|
||||
.unwrap_or(&(CheckerMove::default(), CheckerMove::default()));
|
||||
if self.color == Color::White {
|
||||
choosen_move
|
||||
} else {
|
||||
(choosen_move.0.mirror(), choosen_move.1.mirror())
|
||||
}
|
||||
|
||||
let fields = self.game.board.get_color_fields(self.color);
|
||||
let first_field = fields.first().unwrap();
|
||||
(
|
||||
CheckerMove::new(first_field.0, (first_field.0 as i8 + dice1) as usize).unwrap(),
|
||||
CheckerMove::new(first_field.0, (first_field.0 as i8 + dice2) as usize).unwrap(),
|
||||
)
|
||||
// let (dice1, dice2) = match self.color {
|
||||
// Color::White => (self.game.dice.values.0 as i8, self.game.dice.values.1 as i8),
|
||||
// Color::Black => (
|
||||
// 0 - self.game.dice.values.0 as i8,
|
||||
// 0 - self.game.dice.values.1 as i8,
|
||||
// ),
|
||||
// };
|
||||
//
|
||||
// let fields = self.game.board.get_color_fields(self.color);
|
||||
// let first_field = fields.first().unwrap();
|
||||
// (
|
||||
// CheckerMove::new(first_field.0, (first_field.0 as i8 + dice1) as usize).unwrap(),
|
||||
// CheckerMove::new(first_field.0, (first_field.0 as i8 + dice2) as usize).unwrap(),
|
||||
// )
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -263,7 +263,7 @@ Rolled dice : 0 & 0
|
|||
InGame > myself > RollDice
|
||||
Rolled dice : 4 & 6
|
||||
Rolled dice jans : {}
|
||||
Last move : CheckerMove { from: 24, to: 20 } , CheckerMove { from: 24, to: 18 }
|
||||
Last move : CheckerMove { from: 24, to: 18 } , CheckerMove { from: 24, to: 20 }
|
||||
|
||||
Player :: holes :: points
|
||||
1. myself :: 0 :: 0
|
||||
|
|
|
|||
|
|
@ -20,7 +20,11 @@ impl GameRunner {
|
|||
) -> Self {
|
||||
let mut state = GameState::new(schools_enabled);
|
||||
// local : player
|
||||
let player_id: Option<PlayerId> = state.init_player("myself");
|
||||
let player_id: Option<PlayerId> = if bot_strategies.len() > 1 {
|
||||
None
|
||||
} else {
|
||||
state.init_player("myself")
|
||||
};
|
||||
|
||||
// bots
|
||||
let bots: Vec<Bot> = bot_strategies
|
||||
|
|
@ -55,6 +59,7 @@ impl GameRunner {
|
|||
|
||||
pub fn handle_event(&mut self, event: &GameEvent) -> Option<GameEvent> {
|
||||
if !self.state.validate(event) {
|
||||
println!("event not valid : {:?}", event);
|
||||
return None;
|
||||
}
|
||||
// println!("consuming {:?}", event);
|
||||
|
|
@ -72,9 +77,15 @@ impl GameRunner {
|
|||
.filter_map(|bot| bot.handle_event(event))
|
||||
.collect();
|
||||
|
||||
// if bot_events.len() > 1 {
|
||||
// println!(
|
||||
// "There might be a problem : 2 bots events : {:?}",
|
||||
// bot_events
|
||||
// );
|
||||
// }
|
||||
|
||||
let mut next_event = None;
|
||||
for bot_event in bot_events {
|
||||
println!("bot event {:?}", bot_event);
|
||||
let bot_result_event = self.handle_event(&bot_event);
|
||||
if let Some(bot_id) = bot_event.player_id() {
|
||||
next_event = if self.bot_needs_dice_roll(bot_id) {
|
||||
|
|
@ -85,7 +96,7 @@ impl GameRunner {
|
|||
})
|
||||
} else {
|
||||
bot_result_event
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
next_event
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
mod game;
|
||||
mod game_rules_moves;
|
||||
pub use game_rules_moves::MoveRules;
|
||||
mod game_rules_points;
|
||||
pub use game::{EndGameReason, GameEvent, GameState, Stage, TurnStage};
|
||||
pub use game_rules_points::PointsRules;
|
||||
|
|
|
|||
Loading…
Reference in a new issue