go wild on KlondikeInstruction

This commit is contained in:
2026-05-16 11:29:25 -07:00
parent 0d98f8e25e
commit 4fdc011be5
2 changed files with 192 additions and 213 deletions
+179 -200
View File
@@ -9,9 +9,8 @@ impl Default for KlondikeConfig {
} }
} }
#[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum KlondikePileId { pub enum Tableau {
Tableau1, Tableau1,
Tableau2, Tableau2,
Tableau3, Tableau3,
@@ -19,15 +18,11 @@ pub enum KlondikePileId {
Tableau5, Tableau5,
Tableau6, Tableau6,
Tableau7, Tableau7,
Foundation1,
Foundation2,
Foundation3,
Foundation4,
Stock,
} }
impl KlondikePileId { impl Tableau {
const ITER_BEGIN: Self = Self::Tableau1;
const fn next(self) -> Option<Self> { const fn next(self) -> Option<Self> {
use KlondikePileId::*; use Tableau::*;
Some(match self { Some(match self {
Tableau1 => Tableau2, Tableau1 => Tableau2,
Tableau2 => Tableau3, Tableau2 => Tableau3,
@@ -35,229 +30,213 @@ impl KlondikePileId {
Tableau4 => Tableau5, Tableau4 => Tableau5,
Tableau5 => Tableau6, Tableau5 => Tableau6,
Tableau6 => Tableau7, Tableau6 => Tableau7,
Tableau7 => Foundation1, Tableau7 => return None,
Foundation1 => Foundation2,
Foundation2 => Foundation3,
Foundation3 => Foundation4,
Foundation4 => Stock,
Stock => return None,
}) })
} }
} }
/// high four bits for stack depth, low four bits for Pile Id
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct InstructionSrc(u8); pub enum Foundation {
impl InstructionSrc { Foundation1,
const STOCK: Self = InstructionSrc::new(KlondikePileStack::Stock); Foundation2,
pub const fn new(src: KlondikePileStack) -> Self { Foundation3,
match src { Foundation4,
KlondikePileStack::Tableau1(skip_cards) => { }
Self(KlondikePileId::Tableau1 as u8 + ((skip_cards as u8) << 4)) impl Foundation {
} const ITER_BEGIN: Self = Self::Foundation1;
KlondikePileStack::Tableau2(skip_cards) => {
Self(KlondikePileId::Tableau2 as u8 + ((skip_cards as u8) << 4))
}
KlondikePileStack::Tableau3(skip_cards) => {
Self(KlondikePileId::Tableau3 as u8 + ((skip_cards as u8) << 4))
}
KlondikePileStack::Tableau4(skip_cards) => {
Self(KlondikePileId::Tableau4 as u8 + ((skip_cards as u8) << 4))
}
KlondikePileStack::Tableau5(skip_cards) => {
Self(KlondikePileId::Tableau5 as u8 + ((skip_cards as u8) << 4))
}
KlondikePileStack::Tableau6(skip_cards) => {
Self(KlondikePileId::Tableau6 as u8 + ((skip_cards as u8) << 4))
}
KlondikePileStack::Tableau7(skip_cards) => {
Self(KlondikePileId::Tableau7 as u8 + ((skip_cards as u8) << 4))
}
KlondikePileStack::Foundation1 => Self(KlondikePileId::Foundation1 as u8),
KlondikePileStack::Foundation2 => Self(KlondikePileId::Foundation2 as u8),
KlondikePileStack::Foundation3 => Self(KlondikePileId::Foundation3 as u8),
KlondikePileStack::Foundation4 => Self(KlondikePileId::Foundation4 as u8),
KlondikePileStack::Stock => Self(KlondikePileId::Stock as u8),
}
}
const fn into_spec(self) -> KlondikePileStack {
// SAFETY: there is no way to construct an invalid InstructionSrc
let pile = unsafe { core::mem::transmute(self.0 & 0b1111) };
match pile {
KlondikePileId::Tableau1 => {
KlondikePileStack::Tableau1(unsafe { core::mem::transmute(self.0 >> 4) })
}
KlondikePileId::Tableau2 => {
KlondikePileStack::Tableau2(unsafe { core::mem::transmute(self.0 >> 4) })
}
KlondikePileId::Tableau3 => {
KlondikePileStack::Tableau3(unsafe { core::mem::transmute(self.0 >> 4) })
}
KlondikePileId::Tableau4 => {
KlondikePileStack::Tableau4(unsafe { core::mem::transmute(self.0 >> 4) })
}
KlondikePileId::Tableau5 => {
KlondikePileStack::Tableau5(unsafe { core::mem::transmute(self.0 >> 4) })
}
KlondikePileId::Tableau6 => {
KlondikePileStack::Tableau6(unsafe { core::mem::transmute(self.0 >> 4) })
}
KlondikePileId::Tableau7 => {
KlondikePileStack::Tableau7(unsafe { core::mem::transmute(self.0 >> 4) })
}
KlondikePileId::Foundation1 => KlondikePileStack::Foundation1,
KlondikePileId::Foundation2 => KlondikePileStack::Foundation2,
KlondikePileId::Foundation3 => KlondikePileStack::Foundation3,
KlondikePileId::Foundation4 => KlondikePileStack::Foundation4,
KlondikePileId::Stock => KlondikePileStack::Stock,
}
}
const fn next(self) -> Option<Self> { const fn next(self) -> Option<Self> {
match self.into_spec().next() { use Foundation::*;
Some(s) => Some(Self::new(s)), Some(match self {
None => None, Foundation1 => Foundation2,
} Foundation2 => Foundation3,
Foundation3 => Foundation4,
Foundation4 => return None,
})
} }
} }
impl From<InstructionSrc> for KlondikePileId {
fn from(value: InstructionSrc) -> Self { #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
match value.into_spec() { pub enum KlondikePile {
KlondikePileStack::Tableau1(_) => KlondikePileId::Tableau1, Tableau(Tableau),
KlondikePileStack::Tableau2(_) => KlondikePileId::Tableau2, Stock,
KlondikePileStack::Tableau3(_) => KlondikePileId::Tableau3, Foundation(Foundation),
KlondikePileStack::Tableau4(_) => KlondikePileId::Tableau4, }
KlondikePileStack::Tableau5(_) => KlondikePileId::Tableau5, impl KlondikePile {
KlondikePileStack::Tableau6(_) => KlondikePileId::Tableau6, const ITER_BEGIN: Self = Self::Tableau(Tableau::ITER_BEGIN);
KlondikePileStack::Tableau7(_) => KlondikePileId::Tableau7, const fn next(self) -> Option<Self> {
KlondikePileStack::Foundation1 => KlondikePileId::Foundation1, Some(match self {
KlondikePileStack::Foundation2 => KlondikePileId::Foundation2, Self::Tableau(tableau_stack) => match tableau_stack.next() {
KlondikePileStack::Foundation3 => KlondikePileId::Foundation3, Some(tableau_stack) => Self::Tableau(tableau_stack),
KlondikePileStack::Foundation4 => KlondikePileId::Foundation4, None => Self::Stock,
KlondikePileStack::Stock => KlondikePileId::Stock, },
} Self::Stock => Self::Foundation(Foundation::ITER_BEGIN),
Self::Foundation(foundation) => match foundation.next() {
Some(foundation) => Self::Foundation(foundation),
None => return None,
},
})
} }
} }
#[repr(u8)] #[repr(u8)]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum SkipCards { pub enum SkipCards {
Zero, Skip0,
One, Skip1,
Two, Skip2,
Three, Skip3,
Four, Skip4,
Five, Skip5,
Six, Skip6,
Seven, Skip7,
Eight, Skip8,
Nine, Skip9,
Ten, Skip10,
Eleven, Skip11,
Twelve, Skip12,
} }
impl SkipCards { impl SkipCards {
const ITER_BEGIN: Self = Self::Skip0;
const fn next(self) -> Option<Self> { const fn next(self) -> Option<Self> {
use SkipCards::*; use SkipCards::*;
Some(match self { Some(match self {
Zero => One, Skip0 => Skip1,
One => Two, Skip1 => Skip2,
Two => Three, Skip2 => Skip3,
Three => Four, Skip3 => Skip4,
Four => Five, Skip4 => Skip5,
Five => Six, Skip5 => Skip6,
Six => Seven, Skip6 => Skip7,
Seven => Eight, Skip7 => Skip8,
Eight => Nine, Skip8 => Skip9,
Nine => Ten, Skip9 => Skip10,
Ten => Eleven, Skip10 => Skip11,
Eleven => Twelve, Skip11 => Skip12,
Twelve => return None, Skip12 => return None,
})
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum KlondikePileStack {
Tableau1(SkipCards),
Tableau2(SkipCards),
Tableau3(SkipCards),
Tableau4(SkipCards),
Tableau5(SkipCards),
Tableau6(SkipCards),
Tableau7(SkipCards),
Foundation1,
Foundation2,
Foundation3,
Foundation4,
Stock,
}
impl KlondikePileStack {
const fn next(self) -> Option<Self> {
use KlondikePileStack::*;
Some(match self {
Tableau1(skip) => match skip.next() {
Some(next) => Tableau1(next),
None => Tableau2(SkipCards::Zero),
},
Tableau2(skip) => match skip.next() {
Some(next) => Tableau2(next),
None => Tableau3(SkipCards::Zero),
},
Tableau3(skip) => match skip.next() {
Some(next) => Tableau3(next),
None => Tableau4(SkipCards::Zero),
},
Tableau4(skip) => match skip.next() {
Some(next) => Tableau4(next),
None => Tableau5(SkipCards::Zero),
},
Tableau5(skip) => match skip.next() {
Some(next) => Tableau5(next),
None => Tableau6(SkipCards::Zero),
},
Tableau6(skip) => match skip.next() {
Some(next) => Tableau6(next),
None => Tableau7(SkipCards::Zero),
},
Tableau7(skip) => match skip.next() {
Some(next) => Tableau7(next),
None => Foundation1,
},
Foundation1 => Foundation2,
Foundation2 => Foundation3,
Foundation3 => Foundation4,
Foundation4 => Stock,
Stock => return None,
}) })
} }
} }
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct KlondikeInstruction { pub struct TableauStack {
pub src: InstructionSrc, tableau: Tableau,
pub dst: KlondikePileId, skip_cards: SkipCards,
} }
impl KlondikeInstruction {
pub const fn stock() -> Self { impl TableauStack {
Self { const ITER_BEGIN: Self = Self {
src: InstructionSrc::STOCK, tableau: Tableau::ITER_BEGIN,
dst: KlondikePileId::Stock, skip_cards: SkipCards::ITER_BEGIN,
} };
}
const fn next(self) -> Option<Self> { const fn next(self) -> Option<Self> {
let KlondikeInstruction { src, dst } = self; let TableauStack {
if let Some(next_dst) = dst.next() { tableau,
return Some(Self { src, dst: next_dst }); skip_cards,
} } = self;
if let Some(next_src) = src.next() { if let Some(skip_cards) = skip_cards.next() {
return Some(Self { return Some(Self {
src: next_src, tableau,
dst: KlondikePileId::Tableau1, skip_cards,
});
}
if let Some(tableau) = tableau.next() {
let skip_cards = SkipCards::Skip0;
return Some(Self {
tableau,
skip_cards,
}); });
} }
None None
} }
} }
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum KlondikePileStack {
Tableau(TableauStack),
Stock,
Foundation(Foundation),
}
impl KlondikePileStack {
const ITER_BEGIN: Self = Self::Tableau(TableauStack::ITER_BEGIN);
const fn next(self) -> Option<Self> {
Some(match self {
Self::Tableau(tableau_stack) => match tableau_stack.next() {
Some(tableau_stack) => Self::Tableau(tableau_stack),
None => Self::Stock,
},
Self::Stock => Self::Foundation(Foundation::ITER_BEGIN),
Self::Foundation(foundation) => match foundation.next() {
Some(foundation) => Self::Foundation(foundation),
None => return None,
},
})
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct DstFoundation {
src: KlondikePile,
foundation: Foundation,
}
impl DstFoundation {
const fn next(self) -> Option<Self> {
let DstFoundation { src, foundation } = self;
if let Some(src) = src.next() {
return Some(Self { src, foundation });
}
if let Some(foundation) = foundation.next() {
let src = KlondikePile::ITER_BEGIN;
return Some(Self { src, foundation });
}
None
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct DstTableau {
src: KlondikePileStack,
tableau: Tableau,
}
impl DstTableau {
const ITER_BEGIN: Self = Self {
src: KlondikePileStack::ITER_BEGIN,
tableau: Tableau::ITER_BEGIN,
};
const fn next(self) -> Option<Self> {
let DstTableau { src, tableau } = self;
if let Some(src) = src.next() {
return Some(Self { src, tableau });
}
if let Some(tableau) = tableau.next() {
let src = KlondikePileStack::ITER_BEGIN;
return Some(Self { src, tableau });
}
None
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum KlondikeInstruction {
DstFoundation(DstFoundation),
DstTableau(DstTableau),
RotateStock,
}
impl KlondikeInstruction {
const fn next(self) -> Option<Self> {
Some(match self {
Self::DstFoundation(dst_foundation) => match dst_foundation.next() {
Some(dst_foundation) => Self::DstFoundation(dst_foundation),
None => Self::DstTableau(DstTableau::ITER_BEGIN),
},
Self::DstTableau(tableau) => match tableau.next() {
Some(tableau) => Self::DstTableau(tableau),
None => Self::RotateStock,
},
Self::RotateStock => return None,
})
}
}
const TABLEAUS: usize = 7; const TABLEAUS: usize = 7;
const fn sum(n: usize) -> usize { const fn sum(n: usize) -> usize {
n * (n + 1) / 2 n * (n + 1) / 2
@@ -479,7 +458,7 @@ impl KlondikeIter {
const fn new() -> Self { const fn new() -> Self {
Self { Self {
instruction: Some(KlondikeInstruction { instruction: Some(KlondikeInstruction {
src: InstructionSrc::new(KlondikePileStack::Tableau1(SkipCards::Zero)), src: InstructionSrc::new(KlondikePileStack::Tableau1(SkipCards::Skip0)),
dst: KlondikePileId::Tableau2, dst: KlondikePileId::Tableau2,
}), }),
} }
+13 -13
View File
@@ -152,19 +152,19 @@ fn find_valid_instruction(
naive_instruction: NaiveInstruction, naive_instruction: NaiveInstruction,
) -> Option<KlondikeInstruction> { ) -> Option<KlondikeInstruction> {
const SKIP_LIST: [SkipCards; 13] = [ const SKIP_LIST: [SkipCards; 13] = [
SkipCards::Zero, SkipCards::Skip0,
SkipCards::One, SkipCards::Skip1,
SkipCards::Two, SkipCards::Skip2,
SkipCards::Three, SkipCards::Skip3,
SkipCards::Four, SkipCards::Skip4,
SkipCards::Five, SkipCards::Skip5,
SkipCards::Six, SkipCards::Skip6,
SkipCards::Seven, SkipCards::Skip7,
SkipCards::Eight, SkipCards::Skip8,
SkipCards::Nine, SkipCards::Skip9,
SkipCards::Ten, SkipCards::Skip10,
SkipCards::Eleven, SkipCards::Skip11,
SkipCards::Twelve, SkipCards::Skip12,
]; ];
let dst = naive_instruction.dst; let dst = naive_instruction.dst;
let src = match naive_instruction.src { let src = match naive_instruction.src {