From 5f3a8e5a630903c4b7b1f74e1001778aa8100aa9 Mon Sep 17 00:00:00 2001 From: "github-classroom[bot]" <66690702+github-classroom[bot]@users.noreply.github.com> Date: Fri, 25 Nov 2022 21:49:41 +0000 Subject: [PATCH 1/4] Setting up GitHub Classroom Feedback From a383542f9e8488cbcb1610e7dfb8629995ccc112 Mon Sep 17 00:00:00 2001 From: asher Date: Sat, 26 Nov 2022 03:31:11 -0500 Subject: [PATCH 2/4] finished --- src/card.rs | 7 ++++++- src/main.rs | 4 ++++ src/shop.rs | 34 ++++++++++++++++++++++++++++++---- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/src/card.rs b/src/card.rs index d152509..06d27e4 100644 --- a/src/card.rs +++ b/src/card.rs @@ -11,7 +11,12 @@ pub struct Card { impl Card { pub fn fight(&self, other: &Card) -> FightResult { - todo!() + match (self.damage >= other.health, other.damage>= self.health) { + (true, true) => FightResult::Tie, + (true,false) => FightResult::Win, + (false,true) => FightResult::Loss, + (false,false) => FightResult::Draw, + } } /// Give a play by play of the battle diff --git a/src/main.rs b/src/main.rs index c667fbe..04af5e2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,10 @@ mod strings; pub enum FightResult { // TODO: Add variants for win, loss, tie, and draw + Win, + Loss, + Tie, + Draw, } fn main() { diff --git a/src/shop.rs b/src/shop.rs index 0177cea..bccae3d 100644 --- a/src/shop.rs +++ b/src/shop.rs @@ -9,24 +9,50 @@ pub struct Shop { impl Shop { /// Get the price of the most expensive card in the shop pub fn most_expensive(&self) -> u32 { - self.cards.iter().map(|card| card.price).max().unwrap() + self.cards + .iter() + .map(|card| card.price) + .max() + .unwrap() } /// Get the total damage of all cards in the shop pub fn total_damage(&self) -> u32 { - self.cards.iter().map(|card| card.damage).sum() + self.cards + .iter() + .map(|card| card.damage) + .sum() } /// Get the total health of all cards in the shop pub fn total_health(&self) -> u32 { - self.cards.iter().map(|card| card.health).sum() + self.cards + .iter() + .map(|card| card.health) + .sum() } /// Simulate a fight against another store. Returns a FightResult::Win if /// 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 zipped: i32 = + self.cards + .iter() + .zip(&other.cards) + .map(|(x,y)| x.fight(y)) + .map(|x| match x { + FightResult::Win => 1, + FightResult::Loss => -1, + _ => 0, + } ) + .sum(); + match zipped { + zipped if zipped>0 => FightResult::Win, + zipped if zipped<0 => FightResult::Loss, + _ => FightResult::Tie, + } + } } From db9119c7653fafbe753bdcde61674b66e1fbba7e Mon Sep 17 00:00:00 2001 From: asher Date: Sat, 26 Nov 2022 03:35:55 -0500 Subject: [PATCH 3/4] fmt and clippy used --- src/card.rs | 8 ++++---- src/shop.rs | 43 ++++++++++++++++--------------------------- 2 files changed, 20 insertions(+), 31 deletions(-) diff --git a/src/card.rs b/src/card.rs index 06d27e4..bb52f1d 100644 --- a/src/card.rs +++ b/src/card.rs @@ -11,11 +11,11 @@ pub struct Card { impl Card { pub fn fight(&self, other: &Card) -> FightResult { - match (self.damage >= other.health, other.damage>= self.health) { + match (self.damage >= other.health, other.damage >= self.health) { (true, true) => FightResult::Tie, - (true,false) => FightResult::Win, - (false,true) => FightResult::Loss, - (false,false) => FightResult::Draw, + (true, false) => FightResult::Win, + (false, true) => FightResult::Loss, + (false, false) => FightResult::Draw, } } diff --git a/src/shop.rs b/src/shop.rs index bccae3d..284f3e2 100644 --- a/src/shop.rs +++ b/src/shop.rs @@ -9,50 +9,39 @@ pub struct Shop { impl Shop { /// Get the price of the most expensive card in the shop pub fn most_expensive(&self) -> u32 { - self.cards - .iter() - .map(|card| card.price) - .max() - .unwrap() + self.cards.iter().map(|card| card.price).max().unwrap() } /// Get the total damage of all cards in the shop pub fn total_damage(&self) -> u32 { - self.cards - .iter() - .map(|card| card.damage) - .sum() + self.cards.iter().map(|card| card.damage).sum() } /// Get the total health of all cards in the shop pub fn total_health(&self) -> u32 { - self.cards - .iter() - .map(|card| card.health) - .sum() + self.cards.iter().map(|card| card.health).sum() } /// Simulate a fight against another store. Returns a FightResult::Win if /// 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 { - let zipped: i32 = - self.cards - .iter() - .zip(&other.cards) - .map(|(x,y)| x.fight(y)) - .map(|x| match x { - FightResult::Win => 1, - FightResult::Loss => -1, - _ => 0, - } ) - .sum(); + let zipped: i32 = self + .cards + .iter() + .zip(&other.cards) + .map(|(x, y)| x.fight(y)) + .map(|x| match x { + FightResult::Win => 1, + FightResult::Loss => -1, + _ => 0, + }) + .sum(); match zipped { - zipped if zipped>0 => FightResult::Win, - zipped if zipped<0 => FightResult::Loss, + zipped if zipped > 0 => FightResult::Win, + zipped if zipped < 0 => FightResult::Loss, _ => FightResult::Tie, } - } } From cc8add04267d43f440aaa4e12e56cd920bcb341a Mon Sep 17 00:00:00 2001 From: asher Date: Sat, 26 Nov 2022 04:11:32 -0500 Subject: [PATCH 4/4] easier to read match statement (s/o ~Nebula~#0703) --- src/shop.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/shop.rs b/src/shop.rs index 284f3e2..6fb584a 100644 --- a/src/shop.rs +++ b/src/shop.rs @@ -38,13 +38,12 @@ impl Shop { }) .sum(); match zipped { - zipped if zipped > 0 => FightResult::Win, - zipped if zipped < 0 => FightResult::Loss, - _ => FightResult::Tie, + 1.. => FightResult::Win, + 0 => FightResult::Tie, + _ => FightResult::Loss, } } } - // Implement the Display trait for Shop so that it can be printed. Print the // shop's stats, including the most expensive card, the total damage, and the // total health.