From 6dbedddb7b720180086ae549dd4a167bcb3060f8 Mon Sep 17 00:00:00 2001 From: Tibo De Peuter Date: Tue, 15 Nov 2022 17:05:06 +0100 Subject: [PATCH] #1 Handle all inputs --- lib/PatienceBoard.hs | 65 ++++++++++++++++---------------------------- 1 file changed, 24 insertions(+), 41 deletions(-) diff --git a/lib/PatienceBoard.hs b/lib/PatienceBoard.hs index 54edbca..c33d1f6 100644 --- a/lib/PatienceBoard.hs +++ b/lib/PatienceBoard.hs @@ -115,7 +115,9 @@ isInGame (x, y) g = horizontalCheck && verticalCheck leftBound = fst gameStacksCoord <= x rightBound = x < amountOfGameStacks 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. getZoneFromCoord :: Game -> Coordinate -> Zone @@ -155,50 +157,31 @@ rotatePile g@Game{ board = b } = g{ board = rotatedBoard } (head, tail) = splitAt rotateStep $ pile b -- Check if a card can be placed ontop of a gameStack. -canPlayOn :: Card -> Stack -> Bool -canPlayOn (_,King,_) [] = True -canPlayOn (t1,v1,_) ((t2,v2,_):cs) = differentColor && predValue - where differentColor = t1 /= t2 && (fromEnum t1 + fromEnum t2) `elem` [1,2,4,5] +canPlayOn :: Stack -> Int -> Stack -> Bool +canPlayOn [] _ _ = False +canPlayOn cs index [] = v1 == King && vis == Visible + 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 -canPlayOn _ _ = False + visibility = vis == Visible -- Check if a card can be played ontop of an EndingStack. -canFinishOn :: Card -> Stack -> Bool -canFinishOn (_,Ace,_) [] = True -canFinishOn (t1,v1,_) ((t2,v2,_):cs) = sameType && succValue - where sameType = t1 == t2 +canFinishOn :: Stack -> Int -> Stack -> Bool +canFinishOn [] _ _ = False +canFinishOn cs index [] = v1 == Ace && vis == Visible + 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 -canFinishOn _ _ = False - --- 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 + visibility = vis == Visible -- Move from one gameStack to another. moveGS2GS :: Coordinate -> Int -> Board -> Board moveGS2GS fromCoord toStackNr board - | canPlayOn (from !! (index - 1)) to = newBoard + | canPlayOn from (index - 1) to = newBoard | otherwise = board where (fromStackNr, negIndex) = fromCoord fromAmount = length from @@ -214,7 +197,7 @@ moveGS2GS fromCoord toStackNr board moveGS2ES :: Coordinate -> Int -> Board -> Board moveGS2ES fromCoord toIndex board - | canFinishOn (head from) to = newBoard + | canFinishOn from 0 to = newBoard | otherwise = board where (fromIndex, _) = fromCoord oldGS = gameStacks board @@ -230,7 +213,7 @@ moveGS2ES fromCoord toIndex board -- Move a card between pile and endingStacks. moveP2ES :: Coordinate -> Int -> Board -> Board moveP2ES _ toIndex board - | canFinishOn (head oldPile) to = newBoard + | canFinishOn oldPile 0 to = newBoard | otherwise = board where oldPile = pile board oldES = endingStacks board @@ -243,7 +226,7 @@ moveP2ES _ toIndex board -- Move a card between pile and gameStacks. moveP2GS :: Coordinate -> Int -> Board -> Board moveP2GS _ toStackNr board - | canPlayOn (head oldPile) to = newBoard + | canPlayOn oldPile 0 to = newBoard | otherwise = board where oldPile = pile board oldGS = gameStacks board @@ -255,7 +238,7 @@ moveP2GS _ toStackNr board moveES2GS :: Coordinate -> Int -> Board -> Board moveES2GS fromCoord toStackNr board - | canPlayOn (head from) to = newBoard + | canPlayOn from 0 to = newBoard | otherwise = board where (tempIndex, _) = fromCoord fromIndex = tempIndex - (amountOfGameStacks - amountOfEndingStacks)