1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
module Mahjong.Tile where
import Data.Char (toUpper)
import Data.List (sort)
import Data.Maybe
import Control.Monad
data MCol = Man | Pin | Sou | Cha
deriving (Eq,Ord)
data MTile = MTile MCol Int
deriving (Eq,Ord)
charMcol = [(Man,'M'),(Pin,'P'),(Sou,'S'),(Cha,'C')]
instance Show MCol where
show x = [snd a | a <-charMcol, fst a == x]
instance Read MCol where
readsPrec p r = [(fst a,t)|(c,t) <- lex r, a <- charMcol, map toUpper c == [snd a]]
instance Show MTile where
show (MTile a b) = show b ++ show a
instance Read MTile where
readsPrec p r = [ (MTile col n,u) | (c1,t)<- lex r,
let n = (read c1)::Int,
(c2,u) <-lex t,
let col=(read c2)::MCol]
isCha c@(MTile col n) = (col == Cha)
isLaoTou c@(MTile col n) = ((n == 1) || (n == 9)) && (not $ isCha c)
is19 c@(MTile col n) = (isCha c) || (isLaoTou c)
orderedTile = sort $ [MTile c n | c<-[Man,Pin,Sou], n <-[1..9]] ++ (map (MTile Cha) [1..7])
nextTile' p tiles = if xs /= [] then Just $ head xs
else Nothing
where (x:xs) = dropWhile (/= p) tiles
nextTile :: Maybe MTile -> Maybe MTile
nextTile jp = do
p@(MTile c n) <- jp
guard ((c /= Cha) && (n /= 9))
nx <- nextTile' p orderedTile
return nx
prevTile jp = do
p@(MTile c n) <- jp
guard ((c /= Cha) && (n /= 1))
nx <- nextTile' p $ reverse orderedTile
return nx
|