Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 2 files changed
  • 0 comments
  • 1 contributor

Showing 2 changed files with 56 additions and 6 deletions. Show diff stats Hide diff stats

  1. +6 6 src/Plays.hs
  2. +50 0 src/Rules.hs
12 src/Plays.hs
... ... @@ -1,11 +1,13 @@
1 1 module Plays (
2   - -- | Sets of cards that can be played (note that data constructors are private)
3   - Play,
  2 + -- | Sets of cards that can be played. Do not construct Plays manually; use
  3 + -- allPlays and filter.
  4 + Play(..),
4 5 allPlays,
5 6
6 7 -- | Utility functions for interacting with Plays
7 8 playLength,
8 9 unwrapPlay,
  10 + isBomb,
9 11 ) where
10 12
11 13 import Cards
@@ -26,8 +28,7 @@ type Plays = Set Play
26 28
27 29 -- | Represents a Tichu play. (Not to be confused with Google Play.)
28 30 data Play =
29   - Pass -- ^ Play no cards
30   - | Single Card -- ^ Play a single card
  31 + Single Card -- ^ Play a single card
31 32 | Multiple Cards -- ^ Play 2, 3, or 4 cards
32 33 | Straight Cards -- ^ Play a 5-14 card straight
33 34 | FullHouse Cards Cards -- ^ Play a full house (high three, low two)
@@ -63,12 +64,11 @@ playLength (Single _) = 1
63 64 playLength (Multiple xs) = Set.size xs
64 65 playLength (Straight xs) = Set.size xs
65 66 playLength (FullHouse _ _) = 5
66   -playLength (ConsPairs xs) = 2 * length xs
  67 +playLength (ConsPairs xs) = length xs
67 68
68 69 _showCards = List.intercalate " " . (show <$>) . Set.toAscList
69 70
70 71 instance Show Play where
71   - show Pass = "<Pass>"
72 72 show (Single x) = "<" ++ show x ++ ">"
73 73 show p@(Multiple x) = "<" ++ bomb ++ content ++ ">"
74 74 where bomb = if isBomb p then "!" else ""
50 src/Rules.hs
... ... @@ -0,0 +1,50 @@
  1 +module Rules (
  2 + isLegalPlay,
  3 + ) where
  4 +
  5 +import Cards
  6 +import Plays
  7 +
  8 +-- | Given two plays, return true if the second play can be played over the first.
  9 +isLegalPlay :: Play -> Play -> Bool
  10 +
  11 +-- Non-bombs can always be bombed
  12 +isLegalPlay x y | (not . isBomb) x && isBomb y = True
  13 +
  14 +-- Longer bombs beat bombs
  15 +isLegalPlay x y | isBomb x && isBomb y && playLength x < playLength y = True
  16 +
  17 +-- Mahjong and Dog can only be lead
  18 +isLegalPlay _ (Single Mahjong) = False
  19 +isLegalPlay _ (Single Dog) = False
  20 +
  21 +-- Excepting bombs, nothing beats the Dragon
  22 +isLegalPlay (Single Dragon) _ = False
  23 +
  24 +-- The Phoenix beats everything
  25 +isLegalPlay _ (Single Phoenix) = True
  26 +
  27 +-- But the Phoenix is beaten by everything
  28 +isLegalPlay (Single Phoenix) _ = True
  29 +
  30 +-- Singles increase in rank
  31 +isLegalPlay x@(Single _) y@(Single _) = rank x < rank y
  32 +
  33 +-- Multiples increase in rank but stay the same size
  34 +isLegalPlay x@(Multiple _) y@(Multiple _) =
  35 + rank x < rank y && playLength x == playLength y
  36 +
  37 +-- Full houses increase in rank
  38 +isLegalPlay x@(FullHouse _ _) y@(FullHouse _ _) = rank x < rank y
  39 +
  40 +-- Straights increase in rank, but can be bombed by longer bombs if they are
  41 +-- bombs.
  42 +isLegalPlay x@(Straight _) y@(Straight _) | isBomb x && isBomb y =
  43 + rank x < rank y || playLength x < playLength y
  44 +isLegalPlay x@(Straight _) y@(Straight _) = rank x < rank y
  45 +
  46 +-- Cons pairs increase in rank but stay the same size
  47 +isLegalPlay x@(ConsPairs _) y@(ConsPairs _) =
  48 + rank x < rank y && playLength x == playLength y
  49 +
  50 +isLegalPlay _ _ = False

No commit comments for this range

Something went wrong with that request. Please try again.