summaryrefslogtreecommitdiff
path: root/src/Main.hs
blob: 653b399fdecb434ebc7aaf015afed792528ca5dd (plain)
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
-- This is the default main
module Main where

import Mahjong.Set
import Mahjong.Hand
import Mahjong.Tile
import System.Random
import Data.Array.IO
import Control.Monad
import Control.Monad.Loops

allTiles = concat $ take 4 $ repeat orderedTile
shuffle :: [a] -> IO [a]
shuffle xs = do
        ar <- newArray n xs
        forM [1..n] $ \i -> do
            j <- randomRIO (i,n)
            vi <- readArray ar i
            vj <- readArray ar j
            writeArray ar j vi
            return vj
    where
        n = length xs
        newArray :: Int -> [a] -> IO (IOArray Int a)
        newArray n xs =  newListArray (1,n) xs

main = do
    t <- shuffle allTiles
    let h = take 13 t
    let r = drop 13 t
    choose (Hand h []) r

choose :: Hand -> MSet -> IO ()
choose h r = do
    let (n:rr) = r
    let hand = getHand h
    let call = getCall h
    let nh = hand++[n]
    let l = listen $ Hand nh call
    putStrLn $ (showMSet hand) ++ ":" ++ (show n)
    putStrLn $ (show l) ++ "   "++ (show $ map showMSet $ call)
    putStrLn $ show $ nextTileSet $ Hand nh call
    if l == 0 then putStrLn "End"
    else do
        c <- iterateWhile (\c -> not $ c `elem` nh) $ do
            i <- getLine
            let c = head $ readMSet i
            return c
        let nnh = nh `rmTile` c
        choose (Hand nnh call) rr