-
Notifications
You must be signed in to change notification settings - Fork 0
/
deck.hs
131 lines (88 loc) · 3.42 KB
/
deck.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import Data.Bits
import Data.List
import Data.Maybe
import Control.Monad
import System.Random
import System.Random.Shuffle
colors = ["Karo", "Herz", "Pik", "Kreuz"]
symbols = ["Ass", "Koenig", "Dame", "Bube", "10", "9", "8", "7"]
deckSize = (length colors) * (length symbols)
data Game = Game { deck :: [Int]
, hands :: [[Int]]
, players :: [Int]
, activePlayer :: Int
} deriving (Show)
-- TODO Deckgröße abhängig von Handgröße und Spielerzahl, MauMau-Regeln
main :: IO()
main = do
putStrLn "Welcome to MauMau!"
putStrLn " "
putStrLn "How many players?"
num_players <- getLine
putStrLn " "
putStrLn "How many cards per hand?"
num_hand <- getLine
putStrLn " "
putStrLn ("Generating deck for " ++ (show num_players) ++ " players")
putStrLn "Shuffling deck"
g <- getStdGen
ps <- return ([0..((read num_players)-1)])
game <- return (Game (shuffle' buildDeck deckSize g) [[]] ps 0)
mapM putStrLn (decode (deck game))
putStrLn " "
putStrLn "Preparing players hands"
game <- return (Game (reduceDeck (read num_players) (read num_hand) (deck game)) (takeHand (read num_players) (read num_hand) (deck game) (hands game)) (players game) 0)
putStrLn " "
putStrLn " "
putStrLn "Starting game with 2 players:"
turn 0 game
-- Make turns, take hands and cards, game loop in "turn"
turn :: Int -> Game -> IO ()
turn p game = do
putStrLn ("Turn of player number " ++ show p)
game <- return (takeCard p game)
if length (deck game) == 0 then putStrLn ("Game over, winning player is " ++ show p)
else turn (nextPlayer p game) game
printHands :: Int -> Game -> IO ()
printHands n game = do
mapM putStrLn (decode ((hands game) !! (n-1)))
putStrLn " "
when ((n-1) > 0) $ printHands (n-1) game
takeCard :: Int -> Game -> Game
takeCard p game = Game (reduceDeck 1 1 (deck game)) (takeFrom p game) (players game) p
takeHand :: Int -> Int -> [Int] -> [[Int]] -> [[Int]]
takeHand p n deck hands = [ take n (drop hand_length deck) ++ [] | hand_length <- [0,n..((p-1)*n)]]
takeFrom :: Int -> Game -> [[Int]]
takeFrom p game = [ if fromJust (elemIndex hand (hands game)) == p then head (take 1 (deck game)) : hand else hand | hand <- (hands game)]
reduceDeck :: Int -> Int -> [Int] -> [Int]
reduceDeck p n deck = drop (p * n) deck
nextPlayer :: Int -> Game -> Int
nextPlayer p game
| p == lastPlayer = 0
| otherwise = p + 1
where
lastPlayer = (length (players game)) - 1
-- Deck creation and string representation
buildDeck :: [Int]
buildDeck = [ encode "color" color .|. encode "symbol" symbol | color <- [0..((length colors) - 1)], symbol <- [0..((length symbols) - 1)]]
decode :: [Int] -> [String]
decode deck = [ decode' x | x <- deck]
-- Decoding
decode' :: Int -> String
decode' n = colors!!a ++ " " ++ symbols!!b
where
a = shiftR (n .&. 49152) 14 -- bit 15,16 to 1
b = findSetBit (n .&. 16383) -- first 14 bits to 1
findSetBit :: Int -> Int
findSetBit n = fromJust (elemIndex n (1 : (take 13 (powers 2))))
powers :: Int -> [Int]
powers n = n : map (* n) (powers n)
-- Encoding
encode :: String -> Int -> Int
encode t v
| t == "color" = encodeColor v
| t == "symbol" = encodeSymbol v
encodeColor :: Int -> Int
encodeColor n = shift n 14
encodeSymbol :: Int -> Int
encodeSymbol n = shift 1 n