fix play loop
This commit is contained in:
parent
6ceefe01ab
commit
6a0dc9395a
|
|
@ -46,8 +46,8 @@ impl Bot {
|
|||
|
||||
pub fn consume(&mut self, event: &GameEvent) -> Option<GameEvent> {
|
||||
self.game.consume(event);
|
||||
println!("bot game {:?}", self.game);
|
||||
println!("bot player_id {:?}", self.player_id);
|
||||
// println!("bot game {:?}", self.game);
|
||||
// println!("bot player_id {:?}", self.player_id);
|
||||
if self.game.active_player_id == self.player_id {
|
||||
return match self.game.turn_stage {
|
||||
TurnStage::RollDice => Some(GameEvent::Roll {
|
||||
|
|
|
|||
|
|
@ -42,26 +42,27 @@ impl Game {
|
|||
}
|
||||
|
||||
pub fn consume(&mut self, event: &GameEvent) -> Option<GameEvent> {
|
||||
if self.state.validate(event) {
|
||||
println!("consuming {:?}", event);
|
||||
self.state.consume(event);
|
||||
// chain all successive bot actions
|
||||
let bot_event = self
|
||||
.bot
|
||||
.consume(event)
|
||||
.map(|evt| self.consume(&evt))
|
||||
.flatten();
|
||||
// roll dice for bot if needed
|
||||
if self.bot_needs_dice_roll() {
|
||||
let dice = self.dice_roller.roll();
|
||||
return self.consume(&GameEvent::RollResult {
|
||||
player_id: self.bot.player_id,
|
||||
dice,
|
||||
});
|
||||
}
|
||||
return bot_event;
|
||||
if !self.state.validate(event) {
|
||||
return None;
|
||||
}
|
||||
// println!("consuming {:?}", event);
|
||||
self.state.consume(event);
|
||||
// chain all successive bot actions
|
||||
let bot_event = self
|
||||
.bot
|
||||
.consume(event)
|
||||
.map(|evt| self.consume(&evt))
|
||||
.flatten();
|
||||
// roll dice for bot if needed
|
||||
if self.bot_needs_dice_roll() {
|
||||
let dice = self.dice_roller.roll();
|
||||
self.consume(&GameEvent::RollResult {
|
||||
player_id: self.bot.player_id,
|
||||
dice,
|
||||
})
|
||||
} else {
|
||||
bot_event
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn bot_needs_dice_roll(&self) -> bool {
|
||||
|
|
@ -94,8 +95,10 @@ impl App {
|
|||
}
|
||||
|
||||
pub fn input(&mut self, input: &str) {
|
||||
println!("'{}'", input);
|
||||
// println!("'{}'", input);
|
||||
match input {
|
||||
"state" => self.show_state(),
|
||||
"history" => self.show_history(),
|
||||
"quit" => self.quit(),
|
||||
"roll" => self.roll_dice(),
|
||||
_ => self.add_move(input),
|
||||
|
|
@ -108,6 +111,16 @@ impl App {
|
|||
self.should_quit = true;
|
||||
}
|
||||
|
||||
pub fn show_state(&self) {
|
||||
println!("{:?}", self.game.state)
|
||||
}
|
||||
|
||||
pub fn show_history(&self) {
|
||||
for hist in self.game.state.history.iter() {
|
||||
println!("{:?}\n", hist);
|
||||
}
|
||||
}
|
||||
|
||||
fn roll_dice(&mut self) {
|
||||
if self.game.player_id.is_none() {
|
||||
println!("player_id not set ");
|
||||
|
|
@ -155,6 +168,15 @@ impl App {
|
|||
|
||||
pub fn display(&mut self) -> String {
|
||||
let mut output = "-------------------------------".to_owned();
|
||||
output = output
|
||||
+ "\nWaiting for player "
|
||||
+ &self
|
||||
.game
|
||||
.state
|
||||
.who_plays()
|
||||
.map(|pl| &pl.name)
|
||||
.unwrap_or(&"?".to_owned());
|
||||
|
||||
output = output + "\nRolled dice : " + &self.game.state.dice.to_display_string();
|
||||
output = output + "\n-------------------------------";
|
||||
output = output + "\n" + &self.game.state.board.to_display_grid(9);
|
||||
|
|
@ -169,6 +191,7 @@ mod tests {
|
|||
#[test]
|
||||
fn test_display() {
|
||||
let expected = "-------------------------------
|
||||
Waiting for player ?
|
||||
Rolled dice : 0 & 0
|
||||
-------------------------------
|
||||
|
||||
|
|
@ -203,6 +226,7 @@ Rolled dice : 0 & 0
|
|||
#[test]
|
||||
fn test_move() {
|
||||
let expected = "-------------------------------
|
||||
Waiting for player myself
|
||||
Rolled dice : 4 & 6
|
||||
-------------------------------
|
||||
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ fn transpose(matrix: Vec<Vec<String>>) -> Vec<Vec<String>> {
|
|||
|
||||
impl CheckerMove {
|
||||
pub fn new(from: Field, to: Field) -> Result<Self, Error> {
|
||||
println!("from {} to {}", from, to);
|
||||
// println!("from {} to {}", from, to);
|
||||
// check if the field is on the board
|
||||
// we allow 0 for 'to', which represents the exit of a checker
|
||||
if from < 1 || 24 < from || 24 < to {
|
||||
|
|
|
|||
|
|
@ -292,7 +292,7 @@ impl GameState {
|
|||
let (move1, move2): &(CheckerMove, CheckerMove) = moves.into();
|
||||
let dist1 = (move1.get_to() as i8 - move1.get_from() as i8).abs() as u8;
|
||||
let dist2 = (move2.get_to() as i8 - move2.get_from() as i8).abs() as u8;
|
||||
print!("{}, {}, {}, {}", dist1, dist2, dice1, dice2);
|
||||
// print!("{}, {}, {}, {}", dist1, dist2, dice1, dice2);
|
||||
// basic : same number
|
||||
if cmp::min(dist1, dist2) != cmp::min(dice1, dice2)
|
||||
|| cmp::max(dist1, dist2) != cmp::max(dice1, dice2)
|
||||
|
|
|
|||
Loading…
Reference in a new issue