use crate::card::{Card, JOKER, REAL_FACES};
use crate::rules::*;
use super::counters::AuxCtx;
use super::{Action, Game, Move, PileRules, State};
fn follows_in_cycle(
cycle: &[crate::card::Suit],
under: crate::card::Suit,
over: crate::card::Suit,
) -> bool {
cycle
.iter()
.position(|&x| x == under)
.is_some_and(|i| cycle[(i + 1) % cycle.len()] == over)
}
impl Game {
pub(crate) fn fits(&self, b: &Build, under: Card, over: Card) -> bool {
if b.by == BuildBy::SuitOrSameRank {
if b.blocked_ranks.contains(&under.rank()) {
return false;
}
if under.rank() == over.rank() {
return true;
}
if under.suit() != over.suit() {
return false;
}
}
let ok_suit = match b.by {
BuildBy::None => return false,
BuildBy::Any => true,
BuildBy::Suit | BuildBy::SuitOrSameRank => under.suit() == over.suit(),
BuildBy::Alternate => under.color() != over.color(),
BuildBy::SameColor => under.color() == over.color(),
BuildBy::OtherSuit => under.suit() != over.suit(),
BuildBy::SuitOrRank => under.suit() == over.suit() || under.rank() == over.rank(),
BuildBy::SuitCycle => follows_in_cycle(&b.suit_cycle, under.suit(), over.suit()),
BuildBy::SuitChain => {
let (u, o) = (
self.rank_idx[under.rank() as usize] as i16,
self.rank_idx[over.rank() as usize] as i16,
);
let d = o - u;
return u != 255
&& o != 255
&& ((d == b.step as i16 && under.suit() == over.suit())
|| (b.wrap
&& d == b.step as i16 - self.nranks as i16
&& follows_in_cycle(&b.suit_cycle, under.suit(), over.suit())));
}
BuildBy::RankSuitColor => {
let (u, o) = (
self.rank_idx[under.rank() as usize] as i16,
self.rank_idx[over.rank() as usize] as i16,
);
let n = self.nranks as i16;
return if u == o {
under.color() != over.color()
} else if under.suit() == over.suit() {
(o - u).rem_euclid(n) == 1
} else {
under.color() == over.color() && (u - o).rem_euclid(n) == 1
};
}
};
if !ok_suit || b.blocked_ranks.contains(&under.rank()) {
return false;
}
if b.not_over.contains(&(under.rank(), over.rank())) {
return false;
}
if b.extra_over.contains(&(under.rank(), over.rank())) {
return true;
}
if b.extra_pairs.iter().any(|&(a, c)| {
(under.rank() == a && over.rank() == c) || (under.rank() == c && over.rank() == a)
}) {
return true;
}
let (u, o) = (
self.rank_idx[under.rank() as usize],
self.rank_idx[over.rank() as usize],
);
if u == 255 || o == 255 {
return false;
}
let n = self.nranks as i16;
let step = b
.suit_steps
.iter()
.find(|&&(s, _)| s == under.suit())
.map_or(b.step, |&(_, k)| k) as i16;
let up = |from: u8, to: u8, to_rank: u8| -> bool {
let d = to as i16 - from as i16;
(d == step && !(step == 1 && b.gap_ranks.contains(&to_rank)))
|| (b.wrap && d == step - n)
};
let (ur, or) = (under.rank(), over.rank());
match b.dir {
Dir::Up => up(u, o, or),
Dir::UpAny => o > u,
Dir::Down => up(o, u, ur),
Dir::DownAny => o < u,
Dir::DownOrSame => u == o || up(o, u, ur),
Dir::DoubleUp => o as i16 == (2 * u as i16 + 1) % n,
Dir::Mirror => o as i16 + u as i16 == n - 1,
Dir::Either => up(u, o, or) || up(o, u, ur),
Dir::Same => u == o,
Dir::Any => true,
}
}
#[inline]
pub(crate) fn rule(&self, pile: u16) -> &PileRules {
&self.pile_rules[pile as usize]
}
pub(crate) fn empty_accepts(&self, pile: u16, card: Card, s: &State) -> bool {
let r = self.rule(pile);
if r.admits >> card.face() & 1 == 0 {
return false;
}
if card.is_joker() {
return (r.empty == EmptyRule::Any && r.empty_suit.is_none())
|| (r.wild && self.empty_set(pile, s) != 0);
}
if let Some(es) = r.empty_suit {
if card.suit() != es {
return false;
}
}
let g = self.group_of(pile);
if g.unique_suits
&& g.piles().any(|p| {
p != pile
&& s.piles[p as usize]
.first()
.is_some_and(|c| c.suit() == card.suit())
})
{
return false;
}
if (g.unique_ranks || g.same_suit_group || g.same_rank_group)
&& g.piles().any(|p| {
p != pile
&& s.piles[p as usize].first().is_some_and(|c| {
(g.unique_ranks && c.rank() == card.rank())
|| (g.same_suit_group && c.suit() != card.suit())
|| (g.same_rank_group && c.rank() != card.rank())
})
})
{
return false;
}
if g.cyclic_ranks && !self.cyclic_fits(g, pile, card.rank(), s) {
return false;
}
if g.empty_needs_first_full && !self.first_pile_full(self.pile_group[pile as usize], s) {
return false;
}
if self.empty_is_open(pile, s) {
return true;
}
match &r.empty {
EmptyRule::None => false,
EmptyRule::Any => true,
EmptyRule::Rank(r) => card.rank() == *r,
EmptyRule::Ranks(ranks) => ranks.contains(&card.rank()),
EmptyRule::ExceptRanks(ranks) => !ranks.contains(&card.rank()),
EmptyRule::DealtBase => s.base_rank.is_none_or(|b| card.rank() == b),
EmptyRule::DealtBaseOffset(o) => s
.base_rank
.is_some_and(|b| card.rank() == self.rank_from(b, *o)),
EmptyRule::AfterLeft { .. }
| EmptyRule::AfterNeighbor { .. }
| EmptyRule::AfterRing { .. }
| EmptyRule::AfterPile { .. }
| EmptyRule::AfterPileBottom { .. }
| EmptyRule::SameBase => self.empty_set(pile, s) >> card.face() & 1 != 0,
}
}
fn first_pile_full(&self, gi: u16, s: &State) -> bool {
let first = self.groups[gi as usize].start;
s.piles[first as usize].len() >= self.rule(first).max_cards as usize
}
fn empty_is_open(&self, pile: u16, s: &State) -> bool {
let g = self.group_of(pile);
if g.empty_open_when_full
.is_some_and(|gi| self.first_pile_full(gi, s))
{
return true;
}
let (Some(need), EmptyRule::Rank(k)) = (g.empty_open_after_bases, &self.rule(pile).empty)
else {
return false;
};
g.piles()
.filter(|&p| s.piles[p as usize].first().is_some_and(|c| c.rank() == *k))
.count()
>= need as usize
}
pub(crate) fn rank_from(&self, base: u8, offset: i8) -> u8 {
let n = self.nranks as i16;
let at = self.rank_idx[base as usize];
if at == 255 {
return base;
}
let to = (at as i16 + offset as i16).rem_euclid(n);
self.rules.pack.ranks[to as usize]
}
pub(crate) fn empty_set(&self, pile: u16, s: &State) -> u64 {
let r = self.rule(pile);
let mut real = r.admits & REAL_FACES;
if let Some(es) = r.empty_suit {
real &= self.suit_faces[es as usize];
}
let g = self.group_of(pile);
if g.unique_suits {
for p in g.piles() {
if p != pile {
if let Some(&c) = s.piles[p as usize].first() {
if !c.is_joker() {
real &= !self.suit_faces[c.suit() as usize];
}
}
}
}
}
if g.empty_needs_first_full && !self.first_pile_full(self.pile_group[pile as usize], s) {
return 0;
}
if g.cyclic_ranks {
for r in 1..14u8 {
if self.rank_faces[r as usize] != 0 && !self.cyclic_fits(g, pile, r, s) {
real &= !self.rank_faces[r as usize];
}
}
}
if g.unique_ranks || g.same_suit_group || g.same_rank_group {
for p in g.piles() {
if p == pile {
continue;
}
if let Some(&c) = s.piles[p as usize].first() {
if c.is_joker() {
continue;
}
if g.unique_ranks {
real &= !self.rank_faces[c.rank() as usize];
}
if g.same_suit_group {
real &= self.suit_faces[c.suit() as usize];
}
if g.same_rank_group {
real &= self.rank_faces[c.rank() as usize];
}
}
}
}
if self.empty_is_open(pile, s) {
return real;
}
match &r.empty {
EmptyRule::None => 0,
EmptyRule::Any => real,
EmptyRule::Rank(k) => real & self.rank_faces[*k as usize],
EmptyRule::Ranks(ranks) => ranks
.iter()
.fold(0u64, |m, &k| m | (real & self.rank_faces[k as usize])),
EmptyRule::ExceptRanks(ranks) => {
let excluded = ranks
.iter()
.fold(0u64, |m, &k| m | self.rank_faces[k as usize]);
real & !excluded
}
EmptyRule::DealtBase => s
.base_rank
.map_or(real, |b| real & self.rank_faces[b as usize]),
EmptyRule::DealtBaseOffset(o) => s.base_rank.map_or(0, |b| {
real & self.rank_faces[self.rank_from(b, *o) as usize]
}),
EmptyRule::AfterLeft { first } => {
let first = *first;
let g = self.group_of(pile);
let cols = g.columns.unwrap_or(g.count);
if (pile - g.start) % cols == 0 {
return real & self.rank_faces[first as usize];
}
let left = &s.piles[pile as usize - 1];
if left.is_empty() {
return 0;
}
let base = if left.iter().all(|c| c.is_joker()) {
self.empty_set(pile - 1, s)
} else {
0
};
real & self.accept_set(r, left, base, false)
}
EmptyRule::AfterNeighbor { first } => {
let first = *first;
let g = self.group_of(pile);
let cols = g.columns.unwrap_or(g.count);
let col = (pile - g.start) % cols;
if col == 0 && first != 0 {
return real & self.rank_faces[first as usize];
}
let mut out = 0u64;
if col > 0 {
let left = &s.piles[pile as usize - 1];
if !left.is_empty() {
let base = if left.iter().all(|c| c.is_joker()) {
self.empty_set(pile - 1, s)
} else {
0
};
out |= real & self.accept_set(r, left, base, false);
}
}
if col + 1 < cols {
let right = &s.piles[pile as usize + 1];
if let Some(&rc) = right.last() {
if rc.is_face_up() && !rc.is_joker() {
let mut m = real;
while m != 0 {
let f = m.trailing_zeros() as usize;
if r.fits[f] >> rc.face() & 1 != 0 {
out |= 1 << f;
}
m &= m - 1;
}
}
}
}
out
}
EmptyRule::AfterRing { left_extra } => {
let g = self.group_of(pile);
let n = g.count;
let i = pile - g.start;
let mut out = 0u64;
let left = &s.piles[(g.start + (i + n - 1) % n) as usize];
if let Some(&lc) = left.last() {
out |= real & self.accept_set(r, left, 0, false);
for &(under, over) in left_extra {
if lc.rank() == under {
out |= real & self.rank_faces[over as usize];
}
}
}
let right = &s.piles[(g.start + (i + 1) % n) as usize];
if let Some(&rc) = right.last() {
if rc.is_face_up() && !rc.is_joker() {
let mut m = real;
while m != 0 {
let f = m.trailing_zeros() as usize;
if r.fits[f] >> rc.face() & 1 != 0 {
out |= 1 << f;
}
m &= m - 1;
}
}
}
out
}
EmptyRule::AfterPile { .. } => {
let (ref_gi, offset) = r.after_pile.unwrap();
let idx = pile - self.group_of(pile).start;
let ref_pile = self.groups[ref_gi as usize].start + idx;
match s.piles[ref_pile as usize].last() {
Some(&c) if c.is_face_up() && !c.is_joker() => {
let want = self.rank_from(c.rank(), offset);
real & self.rank_faces[want as usize]
}
_ => 0,
}
}
EmptyRule::AfterPileBottom { .. } => {
let (ref_gi, ref_idx, offset) = r.after_pile_bottom.unwrap();
let ref_pile = self.groups[ref_gi as usize].start + ref_idx;
match s.piles[ref_pile as usize].first() {
Some(&c) if !c.is_joker() => {
let want = self.rank_from(c.rank(), offset);
real & self.rank_faces[want as usize]
}
_ => 0,
}
}
EmptyRule::SameBase => {
let base = g
.piles()
.find_map(|q| s.piles[q as usize].first().filter(|c| !c.is_joker()))
.map(|c| c.rank());
base.map_or(real, |b| real & self.rank_faces[b as usize])
}
}
}
#[inline]
pub(crate) fn step_set(&self, r: &PileRules, set: u64, suit_only: bool) -> u64 {
let mut out = 0u64;
let mut m = set & REAL_FACES;
while m != 0 {
let f = m.trailing_zeros() as usize;
let mut fit = r.fits[f];
if suit_only {
fit &= self.suit_faces[f / 13];
}
out |= fit;
m &= m - 1;
}
out & REAL_FACES
}
pub(crate) fn accept_set(
&self,
r: &PileRules,
cards: &[Card],
base: u64,
suit_only: bool,
) -> u64 {
let Some(&top) = cards.last() else {
return base;
};
if !top.is_face_up() {
return 0;
}
if !top.is_joker() {
let f = top.face() as usize;
let mut fit = r.fits[f];
if suit_only {
fit &= self.suit_faces[f / 13];
}
return fit & REAL_FACES;
}
if r.wild_permissive {
return r.admits & REAL_FACES;
}
if !r.wild {
return 0;
}
let k = cards
.iter()
.rev()
.take_while(|c| c.is_joker() && c.is_face_up())
.count();
let below = cards.len() - k;
let mut id = if below == 0 {
base
} else {
let c = cards[below - 1];
if c.is_face_up() {
self.step_set(r, 1 << c.face(), suit_only)
} else {
r.admits & REAL_FACES
}
};
for _ in 1..k {
id = self.step_set(r, id, suit_only);
}
self.step_set(r, id, suit_only)
}
fn unit_fits(
&self,
r: &PileRules,
allowed: u64,
empty_any: bool,
unit: &[Card],
run: bool,
) -> bool {
let bottom = unit[0];
if !bottom.is_joker() {
return allowed >> bottom.face() & 1 != 0;
}
if r.admits >> JOKER & 1 == 0 {
return false;
}
if !r.wild {
return empty_any && (unit.len() == 1 || !run);
}
let mut id = allowed;
if id == 0 && !empty_any {
return false;
}
if empty_any && id == 0 {
id = r.admits & REAL_FACES;
}
if !run || r.wild_permissive {
return true;
}
for &c in &unit[1..] {
let next = self.step_set(r, id, false);
if !c.is_joker() {
return next >> c.face() & 1 != 0;
}
if next == 0 {
return false;
}
id = next;
}
true
}
#[inline]
pub(crate) fn available(&self, s: &State, pile: u16) -> bool {
if !self.has_covers {
return true;
}
let g = self.group_of(pile);
if g.grid_vertical {
let cols = g.columns.unwrap_or(g.count);
let i = pile - g.start;
let above_blocked = i >= cols && !s.piles[(pile - cols) as usize].is_empty();
let below_blocked = i + cols < g.count && !s.piles[(pile + cols) as usize].is_empty();
return !(above_blocked && below_blocked);
}
self.covered_by[pile as usize]
.iter()
.all(|&c| s.piles[c as usize].is_empty())
}
fn dropdown_ok(&self, s: &State, from: u16, to: u16, n: usize) -> bool {
self.group_of(to).dropdown
&& self.covered_by[to as usize].contains(&from)
&& s.piles[from as usize].len() == n
&& s.piles[to as usize].first().is_some_and(|c| c.is_face_up())
&& self.covered_by[to as usize]
.iter()
.all(|&c| c == from || s.piles[c as usize].is_empty())
}
pub(crate) fn movable(&self, s: &State, pile: u16) -> usize {
let cards = &s.piles[pile as usize];
let Some(&top) = cards.last() else { return 0 };
let grp = self.group_of(pile);
let g = self.rule(pile);
if grp.simple_movable {
if !top.is_face_up() {
return 0;
}
} else {
if s.base_rank.is_none()
&& self
.choose_base
.as_ref()
.is_some_and(|cb| cb.lock_mask >> self.pile_group[pile as usize] & 1 != 0)
{
return 0;
}
if let Some((ref_gi, min_cards)) = grp.moves_when_group_cards_at_least {
let total: usize = self.groups[ref_gi as usize]
.piles()
.map(|p| s.piles[p as usize].len())
.sum();
if total < min_cards as usize {
return 0;
}
}
if !top.is_face_up() {
return usize::from(grp.move_face_down_top && g.moves != MoveUnit::None);
}
if let Some(n) = self.hooks.as_ref().and_then(|h| h.movable(self, s, pile)) {
return n.min(cards.len());
}
if grp.moves_when_stock_empty
&& self
.stock_pile
.is_some_and(|sp| !s.piles[sp as usize].is_empty())
{
return 0;
}
if let Some(ref_gi) = grp.moves_when_any_empty {
if !self.groups[ref_gi as usize]
.piles()
.any(|p| s.piles[p as usize].is_empty())
{
return 0;
}
}
if let Some(fr) = grp.move_from_frontier {
let mut rest = grp.piles().filter(|&q| !s.piles[q as usize].is_empty());
let edge = match fr {
crate::rules::Frontier::Last => rest.next_back(),
crate::rules::Frontier::First => rest.next(),
};
if edge != Some(pile) {
return 0;
}
}
if let Some(ref_gi) = grp.moves_when_same_index_empty {
let ref_pile = self.groups[ref_gi as usize].start + (pile - grp.start);
if !s.piles[ref_pile as usize].is_empty() {
return 0;
}
}
if let Some(ref_gi) = grp.moves_when_slice_empty {
let rg = &self.groups[ref_gi as usize];
let k = rg.count / grp.count;
let lo = rg.start + (pile - grp.start) * k;
if (lo..lo + k).any(|p| !s.piles[p as usize].is_empty()) {
return 0;
}
}
if grp.freeze_when_built
&& cards.len() >= g.max_cards as usize
&& cards.windows(2).all(|w| self.fits(&g.build, w[0], w[1]))
{
return 0;
}
}
let n = match g.moves {
MoveUnit::None => 0,
MoveUnit::Top | MoveUnit::AnySingle => 1,
MoveUnit::Whole => {
if cards.iter().all(|c| c.is_face_up()) {
cards.len()
} else {
0
}
}
MoveUnit::AnyFaceUp => cards.iter().rev().take_while(|c| c.is_face_up()).count(),
MoveUnit::Run if !grp.move_fits.is_empty() => {
let mut n = 1;
while n < cards.len() {
let (under, over) = (cards[cards.len() - n - 1], cards[cards.len() - n]);
if !under.is_face_up()
|| !grp
.move_fits
.iter()
.any(|t| t[under.face() as usize] >> over.face() & 1 != 0)
{
break;
}
n += 1;
}
n
}
MoveUnit::SuitSequence => {
let mut n = 1;
while n < cards.len() {
let (under, over) = (cards[cards.len() - n - 1], cards[cards.len() - n]);
if !under.is_face_up()
|| under.suit() != over.suit()
|| self.rank_idx[under.rank() as usize] == 255
|| self.rank_idx[under.rank() as usize]
!= self.rank_idx[over.rank() as usize].wrapping_add(1)
{
break;
}
n += 1;
}
n
}
MoveUnit::SuitRunOrSameRank => {
let mut n_suit = 1;
while n_suit < cards.len() {
let (under, over) =
(cards[cards.len() - n_suit - 1], cards[cards.len() - n_suit]);
if !under.is_face_up()
|| under.suit() != over.suit()
|| under.rank() == over.rank()
|| g.fits[under.face() as usize] >> over.face() & 1 == 0
{
break;
}
n_suit += 1;
}
let mut n_same = 1;
while n_same < cards.len() {
let (under, over) =
(cards[cards.len() - n_same - 1], cards[cards.len() - n_same]);
if !under.is_face_up() || under.rank() != over.rank() {
break;
}
n_same += 1;
}
n_suit.max(n_same)
}
MoveUnit::Run
| MoveUnit::Supermove
| MoveUnit::SuitRun
| MoveUnit::AltColorRun
| MoveUnit::SameColorRun
| MoveUnit::TopOrWhole
if self.jokers > 0 =>
{
let filter = match g.moves {
MoveUnit::SuitRun => RunFilter::SameSuit,
MoveUnit::AltColorRun => RunFilter::AltColor,
MoveUnit::SameColorRun => RunFilter::SameColor,
_ => RunFilter::Any,
};
let n = self.movable_run_wild(g, cards, filter);
if g.moves == MoveUnit::TopOrWhole && n != cards.len() {
1
} else {
n
}
}
MoveUnit::Run
| MoveUnit::Supermove
| MoveUnit::SuitRun
| MoveUnit::AltColorRun
| MoveUnit::SameColorRun
| MoveUnit::TopOrWhole => {
let mut n = 1;
while n < cards.len() {
let (under, over) = (cards[cards.len() - n - 1], cards[cards.len() - n]);
let extra_ok = match g.moves {
MoveUnit::SuitRun => under.suit() == over.suit(),
MoveUnit::AltColorRun => under.color() != over.color(),
MoveUnit::SameColorRun => under.color() == over.color(),
_ => true,
};
if !under.is_face_up()
|| g.fits[under.face() as usize] >> over.face() & 1 == 0
|| !extra_ok
{
break;
}
n += 1;
}
if g.moves == MoveUnit::TopOrWhole && n != cards.len() {
1
} else {
n
}
}
};
n.min(cards.len().saturating_sub(g.min_cards as usize))
}
fn step_set_filtered(&self, r: &PileRules, set: u64, filter: RunFilter) -> u64 {
let mut out = 0u64;
let mut m = set & REAL_FACES;
while m != 0 {
let f = m.trailing_zeros() as usize;
let mut fit = r.fits[f];
match filter {
RunFilter::Any => {}
RunFilter::SameSuit => fit &= self.suit_faces[f / 13],
RunFilter::SameColor => {
let s = f / 13;
let mask = if s == 0 || s == 3 {
self.suit_faces[0] | self.suit_faces[3]
} else {
self.suit_faces[1] | self.suit_faces[2]
};
fit &= mask;
}
RunFilter::AltColor => {
let s = f / 13;
let mask = if s == 0 || s == 3 {
self.suit_faces[1] | self.suit_faces[2]
} else {
self.suit_faces[0] | self.suit_faces[3]
};
fit &= mask;
}
}
out |= fit;
m &= m - 1;
}
out & REAL_FACES
}
fn movable_run_wild(&self, r: &PileRules, cards: &[Card], filter: RunFilter) -> usize {
let len = cards.len();
if r.wild_permissive {
let mut n = 1;
while n < len {
let under = cards[len - n - 1];
let over = cards[len - n];
if !under.is_face_up() {
break;
}
if !under.is_joker()
&& !over.is_joker()
&& self.step_set_filtered(r, 1 << under.face(), filter) >> over.face() & 1 == 0
{
break;
}
n += 1;
}
return n;
}
let top = cards[len - 1];
let steps = |mut set: u64, k: usize| {
for _ in 0..k {
set = self.step_set_filtered(r, set, filter);
}
set
};
let any = r.admits & REAL_FACES;
let (mut b, mut j) = if top.is_joker() {
if !r.wild {
return 1;
}
(None, 1usize)
} else {
(Some(top.face()), 0usize)
};
let mut n = 1;
while n < len {
let under = cards[len - n - 1];
if !under.is_face_up() {
break;
}
let ok = if under.is_joker() {
r.wild
&& match b {
Some(bf) => steps(any, j + 1) >> bf & 1 != 0,
None => steps(any, j) != 0,
}
} else {
let a = 1u64 << under.face();
match b {
Some(bf) => steps(a, j + 1) >> bf & 1 != 0,
None => steps(a, j) != 0,
}
};
if !ok {
break;
}
if under.is_joker() {
j += 1;
} else {
b = Some(under.face());
j = 0;
}
n += 1;
}
n
}
fn supermove_limit(&self, s: &State, from: u16, to: u16) -> usize {
let fg = self.pile_group[from as usize];
let mut cells = 0usize;
let mut empties = 0u32;
for (p, pile) in s.piles.iter().enumerate() {
if !pile.is_empty() || p as u16 == to || p as u16 == from {
continue;
}
if self.group_of(p as u16).kind == PileKind::Cell {
cells += 1;
} else if self.pile_group[p] == fg && self.pile_rules[p].empty == EmptyRule::Any {
empties += 1;
}
}
(cells + 1) << empties.min(16)
}
pub(crate) fn is_built_from(&self, p: u16, cards: &[Card], from: u8) -> bool {
let fits = &self.rule(p).fits;
cards
.first()
.is_some_and(|c| c.is_face_up() && c.rank() == from)
&& cards
.windows(2)
.all(|w| w[1].is_face_up() && fits[w[0].face() as usize] >> w[1].face() & 1 != 0)
}
pub(crate) fn top_run_has_rank(&self, p: u16, cards: &[Card], rank: u8) -> bool {
let fits = &self.rule(p).fits;
let mut i = cards.len();
while i > 0 {
let c = cards[i - 1];
if !c.is_face_up()
|| (i < cards.len() && fits[c.face() as usize] >> cards[i].face() & 1 == 0)
{
return false;
}
if c.rank() == rank {
return true;
}
i -= 1;
}
false
}
fn is_complete_run(&self, cards: &[Card], n: usize, want: usize, build: &Build) -> bool {
let (by, wrap, step) = (build.by, build.wrap, build.step);
let same = build.dir == Dir::Same;
if n != want || cards.len() < n {
return false;
}
let run = &cards[cards.len() - n..];
let b = Build {
by,
dir: Dir::Down,
wrap,
step,
..Build::default()
};
let b_up = Build {
by,
dir: Dir::Up,
wrap,
step,
..Build::default()
};
let all = |b: &Build| {
run.windows(2)
.all(|w| w[0].is_face_up() && self.fits(b, w[0], w[1]))
};
let b_same = Build {
by,
dir: Dir::Same,
..Build::default()
};
run[0].is_face_up()
&& run[n - 1].is_face_up()
&& if same {
all(&b_same)
} else {
all(&b) || all(&b_up)
}
}
pub(crate) fn can_swap(&self, s: &State, from: u16, to: u16) -> bool {
if from == to {
return false;
}
let fgi = self.pile_group[from as usize];
if let Some(adj) = self.groups[fgi as usize].swap_on_waste_match {
return self.waste_match_swap(s, from, to, adj);
}
let Some(mode) = self.groups[fgi as usize].swap_bases else {
return false;
};
if fgi != self.pile_group[to as usize]
&& !(mode == SwapBases::Crossing
&& self.groups[self.pile_group[to as usize] as usize].swap_bases == Some(mode))
{
return false;
}
let src = &s.piles[from as usize];
let dst = &s.piles[to as usize];
if src.is_empty() || dst.is_empty() {
return false;
}
let c0 = src[0];
let c1 = dst[0];
if !c0.is_face_up() || !c1.is_face_up() {
return false;
}
match mode {
SwapBases::BothSingle => {
src.len() == 1
&& dst.len() == 1
&& self.empty_accepts(to, c0, s)
&& self.empty_accepts(from, c1, s)
}
SwapBases::Crossing => {
let (af, at) = (self.rule(from).admits, self.rule(to).admits);
src.len() == 1
&& dst.len() == 1
&& af != at
&& at >> c0.face() & 1 != 0
&& af >> c1.face() & 1 != 0
&& at >> c1.face() & 1 == 0
}
SwapBases::EitherPile => {
self.rule(from).empty != self.rule(to).empty
&& c0.rank() != c1.rank()
&& (self.empty_accepts(to, c0, s) || self.empty_accepts(from, c1, s))
}
}
}
fn waste_match_swap(&self, s: &State, from: u16, to: u16, adj: Adjacency) -> bool {
let g = &self.groups[self.pile_group[from as usize] as usize];
if self.pile_group[to as usize] != self.pile_group[from as usize] {
return false;
}
let (Some(w), Some(_)) = (self.waste, self.stock.as_ref()) else {
return false;
};
let (src, dst) = (&s.piles[from as usize], &s.piles[to as usize]);
let (&[c0], &[c1]) = (src.as_slice(), dst.as_slice()) else {
return false;
};
let Some(&top) = s.piles[w as usize].last() else {
return false;
};
if !c0.is_face_up() || !c1.is_face_up() || !top.is_face_up() || top.face() != c0.face() {
return false;
}
let cols = g.columns.unwrap() as i32;
let pos = |p: u16| {
let i = (p - g.start) as i32;
(i / cols, i % cols)
};
let ((r0, k0), (r1, k1)) = (pos(from), pos(to));
let (dr, dc) = ((r0 - r1).abs(), (k0 - k1).abs());
match adj {
Adjacency::Eight => dr.max(dc) == 1,
Adjacency::Four => dr + dc == 1,
Adjacency::SameLine => dr == 0 || dc == 0,
}
}
pub(super) fn gen_waste_swaps(&self, s: &State, only: Option<u16>, out: &mut Vec<Move>) {
for g in &self.groups {
let Some(adj) = g.swap_on_waste_match else {
continue;
};
for from in g.piles() {
if only.is_some_and(|o| o != from) {
continue;
}
for to in g.piles() {
if to != from && self.waste_match_swap(s, from, to, adj) {
out.push(Move::new(Action::Transfer { from, to, count: 1 }));
}
}
}
}
}
pub(crate) fn repeat_ok(&self, pile: &[Card], top: Card, card: Card, n: u16) -> bool {
let run = pile
.iter()
.rev()
.take_while(|c| c.rank() == top.rank())
.count();
if run < n as usize {
card.rank() == top.rank()
} else {
card.rank() == self.rank_from(top.rank(), 1)
}
}
pub(crate) fn pile_map_allows(
&self,
s: &State,
tg: &crate::game::Group,
from: u16,
to: u16,
card: Card,
) -> bool {
let Some(m) = &tg.from_pile_map else {
return true;
};
if self.pile_group[from as usize] != m.group {
return true;
}
if !card.is_joker() && m.except_ranks.contains(&card.rank()) {
return true;
}
if m.only_when_stock_non_empty
&& self
.stock_pile
.is_none_or(|sp| s.piles[sp as usize].is_empty())
{
return true;
}
if m.only_non_empty_dest && s.piles[to as usize].is_empty() {
return true;
}
let want =
m.to.get((from - self.groups[m.group as usize].start) as usize)
.copied()
.unwrap_or(-1);
want < 0 || want as u16 == to - tg.start
}
pub(crate) fn find_push_off_target(&self, s: &State, to: u16) -> Option<u16> {
let tg = self.group_of(to);
if tg.push_off_to.is_empty() {
return None;
}
if !s.piles[to as usize].last().is_some_and(|c| c.is_face_up()) {
return None;
}
for &dest_gi in &tg.push_off_to {
for dest in self.groups[dest_gi as usize].piles() {
if dest != to
&& self.can_place_impl(s, to, dest, 1, None, tg.push_off_bypasses_accept)
{
return Some(dest);
}
}
}
None
}
pub(crate) fn grid_hole_fill_targets(&self, s: &State, st: &super::Stock) -> Vec<u16> {
let stock_len = s.piles[st.pile as usize].len();
if stock_len == 0 {
return Vec::new();
}
let tg = &self.groups[st.target as usize];
let cols = tg.columns.unwrap_or(tg.count);
let rows = tg.count / cols;
if cols == 0 || rows == 0 {
return Vec::new();
}
if st.fill_grid_holes_repeat {
return self.grid_hole_fill_targets_repeat(s, tg, cols, stock_len);
}
let cell = |r: u16, c: u16| tg.start + r * cols + c;
let mut heights = Vec::with_capacity(cols as usize);
for c in 0..cols {
let h = (0..rows)
.rev()
.find(|&r| !s.piles[cell(r, c) as usize].is_empty())
.map_or(0, |r| r + 1);
heights.push(h);
}
let max_h = heights.iter().copied().max().unwrap_or(0);
if max_h == 0
|| (heights.iter().all(|&h| h == max_h)
&& (0..cols).all(|c| !s.piles[cell(0, c) as usize].is_empty())
&& max_h < rows)
{
return (0..cols).take(stock_len).map(|c| cell(max_h, c)).collect();
}
let mut out = Vec::new();
let mut rem = stock_len;
let mut phase1_row = vec![None::<u16>; cols as usize];
if max_h >= 2 {
for c in 0..cols {
if rem == 0 {
break;
}
if let Some(r) = (0..max_h - 1).rev().find(|&r| {
s.piles[cell(r, c) as usize].is_empty()
&& !s.piles[cell(r + 1, c) as usize].is_empty()
}) {
out.push(cell(r, c));
phase1_row[c as usize] = Some(r);
rem -= 1;
}
}
}
for c in 0..cols {
if rem == 0 {
break;
}
if let Some(r) = (0..max_h).find(|&r| {
phase1_row[c as usize] != Some(r) && s.piles[cell(r, c) as usize].is_empty()
}) {
out.push(cell(r, c));
rem -= 1;
}
}
out
}
fn grid_hole_fill_targets_repeat(
&self,
s: &State,
tg: &crate::game::Group,
cols: u16,
stock_len: usize,
) -> Vec<u16> {
let rows = tg.count / cols;
let cell = |r: u16, c: u16| tg.start + r * cols + c;
let mut filled = vec![false; (rows * cols) as usize];
for r in 0..rows {
for c in 0..cols {
filled[(r * cols + c) as usize] = !s.piles[cell(r, c) as usize].is_empty();
}
}
let counts: Vec<u16> = (0..cols)
.map(|c| {
(0..rows)
.filter(|&r| filled[(r * cols + c) as usize])
.count() as u16
})
.collect();
let max_col = counts.iter().copied().max().unwrap_or(0);
let top_full = (0..cols).all(|c| filled[c as usize]);
if top_full && counts.iter().all(|&n| n == max_col) {
if max_col >= rows {
return Vec::new();
}
return (0..cols)
.take(stock_len)
.map(|c| cell(max_col, c))
.collect();
}
let depth = max_col.max(1).min(rows);
let mut rem = stock_len;
let mut out = Vec::new();
loop {
let mut any = false;
for c in 0..cols {
if rem == 0 {
break;
}
if let Some(top) = (0..depth).find(|&r| filled[(r * cols + c) as usize]) {
if top > 0 {
filled[((top - 1) * cols + c) as usize] = true;
out.push(cell(top - 1, c));
rem -= 1;
any = true;
}
}
}
if !any || rem == 0 {
break;
}
}
loop {
let mut any = false;
for c in 0..cols {
if rem == 0 {
break;
}
if let Some(r) = (0..depth).find(|&r| !filled[(r * cols + c) as usize]) {
filled[(r * cols + c) as usize] = true;
out.push(cell(r, c));
rem -= 1;
any = true;
}
}
if !any || rem == 0 {
break;
}
}
out
}
pub(crate) fn piles_full_ok(&self, s: &State, tg: &crate::game::Group) -> bool {
let Some((rg, piles)) = &tg.accept_when_piles_full else {
return true;
};
let full = |p: u16| {
s.piles[p as usize].len() >= (self.rule(p).max_cards as usize).min(self.nranks as usize)
};
let g = &self.groups[*rg as usize];
if piles.is_empty() {
g.piles().all(full)
} else {
piles.iter().all(|&i| full(g.start + i))
}
}
pub(crate) fn pile_sets_allows(&self, tg: &crate::game::Group, from: u16, to: u16) -> bool {
match &tg.from_pile_sets {
Some((fg, to_sets)) if self.pile_group[from as usize] == *fg => {
let src_idx = (from - self.groups[*fg as usize].start) as usize;
let dst_idx = to - tg.start;
to_sets
.get(src_idx)
.is_some_and(|dsts| dsts.contains(&dst_idx))
}
_ => true,
}
}
fn cyclic_fits(&self, g: &crate::game::Group, pile: u16, rank: u8, s: &State) -> bool {
let n = self.nranks as i16;
let mut idx: Vec<i16> = g
.piles()
.filter(|&p| p != pile)
.filter_map(|p| s.piles[p as usize].first())
.filter(|c| !c.is_joker())
.map(|c| self.rank_idx[c.rank() as usize] as i16)
.collect();
idx.push(self.rank_idx[rank as usize] as i16);
idx.sort_unstable();
let k = idx.len() as i16;
if idx.windows(2).any(|w| w[0] == w[1]) {
return false;
}
if k <= 1 {
return true;
}
let mut max_gap = idx[0] + n - idx[k as usize - 1];
for w in idx.windows(2) {
max_gap = max_gap.max(w[1] - w[0]);
}
max_gap == n - k + 1
}
fn even_segments_ok(
&self,
s: &State,
tg: &crate::game::Group,
to: u16,
card: Card,
sep: u8,
) -> bool {
if card.is_joker() {
return false;
}
let cols = tg.columns.expect("even_segments validated grid layout") as usize;
let rows = tg.count as usize / cols;
let base = tg.start as usize;
let idx = (to - tg.start) as usize;
let (r, c) = (idx / cols, idx % cols);
let mut has_neighbor = false;
'nbr: for dr in -1isize..=1 {
let nr = r as isize + dr;
if nr < 0 || nr >= rows as isize {
continue;
}
for dc in -1isize..=1 {
if dr == 0 && dc == 0 {
continue;
}
let nc = c as isize + dc;
if nc < 0 || nc >= cols as isize {
continue;
}
if !s.piles[base + nr as usize * cols + nc as usize].is_empty() {
has_neighbor = true;
break 'nbr;
}
}
}
if !has_neighbor {
return false;
}
let check_line = |len: usize, at: usize, pile_at: &dyn Fn(usize) -> usize| -> bool {
let mut sum: u16 = 0;
let mut has_empty = false;
let mut last_sep = false;
for i in 0..len {
let rank = if i == at {
Some(card.rank())
} else {
s.piles[pile_at(i)].first().map(|x| x.rank())
};
match rank {
None => {
has_empty = true;
last_sep = false;
}
Some(rk) if rk < sep => {
sum += u16::from(rk);
last_sep = false;
}
Some(_) => {
if last_sep || (sum % 2 != 0 && !has_empty) {
return false;
}
sum = 0;
has_empty = false;
last_sep = true;
}
}
}
!(sum % 2 != 0 && !has_empty)
};
check_line(cols, c, &|j| base + r * cols + j)
&& check_line(rows, r, &|i| base + i * cols + c)
}
pub(crate) fn can_place(&self, s: &State, from: u16, to: u16, n: usize) -> bool {
self.can_place_impl(s, from, to, n, None, false)
}
fn can_place_with_limit(
&self,
s: &State,
from: u16,
to: u16,
n: usize,
supermove_cap: Option<usize>,
) -> bool {
self.can_place_impl(s, from, to, n, supermove_cap, false)
}
fn can_place_impl(
&self,
s: &State,
from: u16,
to: u16,
n: usize,
supermove_cap: Option<usize>,
bypass_accept: bool,
) -> bool {
if from == to {
return false;
}
if let Some(h) = &self.hooks {
let ok_shape = n > 0
&& n <= s.piles[from as usize].len()
&& self.available(s, to)
&& self.group_of(to).kind != PileKind::Stock;
if ok_shape {
if let Some(b) = h.accepts(self, s, from, to, n) {
return b;
}
}
}
let tg = self.group_of(to);
let tr = self.rule(to);
let fgi = self.pile_group[from as usize];
if !self.aux_allows(s, AuxCtx::Transfer { from, to, n }) {
return false;
}
if let Some(cb) = &self.choose_base {
if s.base_rank.is_none()
&& ((fgi != cb.from_group && Some(fgi) != cb.also_from_group)
|| tg.kind != PileKind::Foundation
|| n != 1)
&& (!cb.free_moves || tg.kind == PileKind::Foundation)
{
return false;
}
}
let dst = &s.piles[to as usize];
if matches!(tg.kind, PileKind::Stock)
&& tr.empty == EmptyRule::None
&& tr.build.by == BuildBy::None
{
return false;
}
if tg.accept_first_round_only && s.redeals_used > 0 {
return false;
}
if !self.piles_full_ok(s, tg) {
return false;
}
if tg.min_accept_len.is_some_and(|n| dst.len() < n as usize) {
return false;
}
if let Some(sl) = &tg.sum_limit {
let src = &s.piles[from as usize];
if n > src.len() || !sum_within(sl, dst, &src[src.len() - n..], tr.max_cards) {
return false;
}
}
if self.can_swap(s, from, to) {
return n == 1 && self.available(s, to);
}
let unrestricted = tg.unrestricted_from & (1 << fgi) != 0;
let is_final_same_index =
tg.final_from_same_index.is_some() && dst.len() + n == tr.max_cards as usize;
if let Some(ref_gi) = tg.final_from_same_index.filter(|_| is_final_same_index) {
let expected_from = self.groups[ref_gi as usize].start + (to - tg.start);
if from != expected_from {
return false;
}
} else {
let mask = if dst.is_empty() {
tg.empty_accepts_from
} else {
tg.accepts_from
};
if !bypass_accept && (mask | tg.unrestricted_from) & (1 << fgi) == 0 {
return false;
}
}
if let Some(reach) = &tg.reach {
let same = self.pile_group[to as usize] as usize == fgi as usize;
if !same {
return false;
}
let dist = if tg.reach_both_ways {
from.abs_diff(to)
} else if from >= to {
from - to
} else {
return false;
};
if !reach.contains(&dist) {
return false;
}
if tg.reach_grid && dist == 1 {
let cols = tg.columns.unwrap_or(tg.count);
if (from - tg.start) / cols != (to - tg.start) / cols {
return false;
}
}
}
let src = &s.piles[from as usize];
if tg.accept_only_when_empty && !dst.is_empty() {
return false;
}
if s.redeals_used == 0
&& tg.first_round_bans.iter().any(|&(t, fg2, f)| {
to - tg.start == t
&& self.pile_group[from as usize] == fg2
&& from - self.groups[fg2 as usize].start == f
})
{
return false;
}
if dst.is_empty() && self.waste == Some(from) {
if let Some(gi) = tg.empty_needs_group_empty {
if self.groups[gi as usize]
.piles()
.any(|q| !s.piles[q as usize].is_empty())
{
return false;
}
}
}
if tg.run_must_fit_build
&& n > 1
&& src[src.len() - n..]
.windows(2)
.any(|w| !w[0].is_joker() && !w[1].is_joker() && !self.fits(&tr.build, w[0], w[1]))
{
return false;
}
if tg.accept_whole_from_group
&& self.pile_group[from as usize] == self.pile_group[to as usize]
&& n != src.len()
{
return false;
}
if tg.from_requires_left_empty {
let fg = &self.groups[fgi as usize];
if let Some(cols) = fg.columns {
let local_p = from - fg.start;
if local_p % cols == 0 || !s.piles[from as usize - 1].is_empty() {
return false;
}
}
}
if tg.grid_bottom_open {
let cols = tg.columns.unwrap_or(tg.count);
let i = to - tg.start;
if dst.is_empty() || (i >= cols && !s.piles[(to - cols) as usize].is_empty()) {
return false;
}
}
if let Some(k) = tg.one_way_split {
if self.pile_group[from as usize] == self.pile_group[to as usize]
&& to - tg.start >= k
&& from - tg.start < k
{
return false;
}
}
if let Some(stg) = &tg.staging {
if n != 1 || !stg.accepts(dst, src[src.len() - 1]) {
return false;
}
}
if tg.require_valid_base && !dst.is_empty() && !self.empty_accepts(to, dst[0], s) {
return false;
}
if tg.accept_when_waste_empty && self.waste.is_some_and(|w| !s.piles[w as usize].is_empty())
{
return false;
}
if tg.accept_when_stock_empty
&& self
.stock_pile
.is_some_and(|sp| !s.piles[sp as usize].is_empty())
{
return false;
}
if let Some(r) = &self.rounds {
if self.pile_group[to as usize] == r.crib {
if let Some(i) = r.hands.iter().position(|&h| h == fgi) {
if r.hands[i + 1..]
.iter()
.any(|&h| !s.piles[self.groups[h as usize].start as usize].is_empty())
{
return false;
}
}
}
}
if tg.no_build_single_while_stock
&& !dst.is_empty()
&& self
.stock_pile
.is_some_and(|sp| !s.piles[sp as usize].is_empty())
&& (dst.len() == 1 || (fgi == self.pile_group[to as usize] && src.len() == 1))
{
return false;
}
if n == 0
|| n > src.len()
|| n < tr.min_accept as usize
|| (!bypass_accept
&& (n > tr.max_accept as usize
|| (dst.is_empty() && n > tr.empty_max_accept as usize)
|| (!dst.is_empty() && n > tr.non_empty_max_accept as usize)))
|| (!dst.is_empty() && !tg.push_off_to.is_empty() && n != 1)
|| (if tg.replace_on_drop.is_some() {
0
} else if !dst.is_empty() && !tg.push_off_to.is_empty() {
dst.len().saturating_sub(1)
} else {
dst.len()
}) + n
> tr.max_cards as usize
{
return false;
}
if !self.available(s, to) && !self.dropdown_ok(s, from, to, n) {
return false;
}
let card = src[src.len() - n];
if !self.pile_map_allows(s, tg, from, to, card) {
return false;
}
if !self.pile_sets_allows(tg, from, to) {
return false;
}
if tr.complete_runs {
let want = tg.run_len.map_or(self.nranks as usize, |l| l as usize);
return (dst.is_empty() || tg.run_len.is_some())
&& self.is_complete_run(src, n, want, &tr.build);
}
let ok = if let Some(ts) = &tg.tiered_set {
n == 1 && ts.accepts(dst, card)
} else if unrestricted
|| (tg.accept_on_face_down && dst.last().is_some_and(|c| !c.is_face_up()))
|| (tg.accept_any_on_single
&& dst.len() == 1
&& n == 1
&& dst[0].is_face_up()
&& (tr.admits >> card.face() & 1 != 0))
{
true
} else if tr.joker_anywhere && card.is_joker() {
tr.admits >> JOKER & 1 != 0
} else if !tg.push_off_to.is_empty() && !dst.is_empty() {
n == 1
&& (tr.admits >> card.face() & 1 != 0)
&& self.find_push_off_target(s, to).is_some()
} else if let Some(n) = tg.repeat_rank {
match dst.last() {
None => self.empty_accepts(to, card, s),
Some(&top) => top.is_face_up() && self.repeat_ok(dst, top, card, n),
}
} else if !tg.rank_sequence.is_empty() {
let Some(&want_rank) = tg.rank_sequence.get(dst.len()) else {
return false;
};
let ok_admit = tr.admits >> card.face() & 1 != 0;
let ok_rank = card.is_joker() || card.rank() == want_rank;
if !ok_admit || !ok_rank {
false
} else if let Some(&top) = dst.last() {
if !top.is_face_up() {
false
} else if card.is_joker() || top.is_joker() {
tr.wild
} else {
match tr.build.by {
BuildBy::None => false,
BuildBy::Any => true,
BuildBy::Suit => top.suit() == card.suit(),
BuildBy::Alternate => top.color() != card.color(),
BuildBy::SameColor => top.color() == card.color(),
BuildBy::OtherSuit => top.suit() != card.suit(),
BuildBy::SuitOrRank | BuildBy::SuitOrSameRank => {
top.suit() == card.suit() || top.rank() == card.rank()
}
BuildBy::RankSuitColor | BuildBy::SuitChain => {
self.fits(&tr.build, top, card)
}
BuildBy::SuitCycle => {
follows_in_cycle(&tr.build.suit_cycle, top.suit(), card.suit())
}
}
}
} else {
card.is_joker() || tr.empty_suit.is_none_or(|es| card.suit() == es)
}
} else if self.jokers == 0 {
match dst.last() {
None => self.empty_accepts(to, card, s),
Some(&top) => {
top.is_face_up() && tr.fits[top.face() as usize] >> card.face() & 1 != 0
}
}
} else if tr.wild_permissive && !dst.is_empty() {
let top = *dst.last().unwrap();
if !top.is_face_up() {
false
} else if top.is_joker() {
tr.admits >> card.face() & 1 != 0
} else if card.is_joker() {
tr.admits >> JOKER & 1 != 0
} else {
tr.fits[top.face() as usize] >> card.face() & 1 != 0
}
} else {
let base = if dst.iter().all(|c| c.is_joker()) {
self.empty_set(to, s)
} else {
0
};
let allowed = self.accept_set(tr, dst, base, false);
let run = is_run_unit(self.rule(from).moves);
let empty_any = dst.is_empty() && tr.empty == EmptyRule::Any;
self.unit_fits(tr, allowed, empty_any, &src[src.len() - n..], run)
};
if !ok {
return false;
}
if (tg.lock_build_dir || tg.lock_pile_dir)
&& !unrestricted
&& !dst.is_empty()
&& !card.is_joker()
{
let n = self.nranks as i16;
let step = tr.build.step as i16;
let is_up = |a: Card, b: Card| -> bool {
let d = self.rank_idx[b.rank() as usize] as i16
- self.rank_idx[a.rank() as usize] as i16;
d == step || (tr.build.wrap && d == step - n)
};
let locked = if tg.lock_build_dir {
tg.piles().find_map(|p| {
let pile = &s.piles[p as usize];
if pile.len() >= 2 && !pile[0].is_joker() && !pile[1].is_joker() {
Some(is_up(pile[0], pile[1]))
} else {
None
}
})
} else if dst.len() >= 2 && !dst[0].is_joker() && !dst[1].is_joker() {
Some(is_up(dst[0], dst[1]))
} else {
None
};
if let Some(want_up) = locked {
let top = *dst.last().unwrap();
if !top.is_joker() && is_up(top, card) != want_up {
return false;
}
}
}
if let Some(ref_gi) = tg.requires_same_index_non_empty {
let ref_pile = self.groups[ref_gi as usize].start + (to - tg.start);
if s.piles[ref_pile as usize].is_empty() {
return false;
}
}
for &ref_gi in &tg.requires_same_index_groups_non_empty {
let ref_g = &self.groups[ref_gi as usize];
let ref_pile = ref_g.start + ((to - tg.start) % ref_g.count);
if s.piles[ref_pile as usize].is_empty() {
return false;
}
}
if let Some(ref_gi) = tg.accept_when_same_index_empty {
let ref_pile = self.groups[ref_gi as usize].start + (to - tg.start);
if from != ref_pile && !s.piles[ref_pile as usize].is_empty() {
return false;
}
}
if let Some(ref_gi) = tg.empty_requires_same_index_non_empty {
if dst.is_empty() {
let ref_g = &self.groups[ref_gi as usize];
let ref_pile = ref_g.start + ((to - tg.start) % ref_g.count);
if s.piles[ref_pile as usize].is_empty() {
return false;
}
}
}
if let Some(ref_gi) = tg.empty_match_suit_of {
if dst.is_empty() {
let ref_g = &self.groups[ref_gi as usize];
let ref_pile = ref_g.start + ((to - tg.start) % ref_g.count);
let Some(&ref_bottom) = s.piles[ref_pile as usize].first() else {
return false;
};
if !card.is_joker() && !ref_bottom.is_joker() && card.suit() != ref_bottom.suit() {
return false;
}
}
}
if tg.requires_prev_non_empty
&& dst.is_empty()
&& to > tg.start
&& s.piles[to as usize - 1].is_empty()
{
return false;
}
if let Some((stride, src_gi)) = tg.staged_empty {
if dst.is_empty() {
if card.is_joker() {
return false;
}
let idx = to - tg.start;
if idx > 0 && s.piles[to as usize - 1].is_empty() {
return false;
}
if idx >= stride {
let prev_suit = s.piles[(to - stride) as usize]
.first()
.filter(|c| !c.is_joker())
.map(|c| c.suit());
if prev_suit != Some(card.suit()) {
return false;
}
} else {
for k in 0..stride.min(tg.count) {
if let Some(&b) = s.piles[(tg.start + k) as usize].first() {
if !b.is_joker() && b.suit() == card.suit() {
return false;
}
}
}
}
for p in self.groups[src_gi as usize].piles() {
if let Some(&top) = s.piles[p as usize].last() {
if top.is_face_up()
&& !top.is_joker()
&& top.suit() == card.suit()
&& top.rank() < card.rank()
{
return false;
}
}
}
for p in tg.piles() {
let pile = &s.piles[p as usize];
if let Some(&top) = pile.last() {
if top.is_face_up()
&& pile.len() < self.rule(p).max_cards as usize
&& self.rule(p).fits[top.face() as usize] >> card.face() & 1 != 0
{
return false;
}
}
}
}
}
if let Some(sep) = tg.even_segments {
if dst.is_empty() && !self.even_segments_ok(s, tg, to, card, sep) {
return false;
}
}
if let Some(limit) = tg.max_same_rank {
if !card.is_joker() {
let r = card.rank();
let existing = dst
.iter()
.rev()
.take_while(|c| c.is_face_up() && !c.is_joker() && c.rank() == r)
.count();
let incoming = src[src.len() - n..]
.iter()
.take_while(|c| !c.is_joker() && c.rank() == r)
.count();
if existing + incoming > limit as usize {
return false;
}
}
}
if tg.requires_rank_on_prev && to > tg.start {
let prev = &s.piles[to as usize - 1];
if prev.is_empty() {
return false;
}
if !card.is_joker() {
let rank_ok = if dst.is_empty() {
prev[0].is_joker() || prev[0].rank() == card.rank()
} else {
prev.iter().any(|c| c.is_joker() || c.rank() == card.rank())
};
if !rank_ok {
return false;
}
}
}
if tg.match_longest_rank && !card.is_joker() {
let mut max_p = None;
let mut max_len = 0usize;
for p in tg.piles() {
let len = s.piles[p as usize].len();
if len > max_len {
max_len = len;
max_p = Some(p);
}
}
if dst.len() < max_len {
let ref_card = s.piles[max_p.unwrap() as usize][dst.len()];
if !ref_card.is_joker() && ref_card.rank() != card.rank() {
return false;
}
}
}
if tg.match_depth_suit {
let base_k = dst.len();
for (offset, &mc) in src[src.len() - n..].iter().enumerate() {
if mc.is_joker() {
continue;
}
let k = base_k + offset;
for p in tg.piles() {
if p == to {
continue;
}
if let Some(&ref_card) = s.piles[p as usize].get(k) {
if !ref_card.is_joker() && ref_card.suit() != mc.suit() {
return false;
}
}
}
}
}
if self.rule(from).moves == MoveUnit::Supermove
&& n > 1
&& !(self.groups[fgi as usize].supermove_complete_run && n == self.nranks as usize)
{
let cap = supermove_cap.unwrap_or_else(|| self.supermove_limit(s, from, to));
return n <= cap;
}
true
}
#[inline]
pub(crate) fn is_group_symmetric(&self, gi: usize, s: &State) -> bool {
let g = &self.groups[gi];
if g.symmetric {
return true;
}
if g.symmetric_when_no_stock {
if let Some(st) = &self.stock {
if s.piles[st.pile as usize].is_empty()
&& st.redeals != u32::MAX
&& s.redeals_used >= st.redeals
{
return true;
}
}
}
if g.symmetric_when_no_redeal {
if let Some(rd) = &self.redeal {
if rd.times != u32::MAX && s.redeals_used >= rd.times {
return true;
}
}
}
false
}
pub(crate) fn compute_first_empty(&self, s: &State) -> ([u16; 64], [u16; 64]) {
let mut first_empty = [u16::MAX; 64];
let mut class_empty = [u16::MAX; 64];
for (gi, g) in self.groups.iter().enumerate() {
if self.is_group_symmetric(gi, s) {
if let Some(p) = g.piles().find(|&p| s.piles[p as usize].is_empty()) {
first_empty[gi] = p;
}
} else if !g.sym_classes.is_empty() {
for cls in &g.sym_classes {
if cls.len() > 1 {
let cid = self.pile_sym_class[cls[0] as usize];
if cid != u16::MAX {
if let Some(&p) = cls.iter().find(|&&p| s.piles[p as usize].is_empty())
{
class_empty[cid as usize] = p;
}
}
}
}
}
}
(first_empty, class_empty)
}
pub(crate) fn gen_actions(&self, s: &State, only: Option<u16>, out: &mut Vec<Move>) {
if let Some(p) = only {
if s.piles[p as usize].is_empty() {
return;
}
}
let first_empty = self.compute_first_empty(s);
self.gen_actions_with_empty(s, only, &first_empty, out);
}
pub(crate) fn gen_actions_with_empty(
&self,
s: &State,
only: Option<u16>,
first_empty: &([u16; 64], [u16; 64]),
out: &mut Vec<Move>,
) {
let npiles = s.piles.len() as u16;
let (empty_cells, empty_any_group) = if self.has_supermove {
let mut cells = 0usize;
let mut by_group = [0u16; 64];
for (p, pile) in s.piles.iter().enumerate() {
if !pile.is_empty() {
continue;
}
let gi = self.pile_group[p] as usize;
if self.groups[gi].kind == PileKind::Cell {
cells += 1;
} else if self.pile_rules[p].empty == EmptyRule::Any {
by_group[gi] += 1;
}
}
(cells, by_group)
} else {
(0, [0u16; 64])
};
let no_hooks = self.hooks.is_none();
let mut single = [0u16; 1];
let sources: &[u16] = match only {
Some(p) => {
single[0] = p;
&single
}
None if no_hooks => &self.movable_piles,
None => &[],
};
let all_piles_iter = if only.is_none() && !no_hooks {
0..npiles
} else {
0..0
};
let use_active = only.is_none() && no_hooks && self.movable_piles.len() >= 4;
let mut active_stack = [(0u16, 0u64, 0u64, 0u64); 64];
let mut active_len = 0usize;
let mut active_heap: Vec<(u16, u64, u64, u64)> = Vec::new();
let mut union_accept_mask = 0u64;
if use_active {
if npiles as usize > active_stack.len() {
active_heap.reserve(npiles as usize);
}
for to in 0..npiles {
let tgi = self.pile_group[to as usize] as usize;
let tg = &self.groups[tgi];
let tr = self.rule(to);
if tg.kind == PileKind::Stock
&& tr.empty == EmptyRule::None
&& tr.build.by == BuildBy::None
{
continue;
}
let dst = &s.piles[to as usize];
if (tg.accept_first_round_only && s.redeals_used > 0)
|| !self.piles_full_ok(s, tg)
|| tg.min_accept_len.is_some_and(|n| dst.len() < n as usize)
{
continue;
}
let dst_empty = dst.is_empty();
if dst_empty {
if self.is_group_symmetric(tgi, s) {
if first_empty.0[tgi] != to {
continue;
}
} else {
let cid = self.pile_sym_class[to as usize];
if cid != u16::MAX && first_empty.1[cid as usize] != to {
continue;
}
}
if !tr.complete_runs
&& tg.unrestricted_from == 0
&& tg.rank_sequence.is_empty()
&& tr.empty == EmptyRule::None
&& !(tr.joker_anywhere && self.jokers > 0)
&& tg.final_from_same_index.is_none()
{
continue;
}
} else {
if tg.grid_bottom_open {
let cols = tg.columns.unwrap_or(tg.count);
let i = to - tg.start;
if i >= cols && !s.piles[(to - cols) as usize].is_empty() {
continue;
}
}
if tg.replace_on_drop.is_none()
&& tg.swap_bases.is_none()
&& tg.push_off_to.is_empty()
&& dst.len() >= tr.max_cards as usize
{
continue;
}
if tg.accept_only_when_empty && tg.swap_bases.is_none() {
continue;
}
if !tr.complete_runs
&& tg.unrestricted_from == 0
&& tg.rank_sequence.is_empty()
&& !(tg.accept_any_on_single && dst.len() == 1)
&& !((tr.wild_permissive || tr.joker_anywhere) && self.jokers > 0)
&& tr.build.by == BuildBy::None
&& tg.swap_bases.is_none()
&& tg.push_off_to.is_empty()
&& tg.final_from_same_index.is_none()
{
continue;
}
}
if tg.grid_bottom_open && dst_empty {
continue;
}
if !self.available(s, to) && !tg.dropdown {
continue;
}
let swap_possible = match tg.swap_bases {
None => false,
Some(SwapBases::BothSingle) => dst.len() == 1,
Some(SwapBases::EitherPile) => !dst_empty,
Some(SwapBases::Crossing) => dst.len() == 1,
};
let mut from_mask = if dst_empty {
tg.empty_accepts_from
} else {
tg.accepts_from
} | tg.unrestricted_from;
if swap_possible {
from_mask |= 1u64 << tgi;
if tg.swap_bases == Some(SwapBases::Crossing) {
for (gi, og) in self.groups.iter().enumerate() {
if og.swap_bases == Some(SwapBases::Crossing) {
from_mask |= 1u64 << gi;
}
}
}
}
if let Some(ref_gi) = tg.final_from_same_index {
from_mask |= 1u64 << ref_gi;
}
if tg.reach.is_some() {
from_mask &= 1u64 << tgi;
}
if from_mask == 0 {
continue;
}
let accept_mask = if !dst_empty && !tg.push_off_to.is_empty() {
if self.find_push_off_target(s, to).is_none() {
continue;
}
tr.admits
} else if self.jokers > 0
|| tr.complete_runs
|| tg.unrestricted_from != 0
|| !tg.rank_sequence.is_empty()
|| tg.final_from_same_index.is_some()
{
u64::MAX
} else if tg.accept_any_on_single && dst.len() == 1 {
if dst[0].is_face_up() {
tr.admits
} else {
continue;
}
} else if dst_empty {
let m = self.empty_set(to, s);
if m == 0 {
continue;
}
m
} else {
let top = *dst.last().unwrap();
if !top.is_face_up() {
continue;
}
let m = tr.fits[top.face() as usize];
if m == 0 && !swap_possible {
continue;
}
m
};
let filter_mask = if swap_possible {
let swap_mask = match tg.swap_bases {
Some(SwapBases::BothSingle) => self.empty_set(to, s),
Some(SwapBases::Crossing) => tr.admits,
Some(SwapBases::EitherPile) => u64::MAX,
None => 0,
};
accept_mask | swap_mask
} else {
accept_mask
};
if filter_mask == 0 {
continue;
}
union_accept_mask |= filter_mask;
if active_len < active_stack.len() && active_heap.is_empty() {
active_stack[active_len] = (to, accept_mask, from_mask, filter_mask);
active_len += 1;
} else {
if active_heap.is_empty() {
active_heap.extend_from_slice(&active_stack[..active_len]);
}
active_heap.push((to, accept_mask, from_mask, filter_mask));
}
}
if active_len == 0 && active_heap.is_empty() {
if !self.removal.is_empty() {
self.gen_removals(s, only, out);
}
if self.any_secondary_stock {
self.gen_secondary_deals(s, only, out);
}
if self.extract.is_some() {
self.gen_extracts(s, only, out);
}
self.gen_picks(s, only, out);
self.gen_waste_swaps(s, only, out);
return;
}
}
let active_targets: &[(u16, u64, u64, u64)] = if active_heap.is_empty() {
&active_stack[..active_len]
} else {
&active_heap
};
let mut emit_from = |from: u16| {
if self.has_covers && !self.available(s, from) {
return;
}
let unit = self.movable(s, from);
if unit == 0 {
return;
}
let fg = self.pile_group[from as usize];
let src = &s.piles[from as usize];
let src_len = src.len();
let src_mask = if use_active && self.jokers == 0 {
let mut m = 0u64;
for &c in &src[src_len - unit..] {
m |= 1u64 << c.face();
}
if (union_accept_mask & m) == 0 {
return;
}
m
} else {
u64::MAX
};
let from_rule = self.rule(from);
let from_kind = self.groups[fg as usize].kind;
let from_moves = from_rule.moves;
let targets: &[u16] = if no_hooks && !use_active {
&self.targets_for_group[fg as usize]
} else {
&[]
};
let target_count = if use_active {
active_targets.len()
} else if no_hooks {
targets.len()
} else {
npiles as usize
};
#[allow(clippy::needless_range_loop)]
for ti in 0..target_count {
let (to, pre_accept_mask) = if use_active {
let (t, am, fm, flt) = active_targets[ti];
if (fm & (1u64 << fg)) == 0 || (self.jokers == 0 && (flt & src_mask) == 0) {
continue;
}
(t, Some(am))
} else {
(targets.get(ti).copied().unwrap_or(ti as u16), None)
};
if to == from {
continue;
}
let tgi = self.pile_group[to as usize] as usize;
let tg = &self.groups[tgi];
if from_kind == PileKind::Foundation && tg.kind == PileKind::Foundation {
continue;
}
let dst = &s.piles[to as usize];
if !use_active
&& ((tg.accept_first_round_only && s.redeals_used > 0)
|| !self.piles_full_ok(s, tg)
|| tg.min_accept_len.is_some_and(|n| dst.len() < n as usize))
{
continue;
}
let dst_empty = dst.is_empty();
let tg_sym = self.is_group_symmetric(tgi, s);
if !use_active && dst_empty {
if tg_sym {
if first_empty.0[tgi] != to {
continue;
}
} else {
let cid = self.pile_sym_class[to as usize];
if cid != u16::MAX && first_empty.1[cid as usize] != to {
continue;
}
}
}
let tr = self.rule(to);
let unrestricted = tg.unrestricted_from & (1 << fg) != 0;
if no_hooks && !use_active {
if tg.grid_bottom_open {
let cols = tg.columns.unwrap_or(tg.count);
let i = to - tg.start;
if dst_empty || (i >= cols && !s.piles[(to - cols) as usize].is_empty()) {
continue;
}
}
if tg.replace_on_drop.is_none()
&& tg.swap_bases.is_none()
&& tg.push_off_to.is_empty()
&& dst.len() >= tr.max_cards as usize
{
continue;
}
if !tr.complete_runs && !unrestricted && tg.rank_sequence.is_empty() {
if dst_empty {
if tr.empty == EmptyRule::None
&& !(tr.joker_anywhere && self.jokers > 0)
{
continue;
}
} else if !(tg.accept_any_on_single && dst.len() == 1)
&& !((tr.wild_permissive || tr.joker_anywhere) && self.jokers > 0)
&& tr.build.by == BuildBy::None
&& tg.swap_bases.is_none()
&& tg.push_off_to.is_empty()
{
continue;
}
}
}
let (lo, hi) = if tr.complete_runs {
let n = tg.run_len.map_or(self.nranks as usize, |l| l as usize);
if n < tr.min_accept as usize || n > unit {
continue;
}
(n, n)
} else if from_moves == MoveUnit::Whole {
if unit < tr.min_accept as usize {
continue;
}
(unit, unit)
} else if !dst_empty && !tg.push_off_to.is_empty() {
(1, 1)
} else {
let max_n = if no_hooks {
let room = if tg.replace_on_drop.is_some() || tg.swap_bases.is_some() {
tr.max_cards as usize
} else {
(tr.max_cards as usize).saturating_sub(dst.len())
};
let cap = if dst_empty {
tr.empty_max_accept.min(tr.max_accept) as usize
} else {
tr.non_empty_max_accept.min(tr.max_accept) as usize
};
unit.min(cap).min(room)
} else {
unit.min(tr.max_accept as usize)
};
let lo = tr.min_accept.max(1) as usize;
if lo > max_n {
continue;
}
(lo, max_n)
};
let fit_mask = if let Some(am) = pre_accept_mask {
(am != u64::MAX).then_some(am)
} else if no_hooks
&& self.jokers == 0
&& !tr.complete_runs
&& !dst_empty
&& !unrestricted
&& !(tg.accept_any_on_single && dst.len() == 1)
&& tg.rank_sequence.is_empty()
&& tg.swap_bases.is_none()
&& tg.push_off_to.is_empty()
{
let top = *dst.last().unwrap();
if !top.is_face_up() {
continue;
}
Some(tr.fits[top.face() as usize])
} else {
None
};
let supermove_cap = if from_moves == MoveUnit::Supermove && hi > 1 {
let mut cells = empty_cells;
let mut empties = empty_any_group[fg as usize] as u32;
if dst_empty {
if tg.kind == PileKind::Cell {
cells = cells.saturating_sub(1);
} else if tgi == fg as usize && tr.empty == EmptyRule::Any {
empties = empties.saturating_sub(1);
}
}
Some((cells + 1) << empties.min(16))
} else {
None
};
for n in lo..=hi {
if !move_count_allowed(from_moves, n, unit, src_len) {
continue;
}
if dst_empty && n == src_len && tgi == fg as usize {
let same_sym = tg_sym
|| (self.pile_sym_class[from as usize] != u16::MAX
&& self.pile_sym_class[from as usize]
== self.pile_sym_class[to as usize]);
if same_sym {
continue;
}
}
if let Some(mask) = fit_mask {
if mask >> src[src_len - n].face() & 1 == 0 {
if n == 1
&& tgi == fg as usize
&& from < to
&& match tg.swap_bases {
Some(SwapBases::BothSingle | SwapBases::Crossing) => {
src_len == 1 && dst.len() == 1
}
Some(SwapBases::EitherPile) => !dst_empty,
None => false,
}
&& self.can_swap(s, from, to)
{
out.push(Move::new(Action::Transfer { from, to, count: 1 }));
}
continue;
}
if tg.simple_accept
&& (use_active
|| (if dst_empty {
tg.empty_accepts_from
} else {
tg.accepts_from
}) & (1u64 << fg)
!= 0)
{
let supermove_ok = from_moves != MoveUnit::Supermove
|| n <= 1
|| (self.groups[fg as usize].supermove_complete_run
&& n == self.nranks as usize)
|| n <= supermove_cap.unwrap();
if supermove_ok {
out.push(Move::new(Action::Transfer {
from,
to,
count: n as u16,
}));
}
continue;
}
} else if tg.simple_accept
&& !tr.complete_runs
&& dst_empty
&& (tg.empty_accepts_from & (1u64 << fg) != 0)
{
if self.empty_accepts(to, src[src_len - n], s) {
let supermove_ok = from_moves != MoveUnit::Supermove
|| n <= 1
|| (self.groups[fg as usize].supermove_complete_run
&& n == self.nranks as usize)
|| n <= supermove_cap.unwrap();
if supermove_ok {
out.push(Move::new(Action::Transfer {
from,
to,
count: n as u16,
}));
}
}
continue;
}
if n == 1
&& from > to
&& match tg.swap_bases {
Some(SwapBases::BothSingle | SwapBases::Crossing) => {
src_len == 1 && dst.len() == 1
}
Some(SwapBases::EitherPile) => !dst_empty,
None => false,
}
&& self.can_swap(s, from, to)
{
continue;
}
if self.can_place_with_limit(s, from, to, n, supermove_cap) {
out.push(Move::new(Action::Transfer {
from,
to,
count: n as u16,
}));
}
}
}
};
if only.is_some() || no_hooks {
for &from in sources {
emit_from(from);
}
} else {
for from in all_piles_iter {
emit_from(from);
}
}
if !self.removal.is_empty() {
self.gen_removals(s, only, out);
}
if self.any_secondary_stock {
self.gen_secondary_deals(s, only, out);
}
if self.extract.is_some() {
self.gen_extracts(s, only, out);
}
self.gen_picks(s, only, out);
self.gen_waste_swaps(s, only, out);
}
pub(super) fn gen_picks(&self, s: &State, only: Option<u16>, out: &mut Vec<Move>) {
for &from in &self.pick_piles {
let src = &s.piles[from as usize];
let len = src.len();
if len < 2 || only.is_some_and(|o| o != from) || self.movable(s, from) == 0 {
continue;
}
let top = src[len - 1];
let fg = self.pile_group[from as usize] as usize;
let mut t = s.clone();
for i in 0..len - 1 {
let c = src[i];
if !c.is_face_up()
|| c.face() == top.face()
|| src[..i].iter().any(|d| d.face() == c.face())
{
continue;
}
t.piles[from as usize].swap(i, len - 1);
for &to in &self.targets_for_group[fg] {
if to != from && self.can_place(&t, from, to, 1) {
out.push(Move::new(Action::Pick {
from,
to,
index: i as u16,
}));
}
}
t.piles[from as usize].swap(i, len - 1);
}
}
}
pub(crate) fn can_deal_secondary(&self, s: &State, gi: usize) -> bool {
let Some(ss) = &self.groups[gi].secondary_stock else {
return false;
};
if s.base_rank.is_none()
&& self
.choose_base
.as_ref()
.is_some_and(|cb| !cb.free_moves || cb.lock_mask >> gi & 1 != 0)
{
return false;
}
let from = self.groups[gi].start as usize;
let to = self.groups[ss.to as usize].start as usize;
let src_len = s.piles[from].len();
if src_len == 0 {
return false;
}
if ss.unlock_when_to_empty {
s.piles[to].is_empty() || ss.stay_unlocked_below.is_some_and(|n| src_len < n as usize)
} else {
true
}
}
fn gen_extracts(&self, s: &State, only: Option<u16>, out: &mut Vec<Move>) {
let Some((fg, to_groups)) = &self.extract else {
return;
};
if !self.aux_allows(s, AuxCtx::Extract) {
return;
}
for from in self.groups[*fg as usize].piles() {
if only.is_some_and(|o| o != from) {
continue;
}
let pile = &s.piles[from as usize];
if pile.len() < 2 || !pile[pile.len() - 2].is_face_up() || !self.available(s, from) {
continue;
}
let mut tmp = s.clone();
tmp.piles[from as usize].pop();
let found = to_groups
.iter()
.flat_map(|&gi| self.groups[gi as usize].piles())
.find(|&t| t != from && self.can_place(&tmp, from, t, 1));
if let Some(to) = found {
out.push(Move::new(Action::Extract { from, to }));
}
}
}
fn gen_secondary_deals(&self, s: &State, only: Option<u16>, out: &mut Vec<Move>) {
for (gi, g) in self.groups.iter().enumerate() {
if let Some(ss) = &g.secondary_stock {
if only.is_none_or(|o| o == g.start) && self.can_deal_secondary(s, gi) {
out.push(Move::new(Action::DealSecondary {
from: g.start,
to: self.groups[ss.to as usize].start,
}));
}
}
}
}
pub(super) fn gen_removals(&self, s: &State, only: Option<u16>, out: &mut Vec<Move>) {
for (ri, r) in self.removal.iter().enumerate() {
if r.needs_latch && s.base_rank.is_none() {
continue;
}
let mut tops: Vec<(u16, Card)> = Vec::new();
for (p, pile) in s.piles.iter().enumerate() {
let p = p as u16;
if r.from & (1 << self.pile_group[p as usize]) == 0 || !self.available(s, p) {
continue;
}
if let Some(&c) = pile.last() {
if c.is_face_up() && !r.exclude_ranks.contains(&c.rank()) {
tops.push((p, c));
}
}
}
let neighbours = |piles: &[u16]| -> bool {
let Some((adj, gi)) = r.adjacent else {
return true;
};
let g = &self.groups[gi as usize];
let cols = g.columns.unwrap() as i32;
let pos = |p: u16| {
let i = (p - g.start) as i32;
(i / cols, i % cols)
};
piles.windows(2).all(|w| {
let ((r1, c1), (r2, c2)) = (pos(w[0]), pos(w[1]));
let (dr, dc) = ((r1 - r2).abs(), (c1 - c2).abs());
match adj {
Adjacency::Eight => dr.max(dc) == 1,
Adjacency::Four => dr + dc == 1,
Adjacency::SameLine => dr == 0 || dc == 0,
}
})
};
let mut emit = |piles: &[u16]| {
if only.is_none_or(|o| piles.contains(&o)) && neighbours(piles) {
out.push(Move::new(removal_action(ri, piles)));
}
};
match &r.kind {
RemovalKind::PairsSum { total, alone } => {
for (i, &(p, c)) in tops.iter().enumerate() {
if alone.contains(&c.rank()) {
emit(&[p]);
continue;
}
for &(q, d) in &tops[i + 1..] {
if c.is_joker() || d.is_joker() || c.rank() + d.rank() == *total {
emit(&[p, q]);
}
}
}
}
RemovalKind::RankDistance { distances } => {
for (i, &(p, c)) in tops.iter().enumerate() {
for &(q, d) in &tops[i + 1..] {
let gap = (c.rank() as i16 - d.rank() as i16).unsigned_abs() as u8;
if c.is_joker() || d.is_joker() || distances.contains(&gap) {
emit(&[p, q]);
}
}
}
}
RemovalKind::PairsSumOtherSuit { total } => {
for (i, &(p, c)) in tops.iter().enumerate() {
for &(q, d) in &tops[i + 1..] {
let other = c.is_joker() || d.is_joker() || c.suit() != d.suit();
if other
&& (c.is_joker() || d.is_joker() || c.rank() + d.rank() == *total)
{
emit(&[p, q]);
}
}
}
}
RemovalKind::PairsSumSameSuit { total } => {
for (i, &(p, c)) in tops.iter().enumerate() {
for &(q, d) in &tops[i + 1..] {
let suited = c.is_joker() || d.is_joker() || c.suit() == d.suit();
if suited
&& (c.is_joker() || d.is_joker() || c.rank() + d.rank() == *total)
{
emit(&[p, q]);
}
}
}
}
RemovalKind::SameRankIn { ranks } => {
for (i, &(p, c)) in tops.iter().enumerate() {
for &(q, d) in &tops[i + 1..] {
let ok = if c.is_joker() && d.is_joker() {
true
} else if c.is_joker() {
ranks.contains(&d.rank())
} else if d.is_joker() {
ranks.contains(&c.rank())
} else {
c.rank() == d.rank() && ranks.contains(&c.rank())
};
if ok {
emit(&[p, q]);
}
}
}
}
RemovalKind::SameRank => {
for (i, &(p, c)) in tops.iter().enumerate() {
for &(q, d) in &tops[i + 1..] {
if c.rank() == d.rank() || c.is_joker() || d.is_joker() {
emit(&[p, q]);
}
}
}
}
RemovalKind::SameSuit => {
for (i, &(p, c)) in tops.iter().enumerate() {
for &(q, d) in &tops[i + 1..] {
if c.suit() == d.suit() || c.is_joker() || d.is_joker() {
emit(&[p, q]);
}
}
}
}
RemovalKind::LowerOfSuit => {
for &(p, c) in tops.iter().filter(|(_, c)| !c.is_joker()) {
let ci = self.rank_idx[c.rank() as usize];
if tops.iter().any(|&(q, d)| {
q != p
&& !d.is_joker()
&& d.suit() == c.suit()
&& self.rank_idx[d.rank() as usize] > ci
}) {
emit(&[p]);
}
}
}
RemovalKind::RankSet { ranks } => {
let mut sets: Vec<Vec<u16>> = Vec::new();
let mut pick: Vec<u16> = Vec::new();
fn rec(
ranks: &[u8],
tops: &[(u16, Card)],
pick: &mut Vec<u16>,
sets: &mut Vec<Vec<u16>>,
) {
let Some((&r, rest)) = ranks.split_first() else {
let mut v = pick.clone();
v.sort_unstable();
if !sets.contains(&v) {
sets.push(v);
}
return;
};
for &(p, c) in tops {
if (c.rank() == r || c.is_joker()) && !pick.contains(&p) {
pick.push(p);
rec(rest, tops, pick, sets);
pick.pop();
}
}
}
rec(ranks, &tops, &mut pick, &mut sets);
for set in sets {
emit(&set);
}
}
RemovalKind::ReachSuitOrRank { distances } => {
for (i, &(p, c)) in tops.iter().enumerate() {
let pg = self.pile_group[p as usize];
for &(q, d) in &tops[i + 1..] {
if self.pile_group[q as usize] != pg {
continue;
}
if distances.contains(&q.abs_diff(p))
&& (c.is_joker()
|| d.is_joker()
|| c.suit() == d.suit()
|| c.rank() == d.rank())
{
emit(&[p, q]);
}
}
}
}
RemovalKind::BetweenMatch { max_len } => {
let max_l = *max_len as usize;
for (gi, g) in self.groups.iter().enumerate() {
if r.from & (1 << gi) == 0 {
continue;
}
let start = g.start;
let end = g.start + g.count;
for len in 1..=max_l {
for p in start + 1..end {
let last = p as usize + len - 1;
let right = p as usize + len;
if right >= end as usize {
break;
}
let (Some(&lc), Some(&rc)) =
(s.piles[p as usize - 1].last(), s.piles[right].last())
else {
continue;
};
if !lc.is_face_up() || !rc.is_face_up() {
continue;
}
let match_lr = lc.is_joker()
|| rc.is_joker()
|| lc.suit() == rc.suit()
|| lc.rank() == rc.rank();
if !match_lr {
continue;
}
let mut buf = [0u16; 4];
let mut ok = true;
for (k, slot) in buf.iter_mut().enumerate().take(len) {
let q = p + k as u16;
if !self.available(s, q)
|| !s.piles[q as usize]
.last()
.is_some_and(|c| c.is_face_up())
{
ok = false;
break;
}
*slot = q;
}
let _ = last;
if ok {
emit(&buf[..len]);
}
}
}
}
}
RemovalKind::ContiguousSumMultiple { divisor, max_len } => {
let div = *divisor as u32;
let min_l = r.min_len.unwrap_or(1) as usize;
let max_l = *max_len as usize;
let max_s = r.max_sum.map(|m| m as u32);
let face_ten = r.face_ten;
let mut buf: Vec<u16> = Vec::with_capacity(max_l);
for (gi, g) in self.groups.iter().enumerate() {
if r.from & (1 << gi) == 0 {
continue;
}
let start = g.start;
let end = g.start + g.count;
for p in start..end {
let mut sum = 0u32;
buf.clear();
for k in 0..max_l {
let q = p + k as u16;
if q >= end || !self.available(s, q) {
break;
}
let Some(&c) = s.piles[q as usize].last() else {
break;
};
if !c.is_face_up()
|| c.is_joker()
|| r.exclude_ranks.contains(&c.rank())
{
break;
}
let val = if face_ten && c.rank() > 10 {
10
} else {
c.rank() as u32
};
sum += val;
if max_s.is_some_and(|m| sum > m) {
break;
}
buf.push(q);
let len = k + 1;
if len >= min_l
&& sum % div == 0
&& only.is_none_or(|o| o >= p && o <= q)
&& neighbours(&buf)
{
let action = if len <= 4 {
removal_action(ri, &buf)
} else {
Action::RemoveRange {
rule: ri as u8,
start: p,
len: len as u16,
}
};
out.push(Move::new(action));
}
}
}
}
}
}
}
}
pub(crate) fn effective_redeals(&self, s: &State, st: &super::Stock) -> u32 {
let Some(br) = &st.bonus_redeals else {
return st.redeals;
};
let n = self.groups[br.groups[0] as usize].count;
let thr = br.threshold as usize;
let mut bonus = 0u32;
let mut i = 0u16;
while i + 1 < n {
let l1: usize = br
.groups
.iter()
.map(|&gi| s.piles[(self.groups[gi as usize].start + i) as usize].len())
.sum();
let l2: usize = br
.groups
.iter()
.map(|&gi| s.piles[(self.groups[gi as usize].start + i + 1) as usize].len())
.sum();
if l1 + l2 >= 2 * thr {
bonus += 2;
} else if l1 >= thr || l2 >= thr {
bonus += 1;
}
i += 2;
}
let total = st.redeals.saturating_add(bonus);
br.max_redeals.map_or(total, |cap| total.min(cap))
}
pub fn can_deal(&self, s: &State) -> bool {
if s.base_rank.is_none()
&& self.choose_base.as_ref().is_some_and(|cb| {
!cb.free_moves
|| self.stock.as_ref().is_some_and(|st| {
cb.lock_mask >> self.pile_group[st.pile as usize] & 1 != 0
})
})
{
return false;
}
if !self.aux_allows(s, AuxCtx::Deal) {
return false;
}
if let Some(rd) = &self.redeal {
if s.redeals_used >= rd.times {
return false;
}
if matches!(rd.how, RedealHow::Compact { .. }) {
let g = &self.groups[rd.group as usize];
let mut saw_empty = false;
let mut can_shift = false;
for p in g.piles() {
if s.piles[p as usize].is_empty() {
saw_empty = true;
} else if saw_empty {
can_shift = true;
break;
}
}
let can_refill = saw_empty
&& rd.refill.is_some_and(|rf| {
self.groups[rf as usize]
.piles()
.any(|p| !s.piles[p as usize].is_empty())
});
return can_shift || can_refill;
}
if matches!(rd.how, RedealHow::CompactColumns { .. }) {
let g = &self.groups[rd.group as usize];
let cols = g.columns.unwrap_or(g.count);
let rows = g.count / cols;
let mut saw_empty = false;
let mut can_shift = false;
for col in 0..cols {
let mut saw_empty_col = false;
for row in 0..rows {
let p = g.start + row * cols + col;
if s.piles[p as usize].is_empty() {
saw_empty = true;
saw_empty_col = true;
} else if saw_empty_col {
can_shift = true;
}
}
}
let can_refill = saw_empty
&& rd.refill.is_some_and(|rf| {
self.groups[rf as usize]
.piles()
.any(|p| !s.piles[p as usize].is_empty())
});
return can_shift || can_refill;
}
if matches!(rd.how, RedealHow::FillEmpty { .. }) {
let g = &self.groups[rd.group as usize];
let has_empty = g.piles().any(|p| s.piles[p as usize].is_empty());
let has_src = rd.refill.is_some_and(|rf| {
self.groups[rf as usize]
.piles()
.any(|p| !s.piles[p as usize].is_empty())
});
return has_empty && has_src;
}
if rd.how == RedealHow::RotateBottom {
let g = &self.groups[rd.group as usize];
return g.piles().any(|p| s.piles[p as usize].len() > 1);
}
if matches!(
rd.how,
RedealHow::DealRows | RedealHow::DealStaircase | RedealHow::ShuffleRedeal
) {
let g = &self.groups[rd.group as usize];
return g.piles().any(|p| !s.piles[p as usize].is_empty());
}
return true;
}
let Some(st) = &self.stock else { return false };
if st.redeal_switch_to_auto_refill.is_some() && s.redeals_used >= 1 {
return false;
}
if let Some(r) = &self.rounds {
return !s.piles[r.starter as usize].is_empty()
&& s.piles[st.pile as usize].len() >= self.round_cards(r);
}
if let Some(grp_gi) = st.sweep_group_on_waste {
let to0 = self.groups[st.target as usize].start;
return !s.piles[st.pile as usize].is_empty()
|| !s.piles[to0 as usize].is_empty()
|| self.groups[grp_gi as usize]
.piles()
.any(|p| !s.piles[p as usize].is_empty());
}
if let Some(rows_gi) = st.fan_rows {
if !s.piles[st.pile as usize].is_empty() {
return true;
}
return s.redeals_used < st.redeals
&& [st.target, rows_gi].iter().any(|&gi| {
self.groups[gi as usize]
.piles()
.any(|p| !s.piles[p as usize].is_empty())
});
}
if st.reshuffle_at_counter.is_some_and(|n| s.aux[0] >= n) {
return s.redeals_used < st.redeals;
}
if st.fill_empty_or_reshuffle {
let stock_has = !s.piles[st.pile as usize].is_empty();
let any_empty = st.targets.iter().any(|&p| s.piles[p as usize].is_empty());
let any_cards = st.targets.iter().any(|&p| !s.piles[p as usize].is_empty());
return (stock_has && any_empty)
|| (s.redeals_used < st.redeals && (stock_has || any_cards));
}
if st.wrap_mid_deal {
let stock_len = s.piles[st.pile as usize].len();
let waste_len = s.piles[self.groups[st.target as usize].start as usize].len();
if stock_len == 0 && waste_len == 0 {
return false;
}
let ncards = st.deal_count(s.redeals_used) as usize;
return stock_len >= ncards || s.redeals_used < self.effective_redeals(s, st);
}
if !s.piles[st.pile as usize].is_empty() {
if st.redeal_gather_only {
return false;
}
if !st.face_up
&& s.piles[st.pile as usize]
.last()
.is_some_and(|c| c.is_face_up())
{
return false;
}
let stock_len = s.piles[st.pile as usize].len() as u16;
if st.final_split_to.is_some() && stock_len < 2 {
return false;
}
let rtargets = st.round_targets(s.redeals_used);
let any_non_empty = || rtargets.iter().any(|&p| !s.piles[p as usize].is_empty());
let any_empty = || rtargets.iter().any(|&p| s.piles[p as usize].is_empty());
let final_to_active = st
.final_to
.is_some_and(|ft| stock_len <= self.groups[ft as usize].count);
if let Some(req_gi) = st.require_no_empty_group {
if self.groups[req_gi as usize]
.piles()
.any(|p| s.piles[p as usize].is_empty())
{
return false;
}
}
if st.max_deals.is_some_and(|m| s.redeals_used >= m) {
return false;
}
if let Some(req_gi) = st.require_group_empty {
if self.groups[req_gi as usize]
.piles()
.any(|p| !s.piles[p as usize].is_empty())
{
return false;
}
}
if st.sweep_to_discard_round1.is_some() {
if s.redeals_used >= 1 {
let dest = self.groups[st.target as usize].start;
return s.piles[dest as usize].len() < self.rule(dest).max_cards as usize;
}
return true;
}
let top_up_active = st.top_up_group_to.is_some_and(|(gi, n)| {
self.groups[gi as usize]
.piles()
.any(|p| s.piles[p as usize].len() < n as usize)
});
if !top_up_active
&& (st.only_when_empty || (st.only_when_empty_first_round && s.redeals_used == 0))
&& any_non_empty()
{
return false;
}
if !top_up_active && st.first_empty && !any_empty() {
return false;
}
if !top_up_active && st.require_no_empty && !final_to_active && any_empty() {
return false;
}
if let Some(r) = st.skip_run_with_rank {
if st
.targets
.iter()
.all(|&p| self.top_run_has_rank(p, &s.piles[p as usize], r))
{
return false;
}
}
if let Some(from) = st.skip_built_from {
if st
.targets
.iter()
.all(|&p| self.is_built_from(p, &s.piles[p as usize], from))
{
return false;
}
}
if let Some(req_gi) = st.require_group_empty {
if self.groups[req_gi as usize]
.piles()
.any(|p| !s.piles[p as usize].is_empty())
{
return false;
}
}
if let Some(other_gi) = st.skip_when_same_index_full {
let final_piles_active = st
.final_piles
.as_ref()
.is_some_and(|fp| stock_len as usize <= fp.len());
if !final_to_active && !final_piles_active {
let tg_start = self.groups[st.target as usize].start;
let og_start = self.groups[other_gi as usize].start;
if rtargets.iter().all(|&p| {
let op = og_start + (p - tg_start);
s.piles[op as usize].len() >= self.rule(op).max_cards as usize
}) {
return false;
}
}
}
if !top_up_active && matches!(st.target_kind, PileKind::Waste | PileKind::Foundation) {
let dest = st.targets[0];
let n = st
.deal_count(s.redeals_used)
.min(s.piles[st.pile as usize].len() as u16);
if s.piles[dest as usize].len() + n as usize > self.rule(dest).max_cards as usize {
return false;
}
}
if st.fill_grid_holes_then_row && self.grid_hole_fill_targets(s, st).is_empty() {
return false;
}
return true;
}
if let Some(discard_gi) = st.sweep_to_discard_round1 {
let discard0 = self.groups[discard_gi as usize].start;
return s.redeals_used < st.redeals
&& (!s.piles[discard0 as usize].is_empty()
|| self.groups[st.target as usize]
.piles()
.any(|p| !s.piles[p as usize].is_empty()));
}
if let Some(gi) = st.redeal_bottom_count_from {
let k: usize = self.groups[gi as usize]
.piles()
.map(|p| s.piles[p as usize].len())
.sum();
return s.redeals_used < st.redeals
&& k > 0
&& s.piles[st.targets[0] as usize].len() >= k;
}
s.redeals_used < st.redeals
&& (self.groups[st.target as usize]
.piles()
.any(|p| !s.piles[p as usize].is_empty())
|| st
.redeal_from_groups
.iter()
.chain(st.bookend.iter())
.any(|&gi| {
self.groups[gi as usize]
.piles()
.any(|p| !s.piles[p as usize].is_empty())
}))
}
pub fn legal_moves(&self, s: &State) -> Vec<Move> {
let mut out = Vec::new();
self.gen_actions(s, None, &mut out);
if self.can_deal(s) {
out.push(Move::new(Action::Deal));
}
out
}
pub fn is_legal(&self, s: &State, mv: Move) -> bool {
let mut st = s.clone();
let mut j = Vec::new();
for _ in 0..mv.talon {
if !self.talon_step(&mut st, &mut j) {
return false;
}
}
let mut moves = Vec::new();
match mv.action {
Action::Deal => return self.can_deal(&st),
_ => self.gen_actions_unpruned(&st, &mut moves),
}
moves.contains(&Move::new(mv.action))
}
pub(super) fn gen_actions_unpruned(&self, s: &State, out: &mut Vec<Move>) {
let n = s.piles.len() as u16;
for from in 0..n {
if !self.available(s, from) {
continue;
}
let unit = self.movable(s, from);
let from_moves = self.rule(from).moves;
let src_len = s.piles[from as usize].len();
for to in 0..n {
for k in 1..=unit {
if !move_count_allowed(from_moves, k, unit, src_len) {
continue;
}
if self.can_place(s, from, to, k) {
out.push(Move::new(Action::Transfer {
from,
to,
count: k as u16,
}));
}
}
}
}
self.gen_removals(s, None, out);
if self.any_secondary_stock {
self.gen_secondary_deals(s, None, out);
}
if self.extract.is_some() {
self.gen_extracts(s, None, out);
}
self.gen_picks(s, None, out);
self.gen_waste_swaps(s, None, out);
}
}
fn sum_within(sl: &SumLimit, have: &[Card], add: &[Card], max_cards: u16) -> bool {
let mut total = 0u16;
let mut lifted = false;
for c in have.iter().chain(add) {
if c.is_joker() {
return false;
}
let r = c.rank();
if sl.lift_ranks.contains(&r) {
lifted = true;
} else {
total += sl.values[r as usize - 1];
}
}
if lifted && total < sl.target {
total = sl.target;
}
total <= sl.target && (have.len() + add.len() < max_cards as usize || total == sl.target)
}
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum RunFilter {
Any,
SameSuit,
AltColor,
SameColor,
}
#[inline]
fn is_run_unit(moves: MoveUnit) -> bool {
matches!(
moves,
MoveUnit::Run
| MoveUnit::SuitRun
| MoveUnit::AltColorRun
| MoveUnit::SameColorRun
| MoveUnit::SuitRunOrSameRank
| MoveUnit::Supermove
| MoveUnit::TopOrWhole
)
}
#[inline]
fn move_count_allowed(moves: MoveUnit, n: usize, unit: usize, src_len: usize) -> bool {
match moves {
MoveUnit::Whole => n == unit,
MoveUnit::TopOrWhole => n == 1 || n == src_len,
_ => true,
}
}
fn removal_action(rule: usize, piles: &[u16]) -> Action {
let mut a = [u16::MAX; 4];
let mut v = piles.to_vec();
v.sort_unstable();
a[..v.len()].copy_from_slice(&v);
Action::Remove {
rule: rule as u8,
n: v.len() as u8,
piles: a,
}
}
#[cfg(test)]
mod sum_tests {
use super::*;
fn c(s: &str) -> Card {
s.parse().unwrap()
}
#[test]
fn totals_cap_lift_and_complete() {
let sl = SumLimit::new(10, &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0]).lift_ranks(&[10]);
assert!(sum_within(&sl, &[c("5S"), c("4H")], &[c("AS")], 4));
assert!(!sum_within(
&sl,
&[c("5S"), c("4H"), c("AS")],
&[c("2S")],
4
));
assert!(sum_within(&sl, &[c("5S"), c("4H"), c("AS")], &[c("QS")], 4));
assert!(!sum_within(&sl, &[c("9S")], &[c("2H")], 4));
assert!(sum_within(&sl, &[c("3S")], &[c("10H")], 4));
assert!(sum_within(&sl, &[c("3S"), c("10H")], &[c("AS")], 4));
assert!(!sum_within(&sl, &[c("3S"), c("10H")], &[c("9S")], 4));
assert!(sum_within(&sl, &[c("3S"), c("10H")], &[c("KS")], 4));
}
}