Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 772459
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:48:23+00:00 2026-05-14T18:48:23+00:00

I am trying to write a Spider Solitaire player as an exercise in learning

  • 0

I am trying to write a Spider Solitaire player as an exercise in learning Clojure. I am trying to figure out how to deal the cards.

I have created (with the help of stackoverflow), a shuffled sequence of 104 cards from two standard decks. Each card is represented as a

(defstruct card :rank :suit :face-up)

The tableau for Spider will be represented as follows:

(defstruct tableau :stacks :complete)

where :stacks is a vector of card vectors, 4 of which contain 5 cards face down and 1 card face up, and 6 of which contain 4 cards face down and 1 card face up, for a total of 54 cards, and :complete is an (initially) empty vector of completed sets of ace-king (represented as, for example, king-hearts, for printing purposes). The remainder of the undealt deck should be saved in a ref

(def deck (ref seq))

During the game, a tableau may contain, for example:

(struct-map tableau
  :stacks [[AH 2C KS ...]
           [6D QH JS ...]
           ...
           ]
  :complete [KC KS])

where “AH” is a card containing {:rank :ace :suit :hearts :face-up false}, etc.

How can I write a function to deal the stacks and then save the remainder in the ref?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:48:23+00:00Added an answer on May 14, 2026 at 6:48 pm

    You could write a function to take chunks vectors of size items each from a given sequence and another one to drop those chunks from the front:

    ;; note the built-in assumption that s contains enough items;
    ;; if it doesn't, one chunk less then requested will be produced
    (defn take-chunks [chunks size s]
      (map vec (partition size (take (* chunks size) s))))
    
    ;; as above, no effort is made to handle short sequences in some special way;
    ;; for a short input sequence, an empty output sequence will be returned
    (defn drop-chunks [chunks size s]
      (drop (* chunks size) s))
    

    Then maybe add a function to do both (modelled after split-at and split-with):

    (defn split-chunks [chunks size s]
      [(take-chunks chunks size s)
       (drop-chunks chunks size s)])
    

    Assuming that each card is initially {:face-up false}, you can use the following function to turn the last card on a stack:

    (defn turn-last-card [stack]
      (update-in stack [(dec (count stack)) :face-up] not))
    

    Then a function to deal out the initial stacks / chunks from the the given deck:

    (defn deal-initial-stacks [deck]
      (dosync
        (let [[short-stacks remaining] (split-chunks 6 5 deck)
              [long-stacks remaining] (split-chunks 4 6 remaining)]
          [remaining
           (vec (map turn-last-card
                     (concat short-stacks long-stacks)))])))
    

    The return value is a doubleton vector whose first element is the remainder of the deck and whose second element is a vector of the initial stacks.

    Then use this in a transaction to take the Ref into account:

    (dosync (let [[new-deck stacks] (deal-initial-stacks @deck-ref)]
              (ref-set deck-ref new-deck)
              stacks))
    

    Better yet, keep the whole state of the game in a single Ref or Atom and switch from ref-set to alter / swap! (I’ll use a Ref for this example, omit the dosync and switch alter to swap! to use an atom instead):

    ;; the empty vector is for the stacks
    (def game-state-ref (ref [(get-initial-deck) []]))
    
    ;; deal-initial-stacks only takes a deck as an argument,
    ;; but the fn passed to alter will receive a vector of [deck stacks];
    ;; the (% 0) bit extracts the first item of the vector,
    ;; that is, the deck; you could instead change the arguments
    ;; vector of deal-initial-stacks to [[deck _]] and pass the
    ;; modified deal-initial-stacks to alter without wrapping in a #(...)
    (dosync (alter game-state-ref #(deal-initial-stacks (% 0))))
    

    Disclaimer: None of this has received the slightest amount of testing attention (though I think it should work fine, modulo any silly typos I might have missed). It’s your exercise, though, so I think leaving the testing / polishing part to you is fine. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 391k
  • Answers 391k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Some use cases: multimap With ZIP code as a key,… May 15, 2026 at 1:32 am
  • Editorial Team
    Editorial Team added an answer Unfortunately, disabling the UITextField won't dismiss the keyboard. You'll need… May 15, 2026 at 1:32 am
  • Editorial Team
    Editorial Team added an answer Well, it turns out that IIS somehow got set to… May 15, 2026 at 1:32 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.