2020-12-26 23:32:41 +00:00
## card_deck
### Instructions
2022-05-24 14:24:48 +00:00
A standard deck of cards has 52 cards: 4 suits with 13 cards per suit.
2021-02-10 02:04:54 +00:00
Represent the cards from a deck:
2020-12-26 23:32:41 +00:00
2022-05-24 14:24:48 +00:00
- Create an `enum` to represent the `Suit` .
- Implement the associated **function** `random` , which returns a random `Suit` (`Heart`, `Diamond` , `Spade` or `Club` ).
2022-05-24 15:58:08 +00:00
- Create a `Rank` enum. For ace and face cards, it can be one of `Ace` , `King` , `Queen` or `Jack` . For the values from 2 to 10, it can have a `Number` value associated to a `u8` .
2022-05-24 14:24:48 +00:00
- Create an associated **function** to `Rank` called `Random` that returns a random `Rank` .
- Create a structure name `Card` which has the fields `suit` and `rank` .
2020-12-26 23:32:41 +00:00
Define:
2021-02-11 00:02:54 +00:00
- The associated **function** `translate` for `Rank` and `Suit` :
2022-05-24 14:24:48 +00:00
- For `Suit` , `translate` converts an integer value (`u8`) to a suit (1 -> Heart, 2 -> Diamonds, 3 -> Spade, 4 -> Club).
- For `Rank` , `translate` converts an integer value (`u8`) to a rank ( 1 -> Ace, 2 -> 2, .., 10 -> 10, 11 -> Jack, 12 -> Queen, 13 -> King).
- The associated **function** `random` for `Rank` and `Suit` returns a random `Rank` and `Suit` respectively.
- Finally define the **function** `winner_card` which returns `true` if the card passed as an argument is an ace of spades.
2020-12-26 23:32:41 +00:00
2021-10-10 22:19:21 +00:00
### Dependencies
2023-08-31 10:07:53 +00:00
[rand = "0.3.14" ](https://docs.rs/crate/rand/0.3.14 )
2021-10-10 22:19:21 +00:00
2020-12-26 23:32:41 +00:00
### Expected Functions and Structures
```rust
2021-02-11 00:02:54 +00:00
pub enum Suit {
2020-12-26 23:32:41 +00:00
}
2021-02-11 00:02:54 +00:00
pub enum Rank {
2020-12-26 23:32:41 +00:00
}
2021-02-11 00:02:54 +00:00
impl Suit {
pub fn random() -> Suit {
}
2020-12-26 23:32:41 +00:00
2021-02-11 00:02:54 +00:00
pub fn translate(value: u8) -> Suit {
}
2020-12-26 23:32:41 +00:00
}
2021-02-11 00:02:54 +00:00
impl Rank {
pub fn random() -> Rank {
}
2020-12-26 23:32:41 +00:00
2021-02-11 00:02:54 +00:00
pub fn translate(value: u8) -> Rank {
}
2020-12-26 23:32:41 +00:00
}
pub struct Card {
pub suit: Suit,
pub rank: Rank,
}
2022-03-04 16:29:42 +00:00
2022-10-06 11:11:52 +00:00
pub fn winner_card(card: & Card) -> bool {
2022-03-04 16:29:42 +00:00
}
2020-12-26 23:32:41 +00:00
```
### Usage
Here is a program to test your function
```rust
2022-11-22 21:04:04 +00:00
use card_deck::*;
2020-12-26 23:32:41 +00:00
fn main() {
let your_card = Card {
rank: Rank::random(),
suit: Suit::random(),
};
2021-02-10 02:04:54 +00:00
println!("Your card is {:?}", your_card);
2020-12-26 23:32:41 +00:00
// Now if the card is an Ace of Spades print "You are the winner"
2022-10-06 11:11:52 +00:00
if card_deck::winner_card(& your_card) {
2020-12-26 23:32:41 +00:00
println!("You are the winner!");
}
}
```
And its output
```console
2021-04-28 09:47:34 +00:00
$ cargo run
2021-02-10 02:04:54 +00:00
Your card is Card { suit: Club, rank: Ace }
2021-04-28 09:47:34 +00:00
$
2020-12-26 23:32:41 +00:00
```
2022-05-24 14:24:48 +00:00
### Notions
- [Crate rand ](https://docs.rs/rand/0.3.14/rand/ )
- [Enums ](https://doc.rust-lang.org/book/ch06-00-enums.html )