mirror of
https://github.com/Vale54321/schafkop-neu.git
synced 2025-12-13 10:39:33 +01:00
init game logic
This commit is contained in:
2
schafkopf-logic/.gitignore
vendored
Normal file
2
schafkopf-logic/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
target
|
||||||
|
Cargo.lock
|
||||||
9
schafkopf-logic/Cargo.toml
Normal file
9
schafkopf-logic/Cargo.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
[package]
|
||||||
|
name = "schafkopf-logic"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
strum = "0.27"
|
||||||
|
strum_macros = "0.27"
|
||||||
|
rand = "0.9"
|
||||||
15
schafkopf-logic/src/deck/card.rs
Normal file
15
schafkopf-logic/src/deck/card.rs
Normal 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.rank, self.suit)
|
||||||
|
}
|
||||||
|
}
|
||||||
50
schafkopf-logic/src/deck/mod.rs
Normal file
50
schafkopf-logic/src/deck/mod.rs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
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 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
schafkopf-logic/src/deck/rank.rs
Normal file
43
schafkopf-logic/src/deck/rank.rs
Normal 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
schafkopf-logic/src/deck/suit.rs
Normal file
22
schafkopf-logic/src/deck/suit.rs
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
29
schafkopf-logic/src/main.rs
Normal file
29
schafkopf-logic/src/main.rs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
mod deck;
|
||||||
|
use deck::Deck;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut deck = Deck::new();
|
||||||
|
deck.shuffle();
|
||||||
|
|
||||||
|
let [mut hand1, mut hand2, mut hand3, mut hand4] = deck.deal_4x8().unwrap();
|
||||||
|
|
||||||
|
println!("Player 1 has:");
|
||||||
|
for card in hand1.iter() {
|
||||||
|
println!("{}", card)
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("\nPlayer 2 has:");
|
||||||
|
for card in hand2.iter() {
|
||||||
|
println!("{}", card)
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("\nPlayer 3 has:");
|
||||||
|
for card in hand3.iter() {
|
||||||
|
println!("{}", card)
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("\nPlayer 4 has:");
|
||||||
|
for card in hand4.iter() {
|
||||||
|
println!("{}", card)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user