wip cli test roll

This commit is contained in:
Henri Bourcereau 2024-03-10 11:49:23 +01:00
parent be0264f9a7
commit c52d53737e
2 changed files with 34 additions and 9 deletions

View file

@ -51,8 +51,15 @@ impl App {
self.should_quit = true; self.should_quit = true;
} }
// Set running to false to quit the application. fn roll_dice(&mut self) {
fn roll_dice(&mut self) {} if self.player_id.is_none() {
println!("player_id not set ");
return;
}
self.game.consume(&GameEvent::Roll {
player_id: self.player_id.unwrap(),
});
}
fn add_move(&mut self, input: &str) { fn add_move(&mut self, input: &str) {
if self.player_id.is_none() { if self.player_id.is_none() {
@ -67,10 +74,16 @@ impl App {
let checker_move = CheckerMove::new(positions[0], positions[1]); let checker_move = CheckerMove::new(positions[0], positions[1]);
if checker_move.is_ok() { if checker_move.is_ok() {
if self.first_move.is_some() { if self.first_move.is_some() {
self.game.consume(&GameEvent::Move { let move_event = GameEvent::Move {
player_id: self.player_id.unwrap(), player_id: self.player_id.unwrap(),
moves: (self.first_move.unwrap(), checker_move.unwrap()), moves: (self.first_move.unwrap(), checker_move.unwrap()),
}); };
if !self.game.validate(&move_event) {
println!("Move invalid");
self.first_move = None;
return;
}
self.game.consume(&move_event);
self.first_move = None; self.first_move = None;
} else { } else {
self.first_move = Some(checker_move.unwrap()); self.first_move = Some(checker_move.unwrap());
@ -82,9 +95,11 @@ impl App {
} }
pub fn display(&mut self) -> String { pub fn display(&mut self) -> String {
let mut board = "".to_owned(); let mut output = "-------------------------------".to_owned();
board = board + &self.game.board.to_display_grid(9); output = output + "\nRolled dice : " + &self.game.dices.to_display_string();
board output = output + "\n-------------------------------";
output = output + "\n" + &self.game.board.to_display_grid(9);
output
} }
} }
@ -94,7 +109,10 @@ mod tests {
#[test] #[test]
fn test_display() { fn test_display() {
let expected = " let expected = "-------------------------------
Rolled dice : 0 & 0
-------------------------------
13 14 15 16 17 18 19 20 21 22 23 24 13 14 15 16 17 18 19 20 21 22 23 24
---------------------------------------------------------------- ----------------------------------------------------------------
| | | X | | | | X |
@ -125,7 +143,10 @@ mod tests {
#[test] #[test]
fn test_move() { fn test_move() {
let expected = " let expected = "-------------------------------
Rolled dice : 0 & 0
-------------------------------
13 14 15 16 17 18 19 20 21 22 23 24 13 14 15 16 17 18 19 20 21 22 23 24
---------------------------------------------------------------- ----------------------------------------------------------------
| | | X | | | | X |

View file

@ -33,6 +33,10 @@ impl Dices {
format!("{:0>3b}{:0>3b}", self.values.0, self.values.1) format!("{:0>3b}{:0>3b}", self.values.0, self.values.1)
} }
pub fn to_display_string(self) -> String {
format!("{} & {}", self.values.0, self.values.1)
}
// pub fn to_bits(self) -> [bool;6] { // pub fn to_bits(self) -> [bool;6] {
// self.to_bits_string().into_bytes().iter().map(|strbit| *strbit == '1' as u8).collect() // self.to_bits_string().into_bytes().iter().map(|strbit| *strbit == '1' as u8).collect()
// } // }