-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlay.hs
More file actions
361 lines (324 loc) · 11.7 KB
/
Play.hs
File metadata and controls
361 lines (324 loc) · 11.7 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
-- CPSC 312 PROJECT 2 GO FISH --
-- Authors: Nafisa Shazia 18769142
-- John Jang 52043122
--
-- This is class is responsible for actual game play of GoFish
-- To run, in ghci
--
-- :l Play
-- goFishStart
module Play where
import GoFish -- Actual GoFish game states
import System.IO -- For basic I/O
import Text.Read -- For reading/parsing inputs
import Data.Array.IO -- For randomness (shuffling cards)
import System.Random -- For randomness (shuffling cards, ai moves)
import Control.Monad
--type GameInfo = (AllPlayers, [Int]) --0th index is for 0th player's score
type GameInfo = ([Bool],[Int])
-- Actual start of the game. It will set all number of players/ai and new set of deck
goFishStart :: IO GameInfo
goFishStart = let
deck = [(x,y)|x<-[Two ..], y<-[Club ..]] in -- List of all decks
do
putStrLn ("Welcome to GoFish in Haskell!")
numOfPlayers <- readInt ("Enter number of human players (1~4):")
if numOfPlayers == 1 then
do
numOfAI <- readInt ("How many of AI do you want? (0~" ++ show (4-numOfPlayers) ++ ")" )
if(numOfAI < 4) then
do
deckS <- shuffle deck
if numOfAI == 1 then
do
play ( (start (2+numOfAI) (7-numOfAI) deckS),True) (makeGameInfo numOfAI ([False, True],[]))
else if numOfAI == 2 then
do
play ( (start (2+numOfAI) (7-numOfAI) deckS),True) (makeGameInfo numOfAI ([False,True,True],[]))
else
do
play ( (start (2+numOfAI) (7-numOfAI) deckS),True) (makeGameInfo numOfAI ([False,True,True,True],[]))
else
do
putStrLn ("Not a valid range")
goFishStart
else if numOfPlayers == 2 then
do
numOfAI <- readInt ("How many of AI do you want? (0~" ++ show (4-numOfPlayers) ++ ")" )
if(numOfAI < 3) then
do
if numOfAI == 1 then
do
deckS <- shuffle deck
play ( (start (2+numOfAI) (7-numOfAI) deckS),True) (makeGameInfo numOfAI ([False,False,True],[]))
else
do
deckS <- shuffle deck
play ( (start (2+numOfAI) (7-numOfAI) deckS),True) (makeGameInfo numOfAI ([False,False,True,True],[]))
else
do
putStrLn ("Not a valid range")
goFishStart
else if numOfPlayers == 3 then
do
numOfAI <- readInt ("How many of AI do you want? (0~" ++ show (4-numOfPlayers) ++ ")" )
if(numOfAI < 2) then
do
deckS <- shuffle deck
if numOfAI == 1 then
do
play ( (start (2+numOfAI) (7-numOfAI) deckS),True) (makeGameInfo numOfAI ([False,False,False,True],[]))
else
do
putStrLn ("Not a valid range")
goFishStart
else
do
putStrLn ("Not a valid range")
goFishStart
else if numOfPlayers == 4 then
do
deckS <- shuffle deck
play ( (start (4) (5) deckS),True) (makeGameInfo 0 ([False,False,False,False],[]))
else
do
putStrLn ("Invalid input\n")
goFishStart
-- play will be going back and forth between players/ai during each state
-- Depending on each state, it will let players/ai play that respective stage
-- When it is AI's turn, it will let AI play. When it is human's turn, it will let the player play
play :: Result -> GameInfo -> IO GameInfo
-- Initial state
play (StartS info, True) gameInfo =
if checkIfAI info gameInfo then
(ai_play (StartS info, True) gameInfo)
else
(person_play (StartS info, True) gameInfo)
-- Giving ard from A to B state
play (GiveS card info, True) gameInfo =
if checkIfAI info gameInfo then
(ai_play (GiveS card info, True) gameInfo)
else
(person_play (GiveS card info, True) gameInfo)
-- Saying GoFish state
play (GoFishS card info, True) gameInfo =
if checkIfAI info gameInfo then
(ai_play (GoFishS card info, True) gameInfo)
else
(person_play (GoFishS card info, True) gameInfo)
-- Drawing a card state
play (DrawS info, True) gameInfo =
if checkIfAI info gameInfo then
(ai_play (DrawS info, True) gameInfo)
else
(person_play (DrawS info, True) gameInfo)
-- End of game state, it will simply print out the winner
play (EndS n, True) gameInfo = do
putStrLn ("Game has ended, player " ++ show (n) ++ " has won the game!")
putStrLn ("To play again, type GoFishStart on GHCI")
return gameInfo
----------------------------------------------------------
ai_play :: Result -> GameInfo -> IO GameInfo
ai_play (StartS info, True) gameInfo = let
hands' = hands $ (players info)!!(source info)
players' = players info
rangeC = (length $ hands $ (players info)!!(source info))-1
rangeP = (length (players info))-1 in
do
indexC <- shuffle [0..rangeC]
indexP <- shuffle [0..rangeP]
if ( head indexP == (source info) ) then
do
(ai_play (StartS info, True) gameInfo)
else
do
play (goFish (StartS info, Ask (head indexP) (hands'!!(head indexC)))) gameInfo
-- do
-- putStr (show (StartS info))
-- card <- readCard
-- player <- readInt "\nEnter player number to ask: "
-- if (card == Zero || player == (source info) || player >= length (players info)
-- || not (checkCard (card,Dummy) ((players info)!!(source info))) ) then
-- do
-- putStr ("\n---Invalid card or player number.\n")
-- (person_play (StartS info, True) gameInfo)
-- else
-- do
-- play (goFish (StartS info, Ask player (card, Dummy))) gameInfo
ai_play (GiveS card info, True) gameInfo =
do
putStr ("AI is making the move...Give " ++ show (card))
putStr (show (GiveS card info))
(play (goFish (GiveS card info, Give)) gameInfo)
ai_play (GoFishS card info, True) gameInfo =
do
putStr ("AI is making the move...GoFish")
putStr (show (GoFishS card info))
(play (goFish (GoFishS card info, GoFish)) gameInfo)
ai_play (DrawS info, True) gameInfo =
do
putStr ("AI is making the move...Draw")
putStr (show (DrawS info))
(play (goFish (DrawS info, Draw)) gameInfo)
person_play :: Result -> GameInfo -> IO GameInfo
-- Initial/start state for a human player. It will ask for a card
-- and a player number to choose from.
person_play (StartS info, True) gameInfo =
do
putStr (show (StartS info))
card <- readCard
player <- readInt "\nEnter player number to ask: "
if (card == Zero || player == (source info) || player >= length (players info)
|| not (checkCard (card,Dummy) ((players info)!!(source info))) ) then
do
putStr ("\n---Invalid card or player number.\n")
(person_play (StartS info, True) gameInfo)
else
do
play (goFish (StartS info, Ask player (card, Dummy))) gameInfo
-- Give state for a human player
person_play (GiveS card info, True) gameInfo =
do
putStr (show (GiveS card info))
move <- readMove "Enter Move: "
if (move /= Give) then
do
putStr ("\n---You have the card! Don't lie!. Your only valid move is Give.\n")
(person_play (GiveS card info, True) gameInfo)
else
do
play (goFish (GiveS card info, move)) gameInfo
-- GoFish state for a human player
person_play (GoFishS card info, True) gameInfo =
do
putStr (show (GoFishS card info))
move <- readMove "Enter Move: "
if (move /= GoFish) then
do
putStr ("\n---You don't have the card... Your only valid move is to say GoFish.\n")
(person_play (GoFishS card info, True) gameInfo)
else
do
play (goFish (GoFishS card info, move)) gameInfo
-- Draw state for a human player
person_play (DrawS info, True) gameInfo =
do
putStr (show (DrawS info))
move <- readMove "Enter Move: "
if (move /= Draw) then
do
putStr ("\n---You have nothing to do except draw the card. Type Draw to draw a card.\n")
(person_play (DrawS info, True) gameInfo)
else
do
play (goFish (DrawS info, move)) gameInfo
-------------Helper function-------------
-- check if current source player is an AI or not. Return True if ai
checkIfAI :: Info -> GameInfo -> Bool
checkIfAI info gameInfo = let
source' = source info
lst = fst gameInfo in
lst!!source'
-- Make ai/players
makeGameInfo :: Int -> GameInfo -> GameInfo
makeGameInfo 0 gameInfo = gameInfo
makeGameInfo num gameInfo = let
lst = fst gameInfo
rest = snd gameInfo in
makeGameInfo (num-1) ((lst++[True]),rest)
-- Read an Int from the user
readInt :: String -> IO Int
readInt msg =
do
putStrLn(msg)
hFlush stdout
readLn
-- Read a card rank from the user
readCard :: IO Rank
readCard =
do
putStrLn("Enter a rank for the card: ")
a <- getLine
return (parseRank a)
-- Read user's move
readMove :: String -> IO AMove
readMove msg =
do
putStrLn(msg)
a <- getLine
return (parseMove a)
-- Shuffle cards, Thank you stackoverflow community!
shuffle :: [a] -> IO [a]
shuffle xs = do
ar <- newArray n xs
forM [1 .. n] $ \i -> do
j <- randomRIO (i,n)
vi <- readArray ar i
vj <- readArray ar j
writeArray ar j vi
return vj
where
n = length xs
newArray :: Int -> [a] -> IO (IOArray Int a)
newArray n xs = newListArray (1,n) xs
-- Given an integer range, it will return a random number from the list
getNumberRange :: [Int] -> Int -> IO Int
getNumberRange lst index = do
(x:xs) <- shuffle lst
if x == index then
getNumberRange lst index
else
return x
-- Parse string input to Move
parseMove :: String -> AMove
parseMove str
| str == "Draw" = Draw
| str == "draw" = Draw
| str == "Give" = Give
| str == "give" = Give
| str == "Go Fish" = GoFish
| str == "go fish" = GoFish
| str == "gofish" = GoFish
| str == "GoFish" = GoFish
-- Prase string input into rank
parseRank :: String -> Rank
parseRank str
| str == "Two" = Two
| str == "two" = Two
| str == "Three" = Three
| str == "three" = Three
| str == "Four" = Four
| str == "four" = Four
| str == "Five" = Five
| str == "five" = Five
| str == "Six" = Six
| str == "six" = Six
| str == "Seven" = Seven
| str == "seven" = Seven
| str == "Eight" = Eight
| str == "eight" = Eight
| str == "nine" = Nine
| str == "Nine" = Nine
| str == "ten" = Ten
| str == "Ten" = Ten
| str == "jack" = Jack
| str == "Jack" = Jack
| str == "queen" = Queen
| str == "Queen" = Queen
| str == "king" = King
| str == "King" = King
| str == "ace" = Ace
| str == "Ace" = Ace
| otherwise = Zero
-- parse string input into suite
parseSuite :: String -> Suite
parseSuite str
| str == "Club" = Club
| str == "club" = Club
| str == "Diamond" = Diamond
| str == "diamond" = Diamond
| str == "Heart" = Heart
| str == "heart" = Heart
| str == "Spade" = Spade
| str == "spade" = Spade
| otherwise = Dummy