functional programming - Haskell- War card Game -
one of rules i'm trying program is:
when 2 players play cards of equal rank, triggers “war”. each player sets down 3 cards, , flips 1 more card. whoever wins between these last 2 flipped cards gets take cards in round, including 3 cards set down each player. if flipped cards match again, war triggered, resolved in same way.
i can implement first round of war inside code, part stuck when second round of war initiated when 2 players same card again. trouble having when second round of war initiated, 2 players put aside cards first war , whoever whens second war gets cards second war , first war. don't know understand how make war function store first round of cards, can see in otherwise dropped cards first round.
war :: ([int], [int]) -> ([int], [int]) war (x:xs,y:ys) | head(drop 3 xs) > head(drop 3 (ys)) = (drop 4 (xs) ++ player1pot (x:xs) ++ player2pot (y:ys), drop 4 (ys)) | head(drop 3 xs) < head(drop 3 (ys)) = (drop 4 (xs), drop 4 (ys) ++ player2pot (y:ys) ++ player1pot (x:xs)) | otherwise = war (drop 4 (xs),drop 4 (ys)) standardround :: ([int], [int]) -> ([int], [int]) standardround ([],y:ys) = ([],y:ys) standardround (x:xs,[]) = (x:xs,[]) standardround (x:xs,y:ys) | x > y = (xs ++ [y] ++ [x], ys) | y > x = (xs, ys ++ [x] ++ [y]) | otherwise = war (x:xs,y:ys)
you can use let ... in
extract values of tuple
| otherwise = let (newxs,newys) = war (drop 4 xs, drop 4 ys) in (take 3 xs ++ newxs, take 3 ys ++ newys)
so can returned modified version. add dropped cards before or after, like.
Comments
Post a Comment