1
Fork 0

#1 Handle all inputs

This commit is contained in:
Tibo De Peuter 2022-11-15 17:05:06 +01:00
parent df3b6ae092
commit 6dbedddb7b

View file

@ -115,7 +115,9 @@ isInGame (x, y) g = horizontalCheck && verticalCheck
leftBound = fst gameStacksCoord <= x leftBound = fst gameStacksCoord <= x
rightBound = x < amountOfGameStacks rightBound = x < amountOfGameStacks
upBound = y <= snd gameStacksCoord upBound = y <= snd gameStacksCoord
downBound = negate y < length (gameStacks (board g) !! x) xStack = gameStacks (board g) !! x
downBound = zero || negate y < length xStack
zero = y == 0 && length xStack == 0
-- Get the zone number from a coordinate. -- Get the zone number from a coordinate.
getZoneFromCoord :: Game -> Coordinate -> Zone getZoneFromCoord :: Game -> Coordinate -> Zone
@ -155,50 +157,31 @@ rotatePile g@Game{ board = b } = g{ board = rotatedBoard }
(head, tail) = splitAt rotateStep $ pile b (head, tail) = splitAt rotateStep $ pile b
-- Check if a card can be placed ontop of a gameStack. -- Check if a card can be placed ontop of a gameStack.
canPlayOn :: Card -> Stack -> Bool canPlayOn :: Stack -> Int -> Stack -> Bool
canPlayOn (_,King,_) [] = True canPlayOn [] _ _ = False
canPlayOn (t1,v1,_) ((t2,v2,_):cs) = differentColor && predValue canPlayOn cs index [] = v1 == King && vis == Visible
where differentColor = t1 /= t2 && (fromEnum t1 + fromEnum t2) `elem` [1,2,4,5] where (_,v1,vis) = cs !! index
canPlayOn cs index ((t2,v2,_):_) = differentColor && predValue && visibility
where (t1,v1,vis) = cs !! index
differentColor = t1 /= t2 && (fromEnum t1 + fromEnum t2) `elem` [1,2,4,5]
predValue = succ v1 == v2 predValue = succ v1 == v2
canPlayOn _ _ = False visibility = vis == Visible
-- Check if a card can be played ontop of an EndingStack. -- Check if a card can be played ontop of an EndingStack.
canFinishOn :: Card -> Stack -> Bool canFinishOn :: Stack -> Int -> Stack -> Bool
canFinishOn (_,Ace,_) [] = True canFinishOn [] _ _ = False
canFinishOn (t1,v1,_) ((t2,v2,_):cs) = sameType && succValue canFinishOn cs index [] = v1 == Ace && vis == Visible
where sameType = t1 == t2 where (_,v1,vis) = cs !! index
canFinishOn cs index ((t2,v2,_):_) = sameType && succValue && visibility
where (t1,v1,vis) = cs !! index
sameType = t1 == t2
succValue = v1 == succ v2 succValue = v1 == succ v2
canFinishOn _ _ = False visibility = vis == Visible
-- Move a card to a GameStack. Move all the cards below the given card
-- on the 'from' stack as well.
moveBetweenGS :: Int -> Stack -> Stack -> (Stack,Stack)
moveBetweenGS index from to
| canPlayOn (from !! index) to = (showFirst removed, added)
| otherwise = (from,to)
where (diff,removed) = splitAt (index + 1) from
added = diff ++ to
-- Move a card to an EndingStack. This can only be a single card at once.
moveToES :: Int -> Stack -> Stack -> (Stack,Stack)
moveToES _ from to
| canFinishOn (head from) to = (showFirst removed, added)
| otherwise = (from,to)
where (diff,removed) = splitAt 1 from
added = diff ++ to
-- Move from an EndingStack to GameStack.
moveESToGS :: Int -> Stack -> Stack -> (Stack,Stack)
moveESToGS _ from to
| canPlayOn (head from) to = (cs, added)
| otherwise = (from, to)
where (c:cs) = from
added = c:to
-- Move from one gameStack to another. -- Move from one gameStack to another.
moveGS2GS :: Coordinate -> Int -> Board -> Board moveGS2GS :: Coordinate -> Int -> Board -> Board
moveGS2GS fromCoord toStackNr board moveGS2GS fromCoord toStackNr board
| canPlayOn (from !! (index - 1)) to = newBoard | canPlayOn from (index - 1) to = newBoard
| otherwise = board | otherwise = board
where (fromStackNr, negIndex) = fromCoord where (fromStackNr, negIndex) = fromCoord
fromAmount = length from fromAmount = length from
@ -214,7 +197,7 @@ moveGS2GS fromCoord toStackNr board
moveGS2ES :: Coordinate -> Int -> Board -> Board moveGS2ES :: Coordinate -> Int -> Board -> Board
moveGS2ES fromCoord toIndex board moveGS2ES fromCoord toIndex board
| canFinishOn (head from) to = newBoard | canFinishOn from 0 to = newBoard
| otherwise = board | otherwise = board
where (fromIndex, _) = fromCoord where (fromIndex, _) = fromCoord
oldGS = gameStacks board oldGS = gameStacks board
@ -230,7 +213,7 @@ moveGS2ES fromCoord toIndex board
-- Move a card between pile and endingStacks. -- Move a card between pile and endingStacks.
moveP2ES :: Coordinate -> Int -> Board -> Board moveP2ES :: Coordinate -> Int -> Board -> Board
moveP2ES _ toIndex board moveP2ES _ toIndex board
| canFinishOn (head oldPile) to = newBoard | canFinishOn oldPile 0 to = newBoard
| otherwise = board | otherwise = board
where oldPile = pile board where oldPile = pile board
oldES = endingStacks board oldES = endingStacks board
@ -243,7 +226,7 @@ moveP2ES _ toIndex board
-- Move a card between pile and gameStacks. -- Move a card between pile and gameStacks.
moveP2GS :: Coordinate -> Int -> Board -> Board moveP2GS :: Coordinate -> Int -> Board -> Board
moveP2GS _ toStackNr board moveP2GS _ toStackNr board
| canPlayOn (head oldPile) to = newBoard | canPlayOn oldPile 0 to = newBoard
| otherwise = board | otherwise = board
where oldPile = pile board where oldPile = pile board
oldGS = gameStacks board oldGS = gameStacks board
@ -255,7 +238,7 @@ moveP2GS _ toStackNr board
moveES2GS :: Coordinate -> Int -> Board -> Board moveES2GS :: Coordinate -> Int -> Board -> Board
moveES2GS fromCoord toStackNr board moveES2GS fromCoord toStackNr board
| canPlayOn (head from) to = newBoard | canPlayOn from 0 to = newBoard
| otherwise = board | otherwise = board
where (tempIndex, _) = fromCoord where (tempIndex, _) = fromCoord
fromIndex = tempIndex - (amountOfGameStacks - amountOfEndingStacks) fromIndex = tempIndex - (amountOfGameStacks - amountOfEndingStacks)