-
Notifications
You must be signed in to change notification settings - Fork 2
/
MinesweeperV6.hs
729 lines (536 loc) · 25.7 KB
/
MinesweeperV6.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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
import System.Random
import Data.List
import Data.Char
import Control.Monad
import Data.Maybe
import Text.Read
import Debug.Trace
{-
Position represents a position on the board as a pari of coordinates represented as a tuple.
-}
type Position = (Int, Int)
{-
Mine represents the mines on the board as bools when a square is opened.
-}
type Mine = Bool
{-
NeighbourMines represent the integers on the board, which indicates how many mines there are adjacent to the current square.
-}
type NeighbourMines = Int
{-
Flag represent the
-}
type Flag = Bool
{-
The datatype Board represent the game field as a list of Positions that creates a square.
-}
type Board = [Square]
{-
-}
data State = Revealed
| Hidden deriving (Show, Eq)
{-
Square represents stuff -} --PERHAPS MOVE THIS ONE ABOVE BOARD?
data Square = Square Position State Mine NeighbourMines Flag deriving (Show,Eq)
{- 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: True.
RETURNS: A minesweeper board as a list.
SIDE EFFECTS:
EXAMPLES: mkBoard 0 = []
mkBoard 1 = [Square (1,1) Hidden False 0 False]
mkBoard 3 = [Square (1,1) Hidden False 0 False,Square (1,2) Hidden False 0 False,Square (1,3) Hidden False 0 False,Square (2,1) Hidden False 0 False,Square (2,2) Hidden False 0 False,Square (2,3) Revealed False 0 False, Square (3,1) Revealed False 0 False,Square (3,2) Revealed False 0 False,Square (3,3) Revealed False 0 False]
-}
mkBoard :: Int -> Board
mkBoard n = [(Square (x,y) Hidden False 0 False)
| 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 "mines".
EXAMPLES: insertMines [] [] = []
insertMines [] (mkBoard 0) = []
insertMines [(1,1)] (mkBoard 0) = []
insertMines [(1,1), (2,3)] (mkBoard 3) = [Square (1,1) Revealed True 1 False,Square (1,2) Revealed False 0 False,Square (1,3) Revealed False 0 False,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)
inserts a mine at the given position.
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 False,Square (1,2) Hidden True 1 False, Square (2,1) Hidden False 0 False,Square (2,2) Hidden False 0 False]
-}
insertMine :: Position -> Board -> Board
insertMine _ [] = []
insertMine position (square@(Square pos _ _ _ _):squares)
| position == pos = (Square pos Hidden True 1 False) : squares
| otherwise = square : insertMine position squares
{- randomCoords n
Creates a random value of the type Position where the two values can not be larger than n
PRE: True
RETURNS: A tuple with two random integers as first and second values.
SIDE EFFECTS:
EXAMPLES: randomCoords 0 = (1,1)
randomCoords 2 = (1,2)
-}
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
PRE:
RETURNS: A string representing a game board.
SIDE EFFECTS:
EXAMPLES: printBoard [] 5 = ""
printBoard (mkBoard 2) 5 = " a b c d e\n1 | # # 2 | # # "
-}
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 of the letters in the alphabet in reversed order, with upper case letters
when n is larger than the amount of letters in the alphabet.
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 52 = "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 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 53 = "Sorry pal!"
-}
printTop :: Int -> String
printTop 0 = []
printTop n | n < 53 = (['a'..'z'] ++ ['A'..'Z'])!!(n-1) : " " ++ printTop (n-1)
printTop n = "Sorry pal!"
{- printSquare square
Convert a square into an ASCII-character.
PRE:
RETURNS: An ASCII-character fulfilling the given condition.
SIDE EFFECTS:
EXAMPLES: printSquare (Square (1,1) Hidden False 0 False) = "#"
printSquare (Square (2,2) Revealed False 7 False) = "7"
printSquare (Square (2,2) Revealed False 7 True) = "F"
-}
printSquare :: Square -> String
printSquare (Square _ _ _ _ True) = "F"
printSquare (Square _ Hidden _ _ _) = "#"
printSquare (Square _ Revealed True n _) = "*"
printSquare (Square _ Revealed _ 0 _) = " "
printSquare (Square _ Revealed _ n _) = show n
{-dupMines mines
Checks if a position is occupied by a mine or not. || Checks if there are any duplicate tuples in a list.
PRE: True.
RETURNS: A bool that indicates whether there is a duplicate tuple or not.
SIDE EFFECTS:
EXAMPLES: dupMines [] = False
dupMines [(1,2), (1,5)] = False
dupMines [(1,5),(1,5)] = True
dupMines [(1,5),(1,5), (1,3)] = True
-}
dupMines :: [Position] -> Bool
dupMines mines = length remDups < length mines
where remDups = map head $ group $ sort mines
{- getMines n size mines
PRE:
RETURNS:
SIDE EFFECTS:
EXAMPLES:
-}
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)
Counts the number of mines at the board.
PRE: True.
RETURNS: An integer representing the number of mines in the called list.
SIDE EFFECTS:
VARIANT: Length (square:s)
EXAMPLES: countMines [] = 0
countMines [Square (1,1) Revealed True 0 False,Square (1,2) Hidden False 0 False,Square (2,1) Hidden False 0 False,Square (2,2) Hidden True 0 False] = 2
countMines [Square (1,1) Hidden False 0 False,Square (1,2) Hidden False 0 False,Square (2,1) Hidden False 0 False,Square (2,2) Hidden True 0 False] = 1
-}
countMines :: Board -> Int
countMines [] = 0
countMines (square@(Square _ _ True _ _):s) = 1 + countMines s
countMines (square:s) = countMines s
{- getNeighbourSquares
Get all surrounding squares of the given position.
PRE: (x,y) has to be a coordinate of the board and size == size of board
RETURNS: A list of the squares that surround the square with position (x,y)
EXAMPLES: getNeighbourSquares (2,2) Board 3 = [Square (1,1) Hidden False 0 False,Square (1,2) Hidden False 0 False,Square (1,3) Hidden False 0 False,Square (2,1) Hidden False 0 False,Square (2,3) Hidden False 0 False,Square (3,1) Hidden False 0 False,Square (3,2) Hidden False 0 False,Square (3,3) Hidden False 0 False]
-}
getNeighbourSquares :: Position -> Board -> Int -> Board
getNeighbourSquares (x,y) board size
| x == 1 && y == 1 = [(getSquare (x,y+1) board),
(getSquare (x+1, y+1) board),
(getSquare (x+1, y) board)
]
| x == 1 && y == size = [(getSquare (x, y-1) board),
(getSquare (x+1, y-1) board),
(getSquare (x+1, y) board)
]
| y == 1 && x == size = [(getSquare (x-1, y) board),
(getSquare (x-1, y+1) board),
(getSquare (x, y+1) board)
]
| y == size && x == size = [(getSquare (x, y-1) board),
(getSquare (x-1, y-1) board),
(getSquare (x-1, y) board)
]
| x == 1 = [(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)
]
| y == 1 = [(getSquare (x-1,y) board),
(getSquare (x-1,y+1) board),
(getSquare (x,y+1) board),
(getSquare (x+1,y) board),
(getSquare (x+1,y+1) board)
]
| x == size = [(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)
]
| y == size = [(getSquare (x-1,y-1) board),
(getSquare (x-1,y) board),
(getSquare (x,y-1) board),
(getSquare (x+1,y-1) board),
(getSquare (x+1,y) board)
]
| otherwise = [(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.
PRE:
RETURNS:
SIDE EFFECTS:
EXAMPLES:
-}
getSquare :: Position -> Board -> Square
getSquare pos (square@(Square spos _ _ _ _):s)
| pos == spos = square
| otherwise = getSquare pos s
getSquare pos board = (Square pos Hidden False 0 False)
{- countNeighbourMines (neighbour:s)
Counts all mines of neighbours.
PRE: (neighbour:s) is a list of the squares surrounding a square
VARIANT: length (neighbour:s)
RETURNS: The amount of mines present in (neighbour:s)
EXAMPLES: countNeighbourMines [Square (1,1) Hidden True 1 False,Square (1,2) Hidden False 0 False,Square (1,3) Hidden False 0 False,Square (2,1) Hidden False 0 False,Square (2,3) Hidden False 0 False,Square (3,1) Hidden False 0 False,Square (3,2) Hidden False 0 False,Square (3,3) Hidden True 1 False] = 2
countNeighbourMines [Square (1,1) Hidden False 0 False,Square (1,2) Hidden False 0 False,Square (1,3) Hidden False 0 False,Square (2,1) Hidden False 0 False,Square (2,3) Hidden False 0 False,Square (3,1) Hidden False 0 False,Square (3,2) Hidden False 0 False,Square (3,3) Hidden False 0 False] = 0
-}
countNeighbourMines :: Board -> Int
countNeighbourMines [] = 0
countNeighbourMines (neighbour:s) = (countMine neighbour) + countNeighbourMines s
{-countMine square
Gives an int that indicates wether or not the square has a mine.
PRE: True
RETURNS: 1 if Square has a mine, 0 if square does not have a mine
EXAMPLES: countMine (Square (1,1) Hidden True 1 False) = 1
countMine (Square (2,2) Hidden False 1 False) = 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 -> Int -> Board
initNeighbours [] boardcpy _ = []
initNeighbours board@(square@(Square pos state mine neighbours flag):s) boardcpy size =
(Square pos state mine (countNeighbourMines $ getNeighbourSquares pos boardcpy size) flag : initNeighbours s boardcpy size)
{- mkChoice position board boarcpy size
Reveals the given position, if the square of that position has 0 surrounding mines it will reveal the neighbours recursively.
PRE: size == size of board
VARIANT: length board
RETURNS: A board that has revealed all given positions
EXAMPLES: mkChoice Nothing (mkBoard 2) (mkBoard 2) 2 = [Square (1,1) Hidden False 0 False, Square (1,2) Hidden False 0 False, Square (2,1) Hidden False 0 False, Square (2,2) Hidden False 0 False]
mkChoice (Just (1,1)) (mkBoard 2) (mkBoard 2) 2 = [Square (1,1) Revealed False 0 False, Square (1,2) Hidden False 0 False, Square (2,1) Hidden False 0 False, Square (2,2) Hidden False 0 False]
-}
mkChoice :: Maybe Position -> Board -> Board -> Int -> Board
mkChoice Nothing board cpy _ = board
mkChoice _ [] cpy _ = cpy
mkChoice (Just (x,y)) (square@(Square pos state mine mines flag):xs) cpy size
| (x,y) == pos && state == Hidden && mines == 0 = revealNeighbours (hiddenNeighbours $ getNeighbourSquares pos cpy size) (insertSquare (Square pos Revealed mine mines flag) cpy) size
| (x,y) == pos && state == Hidden = insertSquare (Square pos Revealed mine mines flag) cpy
| otherwise = mkChoice (Just (x,y)) xs cpy size
{- revealNeighbours (x:xs) board size
Reveals all squares next to a chosen square with 0 adjacent mines, which again applies to any of the adjacent squares that also have 0 adjacent mines
PRE: (x:xs) must be a list of hidden squares
VARIANT: length board
RETURNS: A game board with the proper squares revealed
EXAMPLES: revealNeighbours [] board 3 = board
revealNeighbours [Square (1,2) Hidden False 0 False,Square (2,2) Hidden False 0 False,Square (2,1) Hidden False 0 False] [Square (1,1) Hidden False 0 False,Square (1,2) Hidden False 0 False,Square (1,3) Hidden False 0 False,Square (2,1) Hidden False 0 False,Square (2,2) Hidden False 1 False,Square (2,3) Hidden False 1 False,Square (3,1) Hidden False 0 False,Square (3,2) Hidden False 1 False,Square (3,3) Hidden True 0 False] 3 = [Square (1,1) Revealed False 0 False,Square (1,2) Revealed False 0 False,Square (1,3) Revealed False 0 False,Square (2,1) Revealed False 0 False,Square (2,2) Revealed False 1 False,Square (2,3) Revealed False 1 False,Square (3,1) Revealed False 0 False,Square (3,2) Revealed False 1 False,Square (3,3) Hidden True 0 False]
-}
revealNeighbours :: [Square] -> Board -> Int -> Board
revealNeighbours [] board _ = board
revealNeighbours (x:xs) board size = revealNeighbours xs (revealNeighbours' x board size) size
{-revealNeighbours'
Reveals all squares next to a chosen square with 0 adjacent mines, which again applies to any of the adjacent squares that also have 0 adjacent mines
PRE: Square is hidden
RETURNS: A board with mkChoice called on the square at position pos
EXAMPLES: revealNeighbours' (Square (1,1) Hidden False 0 False) [Square (1,1) Hidden False 0 False,Square (1,2) Hidden False 0 False,Square (1,3) Hidden False 0 False,Square (2,1) Hidden False 0 False,Square (2,2) Hidden False 1 False,Square (2,3) Hidden False 1 False,Square (3,1) Hidden False 0 False,Square (3,2) Hidden False 1 False,Square (3,3) Hidden True 0 False] 3 = [Square (1,1) Revealed False 0 False,Square (1,2) Revealed False 0 False,Square (1,3) Revealed False 0 False,Square (2,1) Revealed False 0 False,Square (2,2) Revealed False 1 False,Square (2,3) Revealed False 1 False,Square (3,1) Revealed False 0 False,Square (3,2) Revealed False 1 False,Square (3,3) Hidden True 0 False]
-}
revealNeighbours' :: Square -> Board -> Int -> Board
revealNeighbours' (Square pos _ _ _ _) board size = mkChoice (Just pos) board board size
{-insertSquare square board
Insert a given square into the board at its position.
PRE: True
VARIANT: length board
RETURNS: The board with the square inserted.
EXAMPLES: insertSquare (Square (1,1) Hidden False 0 False) [] = []
insertSquare (Square (1,1) Revealed False 8 False) (mkBoard 1) = [Square (1,1) Revealed False 8 False]
-}
insertSquare :: Square -> Board -> Board
insertSquare _ [] = []
insertSquare square@(Square pos1 _ _ _ _) (x@(Square pos2 _ _ _ _):xs)
| pos1 == pos2 = square : xs
| otherwise = x : insertSquare square xs
{-hiddenNeighbours neighbours
Takes all the hidden neighbours from the getNeighbourSquares function.
VARIANT: Length of neighbours.
PRE: neighbours == Neighbour squares
RETURNS: A list of of all hidden neighbours in getNeighbourSquares.
EXAMPLES: hiddenNeighbours [] = []
hiddenNeighbours (getNeighbourSquares (3,5) (mkBoard 5) 5) = [Square (2,4) Hidden False 0 False,Square (2,5) Hidden False 0 False,Square (3,4) Hidden False 0 False,Square (4,4) Hidden False 0 False,Square (4,5) Hidden False 0 False]
-}
hiddenNeighbours :: [Square] -> [Square]
hiddenNeighbours [] = []
hiddenNeighbours (x@(Square _ Hidden _ _ _):xs) = x : hiddenNeighbours xs
hiddenNeighbours neighbours = hiddenNeighbours (tail neighbours)
{- choiceMine position board
Checks if given position has a mine on it, if it does, it returns true else false.
PRE: True.
RETURNS: A boolean telling the user whether theres a mine or not in the specific square.
EXAMPLES: choiceMine (Just(1,23)) [] = False
choiceMine (Just(1,2)) (mkBoard 2) = False
choiceMine (Just(1,2)) (insertMine (1,2) (mkBoard 2)) = True
-}
choiceMine :: Maybe Position -> Board -> Bool
choiceMine _ [] = False
choiceMine Nothing board = False
choiceMine (Just (x,y)) (square@(Square pos state mine mines flag):s)
| (x,y) == pos && mine = mine
| otherwise = choiceMine (Just (x,y)) s
{- mkFlag (Just(x,y,)) Board
Flags a square on the board, by changing the second bool in the position.
PRE: True.
RETURNS: A board with a flagged position.
EXAMPLES: mkFlag (Just(1,2)) [] = []
mkFlag (Just(1,1)) (mkBoard 2) = [Square (1,1) Hidden False 0 True,Square (1,2) Hidden False 0 False,Square (2,1) Hidden False 0 False,Square (2,2) Hidden False 0 False]
mkFlag (Just(0,0)) (mkBoard 2) = [Square (1,1) Hidden False 0 False,Square (1,2) Hidden False 0 False,Square (2,1) Hidden False 0 False,Square (2,2) Hidden False 0 False]
-}
mkFlag :: Maybe Position -> Board -> Board
mkFlag _ [] = []
mkFlag Nothing board = board
mkFlag (Just (x,y)) (square@(Square pos state mine mines flag):s)
| (x,y) == pos && flag == False = (Square pos state mine mines True) : mkFlag (Just (x,y)) s
| (x,y) == pos && flag == True = (Square pos state mine mines False) : mkFlag (Just (x,y)) s
| otherwise = square : mkFlag (Just (x,y)) s
{- toPosition s
This function translates a given move into its position.
PRE: True
RETURNS: Nothing if the move is invalid, (Just move) if it is valid
EXAMPLES: toPosition "" = Nothing
toPosition "asd" = Nothing
toPosition "1a" = Just (1,1)
-}
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 c cilist
Outputs the number that is mapped to the given character.
PRE: c exists in cilist
VARIANT: length cilist
RETURNS: A number that represents the given character.
EXAMPLES: chartoInt 'a' [('a',1), ('b', 2)] = 1
chartoInt 'b' [('a',1), ('b', 2)] = 2
chartoInt 'Z' cilist = 52
-}
chartoInt :: Char -> [(Char,Int)] -> Int
chartoInt c (x:xs)
| c == fst x = snd x
| otherwise = chartoInt c xs
cilist = zip (['a'..'z'] ++ ['A'..'Z']) [1..52] -- A list of tuples where characters are mappep to the corresponding numbers.
{- isWin board
Checks if all squares are revealed (not counting the squares that has mines)
PRE: True
VARIANT: length board
RETURNS: True if board not containing a hidden non mine square, else False
EXAMPLES: isWin [] = True
isWin (mkBoard 3) = False
isWin [Square (1,1) Revealed False 0 False] = True
-}
isWin :: Board -> Bool
isWin [] = True
isWin (square@(Square pos state mine mines flag):s)
| state == Hidden && not mine = False
| otherwise = isWin s
{- play board size
This is the gameloop, gets a move from the user and checks if that move is a flag or not, and then insert it in the board.
Checks if that move is a mine and if all squares are revealed, if not it calls the gameloop again.
SIDE EFFECTS: Prints the current board and the amount of mines that exist in the board.
Also prints if you have won or lost the game.
RETURNS: A string, that indicate if you have won or lost.
-}
play :: Board -> Int -> IO ()
play board size = do
putStrLn "Make your move"
choice <- getLine
let newBoard = board
if isPrefixOf "flag " choice then do
let newBoard = mkFlag (toPosition $ drop 5 choice) board
prettyPrint $ printBoard newBoard size
putStrLn $ "Amount of mines: " ++ (show $ countMines newBoard)
play newBoard size
else do
let newBoard = mkChoice (toPosition choice) board board size
prettyPrint $ printBoard newBoard size
putStrLn $ "Amount of mines: " ++ (show $ countMines newBoard)
if choiceMine (toPosition choice) board then putStrLn ("You Lost!\nYou hit a mine at: " ++ show choice)
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
PRE: True.
RETURNS: A tuple where x is the size of the boards axes and y is the number of mines.
SIDE EFFECTS:
EXAMPLES: mkDifficulty "small" = (3,1)
mkDifficulty "Custom" = mkCustom
mkDifficulty "Hello World!" = getDifficulty
-}
mkDifficulty :: String -> IO (Int, Int)
mkDifficulty "small" = return (3,1)
mkDifficulty "medium" = return (8,12)
mkDifficulty "large" = return (18,32)
mkDifficulty "custom" = mkCustom
mkDifficulty _ = getDifficulty
{- getDifficulty
Gets a difficulty from the user, if the input is invalid, it will call the function again.
SIDE EFFECTS: Prints the different choices the user can enter.
RETURNS: An IO tuple that contains the size of the board and the amount of mines, of the inputed difficulty.
-}
getDifficulty :: IO (Int, Int)
getDifficulty = do
putStrLn "Please enter a difficulty (small/medium/large) or choose custom"
stringDifficulty <- getLine
difficulty <- mkDifficulty stringDifficulty
return difficulty
{- mkCustom
Takes custom values from the user and creates a difficulty from those values.
RETURNS: An IO tuple with the size of board and the amount of mines.
-}
mkCustom :: IO (Int, Int)
mkCustom = do
boardChoice <- getBoardChoice
mineChoice <- getMineChoice boardChoice
return (boardChoice, mineChoice)
{- getBoardChoice
Checks if input is of valid size and if that input is of type Int.
If not it will call the function again until it gets a valid input.
RETURNS: The user input as an Int, if it is between range (1 - 52) and of type Int.
SIDE EFFECTS: Prints question and error message if user input is invalid.
EXAMPLES:
-}
getBoardChoice :: IO Int
getBoardChoice = do
putStrLn "Enter the size of the board"
boardChoice <- getLine
let boardChoiceInt = (read boardChoice :: Int)
if not $ checkValidInt boardChoice then do
putStrLn "Invalid type, size is of type Int"
getBoardChoice
else if boardChoiceInt > 52 || boardChoiceInt < 1 then do
putStrLn "Invalid size, valid size is: 1 - 52"
getBoardChoice
else return boardChoiceInt
{- getMineChoice size
Checks if input is of valid size and if that input is of type Int.
If not it will call the function again until it gets a valid input.
PRE: size >= 1
RETURNS: The user input as an Int, if it is between range (0 - maxSize) and of type Int.
SIDE EFFECTS: Prints question and error message if user input is invalid.
EXAMPLES:
-}
getMineChoice :: Int -> IO Int
getMineChoice size = do
putStrLn "Enter the number of mines on the board"
mineChoice <- getLine
let mineChoiceInt = (read mineChoice :: Int)
let maxSize = size * size - 1
if not $ checkValidInt mineChoice then do
putStrLn "Invalid type, amount is of type Int"
getMineChoice size
else if mineChoiceInt > maxSize || mineChoiceInt < 0 then do
putStrLn $ "Invalid amount, valid amount is: " ++ "0 - " ++ (show maxSize)
getMineChoice size
else return mineChoiceInt
{- checkValidInt strInt
Checks if a string consists of only integers or not.
PRE: True.
RETURNS: A boolean that tells the user if theres only integers or not in the string.
SIDE EFFECTS: True.
EXAMPLES: checkValidInt "" = False
checkValidInt "12345" = True
checkValidInt "1234,5" = False
checkValidInt "Hello World!" = False
-}
checkValidInt :: String -> Bool
checkValidInt strInt = isJust (readMaybe strInt :: Maybe Int)
{- playMinesweeper
Initializes the board with mines and the amount neighbours for each square.
Sends the fully initialized board to the gameloop 'play'.
SIDE EFFECTS: Prints the header and the initialized board.
-}
playMinesweeper :: IO ()
playMinesweeper = do
putStrLn "Welcome to Minesweeper v.1"
(size, mineAmount) <- getDifficulty
let board = mkBoard size
mines <- getMines mineAmount size []
let newBoard = insertMines mines board
let playBoard = initNeighbours newBoard newBoard size
prettyPrint $ printBoard playBoard size
play playBoard size