Refactor KlondikeInstruction #4

Merged
Quaternions merged 4 commits from dst-ins-enum into master 2026-05-16 19:09:11 +00:00
Showing only changes of commit d601777e9d - Show all commits
+13 -28
View File
@@ -179,6 +179,10 @@ pub struct DstFoundation {
foundation: Foundation,
}
impl DstFoundation {
const ITER_BEGIN: Self = Self {
src: KlondikePile::ITER_BEGIN,
foundation: Foundation::ITER_BEGIN,
};
const fn next(self) -> Option<Self> {
let DstFoundation { src, foundation } = self;
if let Some(src) = src.next() {
@@ -222,6 +226,7 @@ pub enum KlondikeInstruction {
RotateStock,
}
impl KlondikeInstruction {
const ITER_BEGIN: Self = Self::DstFoundation(DstFoundation::ITER_BEGIN);
const fn next(self) -> Option<Self> {
Some(match self {
Self::DstFoundation(dst_foundation) => match dst_foundation.next() {
@@ -387,33 +392,16 @@ impl KlondikeState {
fn is_instruction_valid(&self, instruction: KlondikeInstruction) -> bool {
match instruction {
// Stock -> Stock draws a card or resets the stock
KlondikeInstruction {
src: InstructionSrc::STOCK,
dst: KlondikePileId::Stock,
} => {
KlondikeInstruction::RotateStock => {
// cannot move stock when stock is empty
!self.stock.is_empty()
}
// cannot move cards to stock
KlondikeInstruction {
src: _,
dst: KlondikePileId::Stock,
} => false,
// moving to foundation has special rules
KlondikeInstruction { src, dst }
if matches!(
dst,
KlondikePileId::Foundation1
| KlondikePileId::Foundation2
| KlondikePileId::Foundation3
| KlondikePileId::Foundation4
) =>
{
KlondikeInstruction::DstFoundation(dst_foundation) => {
// get the top cards
if let Some(src_card) = self.src_card(src) {
match self.dst_card(dst) {
if let Some(src_card) = self.src_card(dst_foundation.src) {
match self.dst_card(dst_foundation.foundation) {
// destination card exists
Some(dst_card) => {
// suit matches?
@@ -429,10 +417,10 @@ impl KlondikeState {
}
}
// other = move to tableau
KlondikeInstruction { src, dst } => {
KlondikeInstruction::DstTableau(dst_tableau) => {
// get the top cards
if let Some(src_card) = self.src_card(src) {
match self.dst_card(dst) {
if let Some(src_card) = self.src_card(dst_tableau.src) {
match self.dst_card(dst_tableau.tableau) {
// destination card exists
Some(dst_card) => {
// red-ness is opposite?
@@ -457,10 +445,7 @@ pub struct KlondikeIter {
impl KlondikeIter {
const fn new() -> Self {
Self {
instruction: Some(KlondikeInstruction {
src: InstructionSrc::new(KlondikePileStack::Tableau1(SkipCards::Skip0)),
dst: KlondikePileId::Tableau2,
}),
instruction: Some(KlondikeInstruction::ITER_BEGIN),
}
}
}