Image source: By Liko81 - Own work, CC BY 3.0
Whist is a trick-taking game for four players in partnerships. The deck is shuffled and 13 cards are dealt to each player. The last card dealt determines the trump suit. Players play 13 tricks, rotating the lead to the player who won the last trick. Teams score points for the number of tricks over 6.
These rules are summarized from https://www.pagat.com/whist/whist.html.
Whist is the prototypical trick-taking game. Other games related to Whist are Bridge, Spades, Rang, and Rússi.
The diagram above visualizes information flow in Whist using a single random rollout in CardStock. Visibility is attached to card locations, so all cards in a location share the same visibility; information is revealed or concealed as cards move between locations and ownership changes.
Public locations are shown in green, hidden locations in yellow, private locations in blue, and memory locations in light gray. Locations containing cards with distinct backs are indicated with bold borders. Rectangles denote ownership (players or table), and directed edges show card movement between locations and resulting visibility changes. Taken information is shown with red dotted edges (public to private/hidden), and shared information with blue dashed edges (private cards transferred to another player).
The rules for Whist are coded in RECYCLE, a card game description language, to encourage standardized implementations across different systems.
You can also Download the code.
;; Whist
;; https://www.pagat.com/whist/whist.html
(game
(setup
;; Set up the players, 2 teams of 2 players, alternating
(create players 4)
(create teams (1, 3) (2, 4))
;; Create the deck source
(create deck (game iloc STOCK)
(deck (RANK (A, TWO, THREE, FOUR, FIVE, SIX,
SEVEN, EIGHT, NINE, TEN, J, Q, K))
(COLOR (RED (SUIT (HEARTS, DIAMONDS)))
(BLACK (SUIT (CLUBS, SPADES)))))))
;; Setup the game
(do (
(shuffle (game iloc STOCK))
(all player 'P
(do (
(repeat 13
(move (top (game iloc STOCK))
(top ('P iloc HAND))))
(set ('P sto TRICKSWON) 0))))
(set (game str LEAD) NONE)
(set (game str TRUMP)
(cardatt SUIT (top ((previous player) iloc HAND))))))
;; players play a round 13 times
(stage player
(end (all player 'P (== (size ('P iloc HAND)) 0)))
;; players play a hand once
(stage player
(end (all player 'P (> (size ('P vloc TRICK)) 0)))
(choice (
;; if following player and cannot follow SUIT
;; play any card
((and (!= (game str LEAD) NONE)
(== (size (filter ((current player) iloc HAND) 'L
(== (cardatt SUIT 'L)
(game str LEAD)))) 0))
(any ((current player) iloc HAND) 'C
(move 'C (top ((current player) vloc TRICK)))))
;; if following player and can follow SUIT
;; play any card that follows SUIT
((!= (game str LEAD) NONE)
(any (filter ((current player) iloc HAND) 'L
(== (cardatt SUIT 'L)
(game str LEAD))) 'C
(move 'C (top ((current player) vloc TRICK)))))
;; if first player, play any card, remember it in the lead spot
((== (game str LEAD) NONE)
(any ((current player) iloc HAND) 'C
(do (
(move 'C (top ((current player) vloc TRICK)))
(set (game str LEAD)
(cardatt SUIT (top ((current player) vloc TRICK)))))))))))
;; after players play hand, computer wraps up trick
(do (
;; solidfy card recedence
(set (game points PRECEDENCE)
(((SUIT : (game str TRUMP)) 200) ((SUIT : (game str LEAD)) 100)
((RANK : A) 14) ((RANK : K) 13) ((RANK : Q) 12)
((RANK : J) 11) ((RANK : TEN) 10) ((RANK : NINE) 9)
((RANK : EIGHT) 8) ((RANK : SEVEN) 7) ((RANK : SIX) 6)
((RANK : FIVE) 5) ((RANK : FOUR) 4) ((RANK : THREE) 3)
((RANK : TWO) 2)))
;; determine who won the hand, set them first next time,
;; and give them a point
(set (game str LEAD) NONE)
(cycle next (owner (max (union (all player 'P ('P vloc TRICK)))
using (game points PRECEDENCE))))
(inc ((next player) sto TRICKSWON))
;; discard all the played cards
(all player 'P
(move (top ('P vloc TRICK))
(top (game vloc DISCARD)))))))
;; determine team score
(stage team
(end (all player 'P (== ('P sto TRICKSWON) 0)))
(do (
;; Add individual points from each player on the team
(set ((current team) sto TRICKSWON) 0)
(all (current team) 'TP
(do (
(inc ((current team) sto TRICKSWON) ('TP sto TRICKSWON))
(set ('TP sto TRICKSWON) 0))))
;; team made their cumulative bid, score positive points
((> ((current team) sto TRICKSWON) 6)
(inc ((current team) sto SCORE)
(- ((current team) sto TRICKSWON) 6))))))
(scoring max ((team (current player)) sto SCORE)))