GoUI/Go/Game.elm

35 lines
1.2 KiB
Elm

module Go.Game exposing (verify)
import Go.Types exposing (..)
import Go.Util exposing (lookup)
import List exposing (foldl)
import Maybe exposing (andThen)
-- Make sure it's our turn to play.
verifyTurn : Model -> Cell -> Maybe Cell
verifyTurn model c = if
(Just model.sessionColor) == model.currentColor then
Just c
else
Nothing
-- Make sure there's not already a piece where we're going.
verifyClear : Model -> Cell -> Maybe Cell
verifyClear model (indx, c) = case lookup indx model.board of
Just _ -> Nothing
Nothing -> Just (indx, c)
-- Make sure cell is in range of the board.
verifyBounds : Model -> Cell -> Maybe Cell
verifyBounds model ((x, y), c) = if x >= 0 && x < model.sessionSize && y >= 0 && y < model.sessionSize
then Just ((x, y), c)
else Nothing
-- Verify a cell placemenet using a list of verification functions.
verifyAll : Cell -> List (Model -> Cell -> Maybe Cell) -> Model -> Maybe Cell
verifyAll cell funcs model = List.foldl (\a b -> andThen (a model) b) (Just cell) funcs
-- Make sure that a move to the given cell can be made.
verify : Cell -> Model -> Maybe Cell
verify cell model = verifyAll cell [verifyBounds, verifyTurn, verifyClear] model