Example

The following is an example game written in RECYCLE called Agram. Agram is a simple Nigerian trick-taking card game for 2 to 6 players. Players are dealt six cards from a reduced French deck, and play six tricks. To win a trick, players must follow the suit of the lead player with a higher card; there is no trump suit. The object of the game is to win the last trick.

  1;; Agram
  2;;
  3;; https://www.pagat.com/last/agram.html
  4
  5(game
  6(declare 4 'NUMP)
  7(setup
  8  (create players 'NUMP)
  9
 10  ;; Create the deck source
 11  (create deck (game iloc STOCK) (deck (RANK (THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN))
 12                                      (COLOR (RED (SUIT (HEARTS, DIAMONDS)))
 13                                              (BLACK (SUIT (SPADES, CLUBS))))))
 14  (create deck (game iloc STOCK) (deck (RANK (ACE))
 15                                      (COLOR (RED (SUIT (HEARTS, DIAMONDS)))
 16                                              (BLACK (SUIT (CLUBS)))))))
 17
 18
 19;; Shuffle and deal each player 6 cards
 20(do (
 21      (shuffle (game iloc STOCK))
 22      (set (game str LEAD) NONE)
 23      (all player 'P
 24          (repeat 6
 25                  (move (top (game iloc STOCK))
 26                        (top ('P iloc HAND)))))))
 27
 28;; players play a round 6 times
 29(stage player
 30        (end
 31        (all player 'P
 32              (== (size ('P iloc HAND)) 0)))
 33
 34        ;; players play a hand once
 35        (stage player
 36              (end
 37                (all player 'P
 38                    (> (size ('P vloc TRICK)) 0)))
 39
 40              (choice (
 41
 42                ;; if following player cannot follow SUIT
 43                ;;   play any card, and end your turn
 44                ((and (!= (game str LEAD) NONE)
 45                      (== (size (filter ((current player) iloc HAND) 'C
 46                                        (== (cardatt SUIT 'C)
 47                                            (game str LEAD)))) 0))
 48                  (any ((current player) iloc HAND) 'AC
 49                      (move 'AC
 50                            (top ((current player) vloc TRICK)))))
 51
 52                ;; if following player and can follow SUIT
 53                ;;   play any card that follows SUIT, and end your turn
 54                (any (filter ((current player) iloc HAND) 'T
 55                              (== (cardatt SUIT 'T)
 56                                  (game str LEAD)))
 57                      'C
 58                                ((!= (game str LEAD) NONE)
 59                                (move 'C
 60                                      (top ((current player) vloc TRICK)))))
 61
 62                ;; if first player, play any card, remember it in the lead spot, and end your turn
 63                ((== (game str LEAD) NONE)
 64                  (any ((current player) iloc HAND) 'AC
 65                      (do
 66                          (
 67                            (move 'AC
 68                                  (top ((current player) vloc TRICK)))
 69                            (set (game str LEAD)
 70                                (cardatt SUIT (top ((current player) vloc TRICK)))))))))))
 71
 72        ;; after players play hand, computer wraps up trick
 73        (do (
 74            ;; solidfy card recedence
 75            (set (game points PRECEDENCE)
 76                  (
 77                  ((SUIT : (game str LEAD)) 100)
 78                  ((RANK : ACE) 14)
 79                  ((RANK : TEN) 10)
 80                  ((RANK : NINE) 9)
 81                  ((RANK : EIGHT) 8)
 82                  ((RANK : SEVEN) 7)
 83                  ((RANK : SIX) 6)
 84                  ((RANK : FIVE) 5)
 85                  ((RANK : FOUR) 4)
 86                  ((RANK : THREE) 3)))
 87
 88            ;; determine who won the hand, set them first next time
 89            (cycle next (owner (max (union (all player 'P ('P vloc TRICK))) using (game points PRECEDENCE))))
 90
 91            (all player 'P
 92                  (move (top ('P vloc TRICK))
 93                        (top (game vloc DISCARD))))
 94            (set (game str LEAD) NONE)
 95
 96            ;; if that was the last round, give the winner a point
 97            ((all player 'P
 98                  (== (size ('P iloc HAND)) 0))
 99              (inc ((next player) sto SCORE) 1)))))
100
101(scoring max ((current player) sto SCORE)))