Anonymous user
Playing cards: Difference between revisions
→{{header|Rust}}: fixed broken Code, not exactly elegant, but working
(→version 2: changed whitespace and comments, uses simpler variable names, optimized the dealing & shuffling.) |
(→{{header|Rust}}: fixed broken Code, not exactly elegant, but working) |
||
Line 3,617:
=={{header|Rust}}==
<lang rust>use std::fmt;
use
use Pip::*;
use Suit::*;
#[
enum Pip {
Ace,
Line 3,636 ⟶ 3,638:
}
#[derive(Copy, Clone, Debug)]
Ace => "Ace",▼
};▼
write!(f, "{}", name)▼
}▼
enum Suit {
Spades,
Line 3,666 ⟶ 3,646:
}
#[derive(Copy, Clone, Debug)]
struct Card {
pip: Pip,
Line 3,691 ⟶ 3,658:
}
impl fmt::
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{:?} of {:?}", self.pip, self.suit)
}
}
#[
struct Deck(Vec<Card>);
Line 3,703 ⟶ 3,670:
fn new() -> Deck {
let mut cards:Vec<Card> = Vec::with_capacity(52);
for &suit in &[Spades, Hearts, Diamonds, Clubs]
for &pip in &[Ace, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King]
cards.push(Card::new(
}
}
Line 3,712 ⟶ 3,679:
fn deal(&mut self) -> Option<Card> {
}
fn shuffle(&mut self) {
let mut rng = task_rng();▼
}
}
impl fmt::
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for card in cards.iter() {▼
}
▲}
fn main() {
deck.shuffle();
println!("{}", deck.deal().unwrap());
}
}</lang>
'''Sample output: 5 random cards'''
<pre>Jack of Diamonds
Nine of Hearts
Queen of Hearts
Six of Clubs
Five of Clubs</pre>
=={{header|Scala}}==
|