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