From d31f0a517643c4e99cae52ad9ee58276f8975cf9 Mon Sep 17 00:00:00 2001 From: Augusto Date: Sat, 26 Dec 2020 23:32:41 +0000 Subject: [PATCH] Add subject and test of the exercise `card_deck` --- rust/tests/card_deck_test/Cargo.lock | 96 +++++++++++++++++++++++++++ rust/tests/card_deck_test/Cargo.toml | 10 +++ rust/tests/card_deck_test/src/main.rs | 40 +++++++++++ subjects/card_deck/README.md | 85 ++++++++++++++++++++++++ 4 files changed, 231 insertions(+) create mode 100644 rust/tests/card_deck_test/Cargo.lock create mode 100644 rust/tests/card_deck_test/Cargo.toml create mode 100644 rust/tests/card_deck_test/src/main.rs create mode 100644 subjects/card_deck/README.md diff --git a/rust/tests/card_deck_test/Cargo.lock b/rust/tests/card_deck_test/Cargo.lock new file mode 100644 index 00000000..8c4fa4f8 --- /dev/null +++ b/rust/tests/card_deck_test/Cargo.lock @@ -0,0 +1,96 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "card_deck" +version = "0.1.0" +dependencies = [ + "rand 0.3.23", +] + +[[package]] +name = "card_deck_test" +version = "0.1.0" +dependencies = [ + "card_deck", +] + +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + +[[package]] +name = "libc" +version = "0.2.71" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49" + +[[package]] +name = "rand" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" +dependencies = [ + "libc", + "rand 0.4.6", +] + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi", +] + +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", +] + +[[package]] +name = "winapi" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/rust/tests/card_deck_test/Cargo.toml b/rust/tests/card_deck_test/Cargo.toml new file mode 100644 index 00000000..88542de7 --- /dev/null +++ b/rust/tests/card_deck_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "card_deck_test" +version = "0.1.0" +authors = ["Augusto "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +card_deck = { path = "../../../../rust-piscine-solutions/card_deck"} \ No newline at end of file diff --git a/rust/tests/card_deck_test/src/main.rs b/rust/tests/card_deck_test/src/main.rs new file mode 100644 index 00000000..ccb20162 --- /dev/null +++ b/rust/tests/card_deck_test/src/main.rs @@ -0,0 +1,40 @@ +// Create a enum that represent the card suits +use card_deck::{self, Card, Rank, Suit}; + +// Write a program that takes that returns a random card in the deck +// A standard deck of cards has 52 cards: 4 suits and 13 cards per suit +#[allow(dead_code)] +fn main() { + let your_card = Card { + rank: Rank::random(), + suit: Suit::random(), + }; + + println!("You're card is {:?}", your_card); + + // Now if the card is an Ace of Spades print "You are the winner" + if card_deck::winner_card(your_card) { + println!("You are the winner!"); + } +} + +#[test] +fn test_winner() { + let winner = Card { + rank: Rank::Ace, + suit: Suit::Spade, + }; + for rank in 1..14 { + for suit in 1..5 { + let card = Card { + rank: Rank::traslate(rank), + suit: Suit::translate(suit), + }; + if card != winner { + assert!(!card_deck::winner_card(card)); + } else { + assert!(card_deck::winner_card(card)); + } + } + } +} diff --git a/subjects/card_deck/README.md b/subjects/card_deck/README.md new file mode 100644 index 00000000..3d3a6c27 --- /dev/null +++ b/subjects/card_deck/README.md @@ -0,0 +1,85 @@ +## card_deck + +### Instructions + +Represent cards from a desk + +- A standard deck of cards has 52 cards: 4 suits and 13 cards per suit + +- Start by creating the `Suit` enum and implement the associated +function `random` which returns a random `Suit` (`Heart`, +`Diamond`, `Spade` or `Club`) + +- Then create the `Rank` enum that can have the value +`Ace`, `King`, `Queen`, `Jack`, and `Number` associated to an `u8` +value to represent the ranks 2 through 10 +After create an associated function to `Rank` called `Random` that +returns a random `Rank` + +- Finally create a structure name `Card` which has the fields `suit` +and `rank` + +Define: + +The associated function `translate` for Rank and Suite +- for `Suit`, `tranlate` makes the translation between an integer value (u8) and the suit of a card (1 -> Heart, 2 -> Diamonds, 3 -> Spade, 4 -> Club) +- for `Rank`, `translate` makes the tranlation between an integer value (u8) and the rank ( 1 -> Ace, 2 -> 2, .., 10 -> 10, 11 -> Jack, 12 -> Queen, 13 -> King) + +The associated function `random` for `Rank` and `Suit` which returns a random rand and suit respectively + +Finally define the function `winner_card` that returns true if the card passed as an argument is an Ace of spades + +### Expected Functions and Structures + +```rust +pub fn random() -> Suit { +} + +pub fn translate(value: u8) -> Suit { +} + +pub fn random() -> Rank { +} + +pub fn traslate(value: u8) -> Rank { +} + +pub enum Suit { +} + +pub enum Rank { +} + +pub struct Card { + pub suit: Suit, + pub rank: Rank, +} +``` + +### Usage + +Here is a program to test your function + +```rust +fn main() { + let your_card = Card { + rank: Rank::random(), + suit: Suit::random(), + }; + + println!("You're card is {:?}", your_card); + + // Now if the card is an Ace of Spades print "You are the winner" + if card_deck::winner_card(your_card) { + println!("You are the winner!"); + } +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +You're card is Card { suit: Club, rank: Ace } +student@ubuntu:~/[[ROOT]]/test$ +```