Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: jrockway/haskell-tichu
base: 3571be6c86
...
head fork: jrockway/haskell-tichu
compare: 6feecead51
  • 3 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 56 additions and 6 deletions.
  1. +6 −6 src/Plays.hs
  2. +50 −0 src/Rules.hs
View
12 src/Plays.hs
@@ -1,11 +1,13 @@
module Plays (
- -- | Sets of cards that can be played (note that data constructors are private)
- Play,
+ -- | Sets of cards that can be played. Do not construct Plays manually; use
+ -- allPlays and filter.
+ Play(..),
allPlays,
-- | Utility functions for interacting with Plays
playLength,
unwrapPlay,
+ isBomb,
) where
import Cards
@@ -26,8 +28,7 @@ type Plays = Set Play
-- | Represents a Tichu play. (Not to be confused with Google Play.)
data Play =
- Pass -- ^ Play no cards
- | Single Card -- ^ Play a single card
+ Single Card -- ^ Play a single card
| Multiple Cards -- ^ Play 2, 3, or 4 cards
| Straight Cards -- ^ Play a 5-14 card straight
| FullHouse Cards Cards -- ^ Play a full house (high three, low two)
@@ -63,12 +64,11 @@ playLength (Single _) = 1
playLength (Multiple xs) = Set.size xs
playLength (Straight xs) = Set.size xs
playLength (FullHouse _ _) = 5
-playLength (ConsPairs xs) = 2 * length xs
+playLength (ConsPairs xs) = length xs
_showCards = List.intercalate " " . (show <$>) . Set.toAscList
instance Show Play where
- show Pass = "<Pass>"
show (Single x) = "<" ++ show x ++ ">"
show p@(Multiple x) = "<" ++ bomb ++ content ++ ">"
where bomb = if isBomb p then "!" else ""
View
50 src/Rules.hs
@@ -0,0 +1,50 @@
+module Rules (
+ isLegalPlay,
+ ) where
+
+import Cards
+import Plays
+
+-- | Given two plays, return true if the second play can be played over the first.
+isLegalPlay :: Play -> Play -> Bool
+
+-- Non-bombs can always be bombed
+isLegalPlay x y | (not . isBomb) x && isBomb y = True
+
+-- Longer bombs beat bombs
+isLegalPlay x y | isBomb x && isBomb y && playLength x < playLength y = True
+
+-- Mahjong and Dog can only be lead
+isLegalPlay _ (Single Mahjong) = False
+isLegalPlay _ (Single Dog) = False
+
+-- Excepting bombs, nothing beats the Dragon
+isLegalPlay (Single Dragon) _ = False
+
+-- The Phoenix beats everything
+isLegalPlay _ (Single Phoenix) = True
+
+-- But the Phoenix is beaten by everything
+isLegalPlay (Single Phoenix) _ = True
+
+-- Singles increase in rank
+isLegalPlay x@(Single _) y@(Single _) = rank x < rank y
+
+-- Multiples increase in rank but stay the same size
+isLegalPlay x@(Multiple _) y@(Multiple _) =
+ rank x < rank y && playLength x == playLength y
+
+-- Full houses increase in rank
+isLegalPlay x@(FullHouse _ _) y@(FullHouse _ _) = rank x < rank y
+
+-- Straights increase in rank, but can be bombed by longer bombs if they are
+-- bombs.
+isLegalPlay x@(Straight _) y@(Straight _) | isBomb x && isBomb y =
+ rank x < rank y || playLength x < playLength y
+isLegalPlay x@(Straight _) y@(Straight _) = rank x < rank y
+
+-- Cons pairs increase in rank but stay the same size
+isLegalPlay x@(ConsPairs _) y@(ConsPairs _) =
+ rank x < rank y && playLength x == playLength y
+
+isLegalPlay _ _ = False

No commit comments for this range

Something went wrong with that request. Please try again.