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, foundation: Foundation,
} }
impl DstFoundation { impl DstFoundation {
const ITER_BEGIN: Self = Self {
src: KlondikePile::ITER_BEGIN,
foundation: Foundation::ITER_BEGIN,
};
const fn next(self) -> Option<Self> { const fn next(self) -> Option<Self> {
let DstFoundation { src, foundation } = self; let DstFoundation { src, foundation } = self;
if let Some(src) = src.next() { if let Some(src) = src.next() {
@@ -222,6 +226,7 @@ pub enum KlondikeInstruction {
RotateStock, RotateStock,
} }
impl KlondikeInstruction { impl KlondikeInstruction {
const ITER_BEGIN: Self = Self::DstFoundation(DstFoundation::ITER_BEGIN);
const fn next(self) -> Option<Self> { const fn next(self) -> Option<Self> {
Some(match self { Some(match self {
Self::DstFoundation(dst_foundation) => match dst_foundation.next() { Self::DstFoundation(dst_foundation) => match dst_foundation.next() {
@@ -387,33 +392,16 @@ impl KlondikeState {
fn is_instruction_valid(&self, instruction: KlondikeInstruction) -> bool { fn is_instruction_valid(&self, instruction: KlondikeInstruction) -> bool {
match instruction { match instruction {
// Stock -> Stock draws a card or resets the stock // Stock -> Stock draws a card or resets the stock
KlondikeInstruction { KlondikeInstruction::RotateStock => {
src: InstructionSrc::STOCK,
dst: KlondikePileId::Stock,
} => {
// cannot move stock when stock is empty // cannot move stock when stock is empty
!self.stock.is_empty() !self.stock.is_empty()
} }
// cannot move cards to stock
KlondikeInstruction {
src: _,
dst: KlondikePileId::Stock,
} => false,
// moving to foundation has special rules // moving to foundation has special rules
KlondikeInstruction { src, dst } KlondikeInstruction::DstFoundation(dst_foundation) => {
if matches!(
dst,
KlondikePileId::Foundation1
| KlondikePileId::Foundation2
| KlondikePileId::Foundation3
| KlondikePileId::Foundation4
) =>
{
// get the top cards // get the top cards
if let Some(src_card) = self.src_card(src) { if let Some(src_card) = self.src_card(dst_foundation.src) {
match self.dst_card(dst) { match self.dst_card(dst_foundation.foundation) {
// destination card exists // destination card exists
Some(dst_card) => { Some(dst_card) => {
// suit matches? // suit matches?
@@ -429,10 +417,10 @@ impl KlondikeState {
} }
} }
// other = move to tableau // other = move to tableau
KlondikeInstruction { src, dst } => { KlondikeInstruction::DstTableau(dst_tableau) => {
// get the top cards // get the top cards
if let Some(src_card) = self.src_card(src) { if let Some(src_card) = self.src_card(dst_tableau.src) {
match self.dst_card(dst) { match self.dst_card(dst_tableau.tableau) {
// destination card exists // destination card exists
Some(dst_card) => { Some(dst_card) => {
// red-ness is opposite? // red-ness is opposite?
@@ -457,10 +445,7 @@ pub struct KlondikeIter {
impl KlondikeIter { impl KlondikeIter {
const fn new() -> Self { const fn new() -> Self {
Self { Self {
instruction: Some(KlondikeInstruction { instruction: Some(KlondikeInstruction::ITER_BEGIN),
src: InstructionSrc::new(KlondikePileStack::Tableau1(SkipCards::Skip0)),
dst: KlondikePileId::Tableau2,
}),
} }
} }
} }