Skip to content

Instantly share code, notes, and snippets.

@vstrimaitis
Last active September 17, 2017 19:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vstrimaitis/cb41f505dc16fa85457a7a373f4d80bf to your computer and use it in GitHub Desktop.
Save vstrimaitis/cb41f505dc16fa85457a7a373f4d80bf to your computer and use it in GitHub Desktop.
fu funkcija
validateMoveOrder :: [Move] -> Maybe String
validateMoveOrder [] = Nothing
validateMoveOrder [_] = Nothing
validateMoveOrder l = validateCurrentMove 0 [] l turns
where
contains :: Eq a => [a] -> a -> Bool
contains [] _ = False
contains (x:xs) el = (x == el) || contains xs el
showSquareTakenMsg :: Int -> String
showSquareTakenMsg x =
"Move #" ++ show (x + 1) ++ " is invalid - the square is already taken!"
showWrongNameMsg :: Int -> String -> String -> String
showWrongNameMsg x exp act =
"Wrong player name on move #" ++
show (x + 1) ++ ". Expected '" ++ exp ++ "', got '" ++ act ++ "'."
showWrongPieceMsg :: Int -> Piece -> Piece -> String
showWrongPieceMsg x exp act =
"Wrong piece on move #" ++
show (x + 1) ++
". Expected '" ++ show exp ++ "', got '" ++ show act ++ "'."
xName :: String
oName :: String
(xName, oName) = extractPlayerNames l
turns :: [(String, Piece)]
turns = (xName, X) : (oName, O) : turns
validateCurrentMove ::
Int -> [(Int, Int)] -> [Move] -> [(String, Piece)] -> Maybe String
validateCurrentMove _ _ [] _ = Nothing
validateCurrentMove currMove c (x:xs) ((expName, expPiece):ts)
| contains c (coords x) = Just (showSquareTakenMsg currMove)
| otherwise =
if expName ~== player x
then if expPiece == piece x
then validateCurrentMove
(currMove + 1)
(coords x : c)
xs
ts
else Just
(showWrongPieceMsg currMove expPiece (piece x))
else Just (showWrongNameMsg currMove expName (player x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment