implement Stack using ArrayVec
This commit is contained in:
+24
-15
@@ -101,14 +101,16 @@ impl Card {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||
pub struct Stack(Vec<Card>);
|
||||
impl Stack {
|
||||
pub struct Stack<const CAP: usize>(arrayvec::ArrayVec<Card, CAP>);
|
||||
impl<const CAP: usize> Stack<CAP> {
|
||||
pub fn new() -> Self {
|
||||
Self(Vec::new())
|
||||
Self(arrayvec::ArrayVec::new())
|
||||
}
|
||||
}
|
||||
impl Stack<52> {
|
||||
/// Generate a full deck of cards with the specified deck id.
|
||||
pub fn full_deck(deck: u8) -> Stack {
|
||||
let mut stack = Vec::with_capacity(52);
|
||||
pub fn full_deck(deck: u8) -> Self {
|
||||
let mut stack = arrayvec::ArrayVec::new();
|
||||
for suit in Suit::SUITS {
|
||||
for value in 1..=13 {
|
||||
stack.push(Card::new(deck, suit, CardValue(value)));
|
||||
@@ -117,36 +119,43 @@ impl Stack {
|
||||
Stack(stack)
|
||||
}
|
||||
}
|
||||
impl From<Vec<Card>> for Stack {
|
||||
fn from(value: Vec<Card>) -> Self {
|
||||
impl<const CAP: usize> From<arrayvec::ArrayVec<Card, CAP>> for Stack<CAP> {
|
||||
fn from(value: arrayvec::ArrayVec<Card, CAP>) -> Self {
|
||||
Self(value)
|
||||
}
|
||||
}
|
||||
impl std::ops::Deref for Stack {
|
||||
type Target = Vec<Card>;
|
||||
impl<const CAP: usize> std::ops::Deref for Stack<CAP> {
|
||||
type Target = arrayvec::ArrayVec<Card, CAP>;
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
impl std::ops::DerefMut for Stack {
|
||||
impl<const CAP: usize> std::ops::DerefMut for Stack<CAP> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.0
|
||||
}
|
||||
}
|
||||
impl<const CAP: usize> IntoIterator for Stack<CAP> {
|
||||
type Item = Card;
|
||||
type IntoIter = arrayvec::IntoIter<Card, CAP>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.0.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||
pub struct Pile {
|
||||
face_down: Stack,
|
||||
face_up: Stack,
|
||||
pub struct Pile<const CAP: usize> {
|
||||
face_down: Stack<CAP>,
|
||||
face_up: Stack<CAP>,
|
||||
}
|
||||
impl Pile {
|
||||
impl<const CAP: usize> Pile<CAP> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
face_down: Stack::new(),
|
||||
face_up: Stack::new(),
|
||||
}
|
||||
}
|
||||
pub fn new_face_down(stack: Stack) -> Self {
|
||||
pub fn new_face_down(stack: Stack<CAP>) -> Self {
|
||||
Self {
|
||||
face_down: stack,
|
||||
face_up: Stack::new(),
|
||||
|
||||
+9
-9
@@ -75,13 +75,13 @@ impl KlondikeInstruction {
|
||||
|
||||
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
||||
struct KlondikeState {
|
||||
piles: [Pile; 13],
|
||||
piles: [Pile<13>; 13],
|
||||
}
|
||||
impl KlondikeState {
|
||||
fn pile(&self, index: KlondikePileId) -> &Pile {
|
||||
fn pile(&self, index: KlondikePileId) -> &Pile<13> {
|
||||
&self.piles[index as usize]
|
||||
}
|
||||
fn pile_mut(&mut self, index: KlondikePileId) -> &mut Pile {
|
||||
fn pile_mut(&mut self, index: KlondikePileId) -> &mut Pile<13> {
|
||||
&mut self.piles[index as usize]
|
||||
}
|
||||
fn is_instruction_valid(&self, instruction: KlondikeInstruction) -> bool {
|
||||
@@ -187,18 +187,18 @@ impl Klondike {
|
||||
let mut deck = Stack::full_deck(0);
|
||||
use rand::seq::SliceRandom;
|
||||
deck.shuffle(&mut seed);
|
||||
let mut deck = deck.into_iter();
|
||||
|
||||
// generate tableaus
|
||||
let [t0, t1, t2, t3, t4, t5, t6, t7] = core::array::from_fn(|i| {
|
||||
let remaining = deck.split_off(i).into();
|
||||
let stack = core::mem::replace(&mut deck, remaining);
|
||||
let stack = arrayvec::ArrayVec::from_iter((&mut deck).take(i)).into();
|
||||
let mut pile = Pile::new_face_down(stack);
|
||||
pile.push(deck.pop().unwrap());
|
||||
pile.push(deck.next().unwrap());
|
||||
pile
|
||||
});
|
||||
|
||||
// stock is remaining cards
|
||||
let stock = Pile::new_face_down(deck);
|
||||
let stock = Pile::new_face_down(arrayvec::ArrayVec::from_iter(deck).into());
|
||||
|
||||
let state = KlondikeState {
|
||||
piles: [
|
||||
@@ -220,11 +220,11 @@ impl Klondike {
|
||||
Self { config, state }
|
||||
}
|
||||
#[inline]
|
||||
pub fn pile(&self, index: KlondikePileId) -> &Pile {
|
||||
pub fn pile(&self, index: KlondikePileId) -> &Pile<13> {
|
||||
self.state.pile(index)
|
||||
}
|
||||
#[inline]
|
||||
fn pile_mut(&mut self, index: KlondikePileId) -> &mut Pile {
|
||||
fn pile_mut(&mut self, index: KlondikePileId) -> &mut Pile<13> {
|
||||
self.state.pile_mut(index)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user