Skip to content

Commit

Permalink
implementation of card evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
Ipagaxi committed May 15, 2023
1 parent d3ef2cd commit 30fab79
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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)]
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
1 change: 1 addition & 0 deletions models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ pub enum MessageToPc {}
pub enum MessageToPyBadge {
NewLevel(AvailableCards)
}

1 change: 1 addition & 0 deletions pc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ sdl2_bundled = ["tetra/sdl2_bundled"]
log = "0.4.17"
m3-macro = { version = "0.1.0", path = "../macro" }
m3-map = { version = "0.1.0", path = "../map" }
m3-models = { version = "0.1.0", path = "../models" }
my-env-logger-style = "0.1.0"
num_enum = "0.6.1"
once_cell = { version = "1.17.1", features = ["parking_lot"] }
Expand Down
74 changes: 74 additions & 0 deletions pc/src/cards.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use m3_models::Card;
use m3_models::Card::*;



pub enum CarAction {
TurnLeft,
TurnRight,
DriveForward,
Nothing
}
pub struct CardChanges<'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 CardChanges<'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(Some(CarAction::Nothing))
}
}
Some(card) => {
match card {
Card::Left => {
self.card_pos += 1;
self.driving = true;
Some(Some(CarAction::TurnLeft))
}
Right => {
self.card_pos += 1;
self.driving = true;
Some(Some(CarAction::TurnRight))
}
Wait(i) => {
if self.wait_counter < *i {
self.wait_counter += 1;
if self.driving {
Some(Some(CarAction::DriveForward))
} else {
Some(Some(CarAction::Nothing))
}
} else {
Some(Some(CarAction::Nothing))
}
}
MotorOn => {
self.driving = true;
Some(Some(CarAction::Nothing))
}
MotorOff => {
self.driving = false;
Some(Some(CarAction::Nothing))
}
}
}
}
}
}

pub fn evaluateCards(cards:&Vec<Card>) -> CardChanges{
CardChanges { card_pos: 0, wait_counter: 0, driving: true, cards: cards }
}
1 change: 1 addition & 0 deletions pc/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use tetra::{

mod tiles;
use tiles::Textures;
mod cards;

const CARGO_PKG_NAME: &str = env!("CARGO_PKG_NAME");
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down

0 comments on commit 30fab79

Please sign in to comment.