Skip to content

Instantly share code, notes, and snippets.

@decrn
Last active December 5, 2020 20:06
Show Gist options
  • Save decrn/634d720f27cb451927f7fefe4989c1cf to your computer and use it in GitHub Desktop.
Save decrn/634d720f27cb451927f7fefe4989c1cf to your computer and use it in GitHub Desktop.
data ValidatedCommand = RegularMove Char Char String Char
| WinningMove Char Char
| InvalidCommand
deriving (Show, Eq)
validateCommand :: Command -> ValidatedCommand
validateCommand (row:col:',':rot)
| length rot >= 2 &&
row `elem` "abcdef" &&
col `elem` "123456" &&
init rot `elem` ["I", "II", "III", "IV"] &&
last rot `elem` "lr"
= RegularMove row col (init rot) (last rot)
| otherwise = InvalidCommand
validateCommand [row, col]
| row `elem` "abcdef" &&
col `elem` "123456"
= WinningMove row col
| otherwise
= InvalidCommand
validateCommand _ = InvalidCommand
step :: State -> Command -> (Maybe Message, Maybe State)
step s c = playMove s vc where vc = validateCommand c
playMove :: State -> ValidatedCommand -> (Maybe Message, Maybe State)
playMove s (RegularMove row column quadrant rotation) = undefined
playMove s (WinningMove row column) = undefined
playMove s InvalidCommand =
(Just "Invalid command! Must adhere to [a-f][1-6](,[I-IV](r|l))?", Just s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment