Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitff769ba

Browse files
committed
Fix lose condition and make board size configurable
1 parent8c52d27 commitff769ba

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

‎10.hs‎

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import System.Random
55

66
main=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=take5 (repeat (take5 (repeat0)))
11+
-- Creates an empty,SxS, all-0s board.
12+
initialBoard::Int->[[Int]]
13+
initialBoards=takes (repeat (takes (repeat0)))
1414

1515
step1_check:: [[Int]]->IO()
1616
step1_check b=
17-
if wins bthendoputStrLn"Nice."
18-
elseif loses bthendoputStrLn"Oof."
17+
if wins bthenprintWinMessage
18+
elseif loses bthenprintLoseMessage b
1919
else step2_computer b
2020

2121
step2_computer:: [[Int]]->IO()
@@ -26,13 +26,13 @@ step2_computer b = do
2626

2727
step3_check:: [[Int]]->IO()
2828
step3_check b=
29-
if wins bthendoputStrLn"Nice."
30-
elseif loses bthendoputStrLn"Oof."
29+
if wins bthenprintWinMessage
30+
elseif loses bthenprintLoseMessage b
3131
else step4_player b
3232

3333
step4_player:: [[Int]]->IO()
3434
step4_player b=do
35-
putStrLn"---"
35+
putStrLn""
3636
putStrLn (showboard b)
3737
putStrLn"Enter wasd:"
3838
move<-getLine
@@ -45,16 +45,27 @@ step4_player b = do
4545
step1_check b'
4646

4747
showboard:: [[Int]]->String
48-
showboard board=concat [
49-
if i`mod`5==0then
50-
if c==0then".\n"
51-
else (show c)++"\n"
52-
elseif c==0then"."
53-
else (show c)++""
54-
| (i, c)<-(enumerateboard board)]
48+
showboard b=
49+
let s=length b
50+
inconcat [
51+
if i`mod` s==0then
52+
if c==0then".\n"
53+
else (show c)++"\n"
54+
elseif c==0then"."
55+
else (show c)++""
56+
| (i, c)<-(enumerateboard b)]
5557

5658
enumerateboard:: [[Int]]-> [(Int,Int)]
57-
enumerateboard board=zip [1..] (concat board)
59+
enumerateboard b=zip [1..] (concat b)
60+
61+
printWinMessage::IO()
62+
printWinMessage=doputStrLn"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
7486
in
7587
replace2d index1 b
7688

@@ -87,11 +99,11 @@ replace 0 a (_:xs) = a:xs
8799
replace 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
95107
in (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
194206
loses:: [[Int]]->Bool
195-
loses b= (hasNoEmptyCells b)
207+
loses b= (not (hasNoEmptyCells b))
196208
&& (not (anyRowNeighboursMatch b))
197209
&& (not (anyColNeighboursMatch b))
198210

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp