@@ -5,17 +5,17 @@ import System.Random
55
66main= do
77-- Computer goes first
8- let b= initialBoard
8+ let b= initialBoard5
99 step1_check b
1010
11- -- Creates an empty,5x5 , all-0s board.
12- initialBoard :: [[Int ]]
13- initialBoard= take 5 (repeat (take 5 (repeat 0 )))
11+ -- Creates an empty,SxS , all-0s board.
12+ initialBoard :: Int -> [[Int ]]
13+ initialBoards = take s (repeat (take s (repeat 0 )))
1414
1515step1_check :: [[Int ]]-> IO ()
1616step1_check b=
17- if wins bthen do putStrLn " Nice. "
18- else if loses bthen do putStrLn " Oof. "
17+ if wins bthen printWinMessage
18+ else if loses bthen printLoseMessage b
1919else step2_computer b
2020
2121step2_computer :: [[Int ]]-> IO ()
@@ -26,13 +26,13 @@ step2_computer b = do
2626
2727step3_check :: [[Int ]]-> IO ()
2828step3_check b=
29- if wins bthen do putStrLn " Nice. "
30- else if loses bthen do putStrLn " Oof. "
29+ if wins bthen printWinMessage
30+ else if loses bthen printLoseMessage b
3131else step4_player b
3232
3333step4_player :: [[Int ]]-> IO ()
3434step4_player b= do
35- putStrLn " --- "
35+ putStrLn " "
3636putStrLn (showboard b)
3737putStrLn " Enter wasd:"
3838 move<- getLine
@@ -45,16 +45,27 @@ step4_player b = do
4545 step1_check b'
4646
4747showboard :: [[Int ]]-> String
48- showboard board= concat [
49- if i`mod` 5 == 0 then
50- if c== 0 then " .\n "
51- else (show c)++ " \n "
52- else if c== 0 then " ."
53- else (show c)++ " "
54- | (i, c)<- (enumerateboard board)]
48+ showboard b=
49+ let s= length b
50+ in concat [
51+ if i`mod` s== 0 then
52+ if c== 0 then " .\n "
53+ else (show c)++ " \n "
54+ else if c== 0 then " ."
55+ else (show c)++ " "
56+ | (i, c)<- (enumerateboard b)]
5557
5658enumerateboard :: [[Int ]]-> [(Int ,Int )]
57- enumerateboard board= zip [1 .. ] (concat board)
59+ enumerateboard b= zip [1 .. ] (concat b)
60+
61+ printWinMessage :: IO ()
62+ printWinMessage= do putStrLn " Nice."
63+
64+ printLoseMessage :: [[Int ]]-> IO ()
65+ printLoseMessage b= do
66+ putStrLn " "
67+ putStrLn (showboard b)
68+ putStrLn " Oof, you lose."
5869
5970-- MARK:
6071-- COMPUTER'S TURN
@@ -70,7 +81,8 @@ cturn b g =
7081 rs= randomRs (0 , upperbound- 1 ) g
7182 i= rs!! 0
7283 x= xs!! i
73- index= mapback x
84+ s= length b
85+ index= mapback x s
7486in
7587 replace2d index1 b
7688
@@ -87,11 +99,11 @@ replace 0 a (_:xs) = a:xs
8799replace i a (x: xs)= x: (replace (i- 1 ) a xs)
88100
89101-- Map the given index from flat space to a (row, col) in 2d space,
90- -- assuming a 5x5 board.
91- mapback :: Int -> (Int ,Int )
92- mapback i=
93- let row= quot i5 -- i.e. floor of i/5
94- col= rem i5 -- i.e. remainder of i/5
102+ -- for an SxS board.
103+ mapback :: Int -> Int -> (Int ,Int )
104+ mapback is =
105+ let row= quot is -- i.e. floor of i/s
106+ col= rem is -- i.e. remainder of i/s
95107in (row, col)
96108
97109-- Returns a list of all indices of zero items in the given board;
@@ -192,7 +204,7 @@ wins b = 10 `elem` (flatten b)
192204-- - There are no empty cells, AND
193205-- - There are no available moves
194206loses :: [[Int ]]-> Bool
195- loses b= (hasNoEmptyCells b)
207+ loses b= (not ( hasNoEmptyCells b))
196208&& (not (anyRowNeighboursMatch b))
197209&& (not (anyColNeighboursMatch b))
198210