card game stuff
This commit is contained in:
Generated
-7
@@ -18,7 +18,6 @@ checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3"
|
||||
name = "card_game"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"deranged",
|
||||
"rand",
|
||||
]
|
||||
|
||||
@@ -48,12 +47,6 @@ dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "deranged"
|
||||
version = "0.5.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7cd812cc2bc1d69d4764bd80df88b4317eaef9e773c75226407d9bc0876b211c"
|
||||
|
||||
[[package]]
|
||||
name = "equivalent"
|
||||
version = "1.0.2"
|
||||
|
||||
@@ -4,5 +4,4 @@ version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
deranged = "0.5.8"
|
||||
rand = { version = "0.10.1", default-features = false, features = ["thread_rng"] }
|
||||
|
||||
+32
-4
@@ -13,9 +13,10 @@ pub trait Game {
|
||||
/// 2 bits for suit ID
|
||||
/// 4 bits for card Value
|
||||
/// TODO: better encoding for slightly more decks
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||
pub struct Card(u8);
|
||||
pub struct CardValue(deranged::RangedU8<1, 13>);
|
||||
pub struct CardValue(u8);
|
||||
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
||||
pub enum Suit {
|
||||
Spades,
|
||||
Hearts,
|
||||
@@ -25,8 +26,7 @@ pub enum Suit {
|
||||
impl Card {
|
||||
pub fn value(&self) -> CardValue {
|
||||
let masked = self.0 & 0b1111;
|
||||
let value = unsafe { deranged::RangedU8::new_unchecked(masked) };
|
||||
CardValue(value)
|
||||
CardValue(masked)
|
||||
}
|
||||
pub fn suit(&self) -> Suit {
|
||||
let red = self.is_red();
|
||||
@@ -67,6 +67,34 @@ impl Stack {
|
||||
use rand::seq::SliceRandom;
|
||||
self.0.shuffle(rng);
|
||||
}
|
||||
pub fn push(&mut self, card: Card) {
|
||||
self.0.push(card);
|
||||
}
|
||||
pub fn pop(&mut self) -> Option<Card> {
|
||||
self.0.pop()
|
||||
}
|
||||
pub fn is_empty(&mut self) -> bool {
|
||||
self.0.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Pile {
|
||||
face_down: Stack,
|
||||
face_up: Stack,
|
||||
}
|
||||
impl Pile {
|
||||
pub fn pop(&mut self) -> Option<Card> {
|
||||
let card = self.face_up.pop()?;
|
||||
if self.face_up.is_empty() {
|
||||
if let Some(card) = self.face_down.pop() {
|
||||
self.face_up.push(card);
|
||||
}
|
||||
}
|
||||
Some(card)
|
||||
}
|
||||
pub fn push(&mut self, card: Card) {
|
||||
self.face_up.push(card);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Session<G: Game> {
|
||||
|
||||
+18
-9
@@ -1,15 +1,11 @@
|
||||
use crate::Rng;
|
||||
use crate::card_game::{Card, Game, Stack};
|
||||
use crate::card_game::{Card, Game, Pile, Stack};
|
||||
|
||||
struct Pile {
|
||||
face_down: Stack,
|
||||
face_up: Stack,
|
||||
}
|
||||
struct KlondikeConfig {}
|
||||
struct KlondikeState {
|
||||
piles: [Pile; 14],
|
||||
}
|
||||
enum KlondikePileId {
|
||||
pub enum KlondikePileId {
|
||||
Stock,
|
||||
Hand,
|
||||
Foundation0,
|
||||
@@ -25,9 +21,21 @@ enum KlondikePileId {
|
||||
Tableau6,
|
||||
Tableau7,
|
||||
}
|
||||
impl std::ops::Index<KlondikePileId> for KlondikeState {
|
||||
type Output = Pile;
|
||||
fn index(&self, index: KlondikePileId) -> &Self::Output {
|
||||
&self.piles[index as usize]
|
||||
}
|
||||
}
|
||||
impl std::ops::IndexMut<KlondikePileId> for KlondikeState {
|
||||
fn index_mut(&mut self, index: KlondikePileId) -> &mut Self::Output {
|
||||
&mut self.piles[index as usize]
|
||||
}
|
||||
}
|
||||
|
||||
pub struct KlondikeInstruction {
|
||||
src: KlondikePileId,
|
||||
dst: KlondikePileId,
|
||||
pub src: KlondikePileId,
|
||||
pub dst: KlondikePileId,
|
||||
}
|
||||
pub struct Klondike {
|
||||
config: KlondikeConfig,
|
||||
@@ -49,6 +57,7 @@ impl Game for Klondike {
|
||||
todo!()
|
||||
}
|
||||
fn process_instruction(&mut self, instruction: Self::Instruction) {
|
||||
todo!()
|
||||
let card = self.state[instruction.src].pop().unwrap();
|
||||
self.state[instruction.dst].push(card);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user