2018-03-10 23:25:44 +01:00
|
|
|
{-# LANGUAGE NamedFieldPuns #-}
|
|
|
|
module Hanafuda.KoiKoi (
|
2018-03-15 22:32:24 +01:00
|
|
|
Card(..)
|
|
|
|
, Game(..)
|
2018-03-10 23:25:44 +01:00
|
|
|
, Mode(..)
|
|
|
|
, Move(..)
|
2018-03-15 22:32:24 +01:00
|
|
|
, On(..)
|
2018-03-10 23:25:44 +01:00
|
|
|
, Over(..)
|
2018-03-15 22:32:24 +01:00
|
|
|
, new
|
|
|
|
, play
|
2018-03-10 23:25:44 +01:00
|
|
|
) where
|
|
|
|
|
2018-03-15 22:32:24 +01:00
|
|
|
import Hanafuda (Card(..), Flower(Pine), contains, flower, match, remove)
|
|
|
|
import qualified Hanafuda.Player as Player (deal)
|
|
|
|
import Hanafuda.KoiKoi.Game (Game(..), Mode(..), Move(..), On(..), Over(..), Step(..), raise)
|
|
|
|
import qualified Hanafuda.KoiKoi.Round as Round (deal, next)
|
|
|
|
import qualified Hanafuda.KoiKoi.Turn as Turn (catch, end, next)
|
|
|
|
import System.Random (randomIO)
|
|
|
|
|
|
|
|
play :: Move -> On -> IO Game
|
|
|
|
play move on@(On_ {river, step, trick}) =
|
|
|
|
case (step, move) of
|
|
|
|
(ToPlay, Play card) ->
|
|
|
|
either raise (Turn.catch on card) $ match card river
|
|
|
|
(ToPlay, Capture (card, caught)) ->
|
|
|
|
if card `canCatch` caught
|
|
|
|
then Turn.catch on card (remove river caught, [card, caught])
|
|
|
|
else raise "You can't choose that card"
|
|
|
|
(Turned card, Choose caught) ->
|
|
|
|
if card `canCatch` caught
|
|
|
|
then Turn.end on (remove river caught, [card, caught])
|
|
|
|
else raise "You can't choose that card"
|
|
|
|
(Scored, KoiKoi keepOn) -> (if keepOn then Turn.next else Round.next) on
|
|
|
|
(_, _) -> raise "You can't play this move in that state"
|
|
|
|
where
|
|
|
|
canCatch card1 card2 = flower card1 == flower card2 && river `contains` card2
|
2018-03-10 23:25:44 +01:00
|
|
|
|
|
|
|
new :: Mode -> IO On
|
|
|
|
new mode = do
|
2018-03-15 22:32:24 +01:00
|
|
|
playing <- randomIO
|
|
|
|
Round.deal $ On_ {
|
|
|
|
mode
|
|
|
|
, scores = Player.deal [0, 0]
|
|
|
|
, month = Pine
|
|
|
|
, players = undefined
|
|
|
|
, playing
|
|
|
|
, winning = playing
|
|
|
|
, oyake = playing
|
|
|
|
, stock = undefined
|
|
|
|
, river = undefined
|
|
|
|
, step = ToPlay
|
|
|
|
, trick = []
|
|
|
|
}
|