-
Notifications
You must be signed in to change notification settings - Fork 2
/
MinesweeperV3Comments.hs
585 lines (253 loc) · 10.7 KB
/
MinesweeperV3Comments.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
import System.Random
import Data.List
import Data.Char
import Control.Monad
type Position = (Int, Int)
type Mine = Bool
type NeighbourMines = Int
data State = Revealed
| Hidden deriving (Show, Eq)
data Square = Square Position State Mine NeighbourMines deriving (Show,Eq)
type Board = [Square]
{- mkBoard n
Initilize the board using list comprehension,
this makes squares with a default value for every position set to the maximum by the input
PRE:
RETURNS: A minesweeper board as a list.
SIDE EFFECTS:
EXAMPLES: mkBoard 0 = []
mkBoard 1 = [Square (1,1) Revealed False 0]
mkBoard 3 = [Square (1,1) Revealed False 0,Square (1,2) Revealed False 0,Square (1,3) Revealed False 0,Squ are (2,1) Revealed False 0,Square (2,2) Revealed False 0,Square (2,3) Revealed False 0,Square (3,1) Reveal ed False 0,Square (3,2) Revealed False 0,Square (3,3) Revealed False 0]
-}
mkBoard :: Int -> Board
mkBoard n = [(Square (x,y) Hidden False 0)
| x <- [1..n], y <- [1..n]]
{- insertMines mines board
insert a list of mines into the board. The mines are represented as bools.
PRE:
RETURNS: A list with mines inserted to the borad at positions based on the given coordinates.
SIDE EFFECTS:
VARIANT: Length of the list of "mines".
EXAMPLES: insertMines [] [] = []
insertMines [] (mkBoard 0) = []
insertMines [(1,1)] (mkBoard 0) = []
insertMines [(1,1), (2,3)] (mkBoard 3) = [Square (1,1) Revealed True 1,Square (1,2) Revealed False 0,Square (1,3) Revealed False 0,Square (2,1) Revealed False 0,Square (2,2) Revealed False 0,Square (2,3) Revealed True 1,Square (3,1) Revealed False 0,Square (3,2) Revealed False 0,Square (3,3) Revealed False 0]
-}
insertMines :: [Position] -> Board -> Board
insertMines [] board = board
insertMines (p:ps) [] = []
insertMines (p:ps) board = insertMines ps (insertMine p board)
{- insertMine position (square@(Square pos _ _ _):squares)
PRE: Position has to exist.
RETURNS: A list of squares, containing one mine from position.
SIDE EFFECTS:
VARIANT: ?
EXAMPLES: insertMine (1,6) [] = []
insertMine (1,2) (mkBoard 2) = [Square (1,1) Hidden False 0,Square (1,2) Hidden True 1,Square (2,1) Hidden False 0,Square (2,2) Hidden False 0]
-}
insertMine :: Position -> Board -> Board
insertMine _ [] = []
insertMine position (square@(Square pos _ _ _):squares)
| position == pos = (Square pos Hidden True 1) : squares
| otherwise = square : insertMine position squares
{- randomCoords n
Creates a list of n random coordinates.
PRE:
RETURNS: A list of n random positions represented as tuples.
SIDE EFFECTS:
EXAMPLES: randomCoords 0 = []
randomCoords 2 = [(13,3),(2,9)]
-}
randomCoord :: Int -> IO Position
randomCoord n = do
x <- randomRIO (1, n)
y <- randomRIO (1, n)
return (x,y)
{- prettyPrint n
Outputs the string on the terminal
PRE: True.
RETURNS: A string without ' "" '.
SIDE EFFECTS:
EXAMPLES: prettyPrint "" =
prettyPrint "Hello World!" = Hello World!
prettyPrint "I love numbers: 123234" = I love numbers: 123234
-}
prettyPrint :: String -> IO ()
prettyPrint n = putStrLn n
{- printBoard (x@(Square (x1,y1) _ _ _):xs) n
-}
printBoard :: Board -> Int -> String
printBoard [] n = []
printBoard (x@(Square (x1,y1) _ _ _):xs) n
| y1 == 1 && x1 == 1 = " " ++ (reverse $ printTop n) ++ "\n" ++ (show x1) ++ " | " ++ printSquare x ++ " " ++ printBoard xs n
| y1 == 1 = if x1 < 10 then (show x1) ++ " | " ++ printSquare x ++ " " ++ printBoard xs n
else (show x1) ++ "| " ++ printSquare x ++ " " ++ printBoard xs n
| y1 == n = printSquare x ++ "\n" ++ printBoard xs n
| otherwise = printSquare x ++ " " ++ printBoard xs n
{- printTop n
Prints the alphabet in reversed order with the first n letters.
PRE: True.
RETURNS: A string with some of the alphabet, in reversed order.
SIDE EFFECTS:
EXAMPLES: printTop 0 = ""
printTop 26 = "z y x w v u t s r q p o n m l k j i h g f e d c b a "
printTop 27 = "Nice try! The alphabet is not that long"
-}
printTop :: Int -> String
printTop 0 = []
printTop n = if n > 26 then "Nice try! The alphabet is not that long." else ['a'..'z']!!(n-1) : " " ++ printTop (n-1)
{- printSquare square
Convert a square into an ASCII-character
-}
printSquare :: Square -> String
printSquare (Square _ Hidden _ _) = "#"
printSquare (Square _ Revealed True n) = "*"
printSquare (Square _ Revealed _ 0) = " "
printSquare (Square _ Revealed _ n) = show n
{-dupMines mines
-}
dupMines :: [Position] -> Bool
dupMines mines = if length remDups < length mines then True else False
where remDups = map head $ group $ sort mines
{- getMines n size mines
-}
getMines :: Int -> Int -> [Position] -> IO [Position]
getMines 0 size mines = return mines
getMines n size mines = do
mine <- randomCoord size
let mineList = (mine:mines)
if (dupMines mineList) then getMines n size mines
else getMines (n-1) size mineList
{- countMines (square@(Square _ _ True _):s)
-}
countMines :: Board -> Int
countMines [] = 0
countMines (square@(Square _ _ True _):s) = 1 + countMines s
countMines (square:s) = countMines s
{- getNeighbourSquares
get all surrounding squares of hte given position
-}
getNeighbourSquares :: Position -> Board -> Board
getNeighbourSquares (x,y) board = [
(getSquare (x-1,y-1) board),
(getSquare (x-1,y) board),
(getSquare (x-1,y+1) board),
(getSquare (x,y-1) board),
(getSquare (x,y+1) board),
(getSquare (x+1,y-1) board),
(getSquare (x+1,y) board),
(getSquare (x+1,y+1) board)
]
{- getSquare position board
get the corresponding square from the given position
-}
getSquare :: Position -> Board -> Square
getSquare pos [] = (Square pos Hidden False 0)
getSquare pos (square@(Square spos _ _ _):s)
| pos == spos = square
| otherwise = getSquare pos s
{- countNeighbourMines neighbours
counts all mines of neighbours
-}
countNeighbourMines :: Board -> Int
countNeighbourMines [] = 0
countNeighbourMines (neighbour:s) = (countMine neighbour) + countNeighbourMines s
{-countMine (Square _ _ _ _
-}
-- If square has a mine, this will output 1 else 0
countMine :: Square -> Int
countMine (Square _ _ True _) = 1
countMine _ = 0
{- initNeighbours board boardcpy
This function outputs a new board with NeighbourMines initialized for all squares.
-}
initNeighbours :: Board -> Board -> Board
initNeighbours [] boardcpy = []
initNeighbours board@(square@(Square pos state mine neighbours):s) boardcpy =
(Square pos state mine (countNeighbourMines $ getNeighbourSquares pos boardcpy): initNeighbours s boardcpy)
{- mkChoice position board
This function takes a position and reveal the state of that square.
-}
mkChoice :: Maybe Position -> Board -> Board
mkChoice Nothing board = board
mkChoice _ [] = []
mkChoice (Just (x,y)) (square@(Square pos state mine mines):s)
| (x,y) == pos = (Square pos Revealed mine mines) : s
| otherwise = square : mkChoice (Just (x,y)) s
{- choiceMine position board
Checks if given position has a mine on it, if it does, it returns true else false.
-}
choiceMine :: Maybe Position -> Board -> Bool
choiceMine _ [] = False
choiceMine Nothing board = False
choiceMine (Just (x,y)) (square@(Square pos state mine mines):s)
| (x,y) == pos && mine = mine
| otherwise = choiceMine (Just (x,y)) s
{- toPosition s
This function translates a given move (ex. "1f") into its position (ex. (1,6))
-}
toPosition :: String -> Maybe Position
toPosition (i1:i2:c:[]) = if isDigit i1 && isDigit i2 && isAlpha c then Just (read (i1 : i2 : []) :: Int, chartoInt c cilist) else Nothing
toPosition (i:c:[]) = if isDigit i && isAlpha c then Just (digitToInt i, chartoInt c cilist) else Nothing
toPosition _ = Nothing
{- chartoInt
Outputs the number that is mapped to the given character.
-}
chartoInt :: Char -> [(Char,Int)] -> Int
chartoInt c (x:xs)
| c == fst x = snd x
| otherwise = chartoInt c xs
{- cilist
-}
-- A list of tuples where characters are mappe to the corresponding numbers.
cilist = zip ['a'..'z'] [1..26]
{- isWin board
Checks if all squares are revealed (not counting the squares that has mines)
-}
isWin :: Board -> Bool
isWin [] = True
isWin (square@(Square pos state mine mines):s)
| state == Hidden && not mine = False
| otherwise = isWin s
{- play board size
In this function we handle all the moves from the user,
check if the game is over and prints the board.
-}
play :: Board -> Int -> IO ()
play board size = do
putStrLn "Make your move"
choice <- getLine
let newBoard = mkChoice (toPosition choice) board
prettyPrint $ printBoard newBoard size
if choiceMine (toPosition choice) board then putStrLn "You Lost!" else if (isWin newBoard) then putStrLn "You won!" else play newBoard size
{- mkDifficulty s
Outputs the size of the board and amount of mines depending on the desired difficulty
-}
mkDifficulty :: String -> IO (Int, Int)
mkDifficulty "easy" = return (3,1)
mkDifficulty "medium" = return (8,8)
mkDifficulty "hard" = return (18,18)
mkDifficulty "custom" = mkCustom
{-mkCustom
-}
mkCustom = do
putStrLn "Enter the breadth size of the board"
boardChoice <- getLine
putStrLn "Enther the number of mines on the board"
mineChoice <- getLine
return ((read (boardChoice) :: Int,read (mineChoice) :: Int))
{- main
Just testing stuff
-}
main :: IO ()
main = do
putStrLn "Welcome to Minesweeper v.000000000001"
putStrLn "Please enter a difficulty (easy/medium/hard) or choose custom"
getDifficulty <- getLine
difficulty <- mkDifficulty getDifficulty
let board = mkBoard (fst difficulty)
mines <- getMines (snd difficulty) (fst difficulty) []
let newBoard = insertMines mines board
let playBoard = initNeighbours newBoard newBoard
prettyPrint $ printBoard playBoard (fst difficulty)
play playBoard (fst difficulty)