Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: implementation of card evaluation #17

Merged
merged 4 commits into from
May 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions models/src/cards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ use strum_macros::{AsRefStr, EnumIter};

#[derive(AsRefStr, Clone, Copy, Debug, EnumIter)]
pub enum Card {
/// Turn Left
Left,
Right
/// Turn Right
Right,
/// Keep doing current action
Wait(u8),
/// Starts driving foward
MotorOn,
/// Stops driving
MotorOff
}

impl Card {
Expand All @@ -22,15 +30,21 @@ impl Card {
#[derive(Clone, Debug, Decode, Default, Encode, PartialEq)]
pub struct AvailableCards {
pub left: u8,
pub right: u8
pub right: u8,
pub wait: u8,
pub motor_on: u8,
pub motor_off: u8
}

impl AvailableCards {
///return how many cards are avaible from the requested variant `card`
pub fn card_count(&self, card: Card) -> u8 {
match card {
Card::Left => self.left,
Card::Right => self.right
Card::Right => self.right,
Card::Wait(_i) => self.wait,
Card::MotorOn => self.motor_on,
Card::MotorOff => self.motor_off
}
}
}
Expand Down
113 changes: 113 additions & 0 deletions pc/src/cards.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use m3_models::Card;

#[derive(Debug, PartialEq)]
pub enum CarAction {
TurnLeft,
TurnRight,
DriveForward
}

#[derive(Clone, Debug)]
pub struct CardStatus<'a> {
/// Position of card in vector
card_pos: usize,
/// Relative y-position to former position
wait_counter: u8,
driving: bool,
cards: &'a Vec<Card>
}

impl<'a> Iterator for CardStatus<'a> {
type Item = Option<CarAction>;

fn next(&mut self) -> Option<Self::Item> {
match self.cards.get(self.card_pos) {
None => {
if self.driving {
Some(Some(CarAction::DriveForward))
} else {
Some(None)
}
},
Some(card) => match card {
Card::Left => {
self.card_pos += 1;
self.driving = true;
Some(Some(CarAction::TurnLeft))
},
Card::Right => {
self.card_pos += 1;
self.driving = true;
Some(Some(CarAction::TurnRight))
},
Card::Wait(i) => {
if self.wait_counter < (*i) - 1 {
self.wait_counter += 1;
if self.driving {
Some(Some(CarAction::DriveForward))
} else {
Some(None)
}
} else {
self.wait_counter = 0;
self.card_pos += 1;
Some(Some(CarAction::DriveForward))
}
},
Card::MotorOn => {
self.card_pos += 1;
self.driving = true;
Some(None)
},
Card::MotorOff => {
self.card_pos += 1;
self.driving = false;
Some(None)
}
}
}
}
}

pub fn evaluate_cards(cards: &Vec<Card>) -> CardStatus {
CardStatus {
card_pos: 0,
wait_counter: 0,
driving: true,
cards
}
}

#[cfg(test)]
mod tests {
use m3_models::Card::{Left, MotorOff, MotorOn, Wait};

use crate::cards::{evaluate_cards, CarAction::*};
#[test]
fn test_card_evaluation() {
let cards = vec![MotorOn, Wait(3), Left, Wait(2), MotorOff];
let card_status = evaluate_cards(&cards).take(6);
let correct_actions = vec![
None,
Some(DriveForward),
Some(DriveForward),
Some(DriveForward),
Some(TurnLeft),
Some(DriveForward),
Some(DriveForward),
None,
];
for (i, card) in card_status.enumerate() {
assert!(
card == *(correct_actions.get(i).unwrap()),
"Action: `{:?}`, Solution: `{:?}`",
card,
*(correct_actions.get(i).unwrap())
);
}
/*assert!(
card_status.clone().eq(correct_actions),
"Evaluation: `{:?}`", card_status
);*/
}
}