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

Feedback #1

Open
wants to merge 7 commits into
base: feedback
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion src/card.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ pub struct Card {

impl Card {
pub fn fight(&self, other: &Card) -> FightResult {
todo!()
let this_fight: (bool, bool) = (self.damage >= other.health, other.damage >= self.health);

match this_fight {
(true, false) => FightResult::Win,
(false, true) => FightResult::Loss,
(true, true) => FightResult::Tie,
(false, false) => FightResult::Draw,
}
}

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

pub enum FightResult {
// TODO: Add variants for win, loss, tie, and draw
// 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 tally = 0;
for card in self.cards.iter() {
for other_card in other.cards.iter() {
let result = card.print_fight(other_card);
match result {
FightResult::Win => tally += 1,
FightResult::Loss => tally -= 1,
_ => (),
}
}
}
Comment on lines +29 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit funky, but here's another way to solve this that just uses iterators:

let tally = self
    .cards
    .iter()
    .flat_map(|card| {
        other.cards.iter().map(move |other_card| {
            let result = card.print_fight(other_card);
            match result {
                FightResult::Win => 1,
                FightResult::Loss => -1,
                _ => 0,
            }
        })
    })
    .sum();

Where we iterate over the first store's cards, then over the second, and collect +1 or -1 for each battle, then add them up at the end. We use flat_map so that even though we get something that might look like [1,-1,-1], [0, 1, -1]... for each of the cards in the first store, it "flattens" it to just [1, -1, -1, 0, 1, -1], then we can just sum() that together :)

Though I don't think I got far enough in the fall to talk about iterator methods, so this is mostly just extra info /shrug

match tally {
0 => FightResult::Tie,
d if d > 0 => FightResult::Win,
d if d < 0 => FightResult::Loss,
_ => unreachable!(),
}
}
}

Expand Down