Skip to content

Commit

Permalink
complete lab
Browse files Browse the repository at this point in the history
  • Loading branch information
zk-tarts committed Nov 26, 2022
1 parent 7b4458e commit 51c7fbc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
11 changes: 10 additions & 1 deletion src/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ pub struct Card {

impl Card {
pub fn fight(&self, other: &Card) -> FightResult {
todo!()
use std::cmp::Ordering::*;
match (
self.damage.cmp(&other.health), // compare this cards damage to other cards hp
other.damage.cmp(&self.health), // compare other cards damge to this cards hp
) {
(Less, Less) => FightResult::Draw, // both damages are less than health
(Greater | Equal, Less) => FightResult::Win, // only this cards damage is enough to win
(Less, Greater | Equal) => FightResult::Loss, // only other cards damage is enough to win
_ => FightResult::Tie, // cards both have enough damage to win
}
}

/// Give a play by play of the battle
Expand Down
5 changes: 4 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ mod shop;
mod strings;

pub enum FightResult {
// TODO: Add variants for win, loss, tie, and draw
Win,
Loss,
Tie,
Draw,
}

fn main() {
Expand Down
18 changes: 17 additions & 1 deletion src/shop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,23 @@ impl Shop {
/// this store wins, FightResult::Loss if this store loses, and a
/// FightResult::Tie if both stores win the same number of battles.
pub fn fight_store(&self, other: &Shop) -> FightResult {
todo!()
let mut self_wins = 0;
let mut other_wins = 0;
for a in self.cards.iter() {
for b in other.cards.iter() {
match a.fight(b) {
FightResult::Win => self_wins += 1,
FightResult::Loss => other_wins += 1,
_ => (),
}
}
}
use std::cmp::Ordering::*;
match self_wins.cmp(&other_wins) {
Less => FightResult::Loss,
Equal => FightResult::Tie,
Greater => FightResult::Win,
}
}
}

Expand Down

0 comments on commit 51c7fbc

Please sign in to comment.