Skip to content
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
15 changes: 9 additions & 6 deletions solutions/card_deck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ pub enum Rank {
}

impl Rank {
#[inline]
pub fn random() -> Self {
let value = rand::thread_rng().gen_range(1..14);
Self::translate(value)
Self::translate(rand::thread_rng().gen_range(1..14))
}

#[inline]
pub fn translate(value: u8) -> Self {
match value {
1 => Self::Ace,
Expand All @@ -36,11 +37,12 @@ impl Rank {
}

impl Suit {
#[inline]
pub fn random() -> Self {
let value = rand::thread_rng().gen_range(1..5);
Self::translate(value)
Self::translate(rand::thread_rng().gen_range(1..5))
}

#[inline]
pub fn translate(value: u8) -> Self {
match value {
1 => Self::Heart,
Expand All @@ -58,8 +60,9 @@ pub struct Card {
pub rank: Rank,
}

pub fn winner_card(card: Card) -> bool {
card == Card {
#[inline]
pub fn winner_card(card: &Card) -> bool {
card == &Card {
suit: Suit::Spade,
rank: Rank::Ace,
}
Expand Down
8 changes: 4 additions & 4 deletions tests/card_deck_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ fn main() {
suit: Suit::random(),
};

println!("Your card is {:?}", your_card);
println!("Your card is {:?}", &your_card);

if card_deck::winner_card(your_card) {
if card_deck::winner_card(&your_card) {
println!("You are the winner!");
}
}
Expand All @@ -17,7 +17,7 @@ fn main() {
mod tests {
use super::*;

// We cannot test the randomness as there's no 100% accurate consistent way to prove through a predicate that it yields a truly random number
// We cannot truly test the randomness as there's no 100% accurate consistent way to prove through a predicate that it yields a truly random number

#[test]
fn test_winner() {
Expand All @@ -33,7 +33,7 @@ mod tests {
suit: Suit::translate(suit),
};

assert_eq!(card_deck::winner_card(card), card == winner);
assert_eq!(card_deck::winner_card(&card), card == winner);
}
}
}
Expand Down
Loading