initial commit

This commit is contained in:
2025-11-10 18:13:59 +01:00
commit 29b34fa3f0
12 changed files with 863 additions and 0 deletions

15
src/deck/card.rs Normal file
View File

@@ -0,0 +1,15 @@
use std::fmt;
use crate::deck::{Suit, Rank};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Card {
pub suit: Suit,
pub rank: Rank,
}
impl fmt::Display for Card {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} {}", self.suit, self.rank)
}
}

56
src/deck/mod.rs Normal file
View File

@@ -0,0 +1,56 @@
mod rank;
pub use rank::Rank;
mod suit;
pub use suit::Suit;
mod card;
pub use card::Card;
use strum::IntoEnumIterator;
use rand::seq::SliceRandom;
use rand::rng;
pub struct Deck {
cards: Vec<Card>,
}
impl Default for Deck {
fn default() -> Self {
Self::new()
}
}
impl Deck {
pub fn new() -> Self {
let cards = Suit::iter()
.flat_map(|suit| Rank::iter().map(move |rank| Card { suit, rank }))
.collect();
Self { cards }
}
pub fn draw(&mut self) -> Option<Card> {
self.cards.pop()
}
pub fn shuffle(&mut self) {
self.cards.shuffle(&mut rng());
}
pub fn deal_4x8(&mut self) -> Option<[Vec<Card>; 4]> {
if self.cards.len() < 32 { return None; }
let mut hands = [Vec::with_capacity(8), Vec::with_capacity(8),
Vec::with_capacity(8), Vec::with_capacity(8)];
for _ in 0..8 {
for h in 0..4 {
hands[h].push(self.draw()?);
}
}
Some(hands)
}
pub fn iter(&self) -> impl Iterator<Item=&Card> {
self.cards.iter()
}
}

43
src/deck/rank.rs Normal file
View File

@@ -0,0 +1,43 @@
use std::fmt;
use strum_macros::EnumIter;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, EnumIter)]
pub enum Rank {
Ass,
Zehn,
Koenig,
Ober,
Unter,
Neun,
Acht,
Sieben,
}
impl Rank {
pub fn points(&self) -> u8 {
match self {
Rank::Ass => 11,
Rank::Zehn => 10,
Rank::Koenig => 4,
Rank::Ober => 3,
Rank::Unter => 3,
_ => 0
}
}
}
impl fmt::Display for Rank {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self {
Rank::Ass => "Ass",
Rank::Zehn => "Zehn",
Rank::Koenig => "König",
Rank::Ober => "Ober",
Rank::Unter => "Unter",
Rank::Neun => "Neun",
Rank::Acht => "Acht",
Rank::Sieben => "Sieben",
};
write!(f, "{}", name)
}
}

22
src/deck/suit.rs Normal file
View File

@@ -0,0 +1,22 @@
use std::fmt;
use strum_macros::EnumIter;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, EnumIter)]
pub enum Suit {
Eichel,
Gras,
Herz,
Schell,
}
impl fmt::Display for Suit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let name = match self {
Suit::Eichel => "Eichel",
Suit::Gras => "Gras",
Suit::Herz => "Herz",
Suit::Schell => "Schell",
};
write!(f, "{}", name)
}
}