Skip to content

Commit 81d8dff

Browse files
committed
rename cell constructing functions
1 parent e46ecde commit 81d8dff

File tree

4 files changed

+85
-79
lines changed

4 files changed

+85
-79
lines changed

src/Tetris/Model/Board.hs

+19-17
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ module Tetris.Model.Board
66
,board
77
,blocks
88
,clearLines
9-
,empty
10-
,full
9+
,emptyCell
10+
,fullCell
1111
,overlapsAt
1212
,rotated
1313
,spliceBoardAt
@@ -47,13 +47,14 @@ composeN n fun = foldr (.) id (replicate n fun)
4747
toBoard :: Block -> Board
4848
toBoard (Block b) = board (toCells b)
4949
where
50-
toCells I = [[full], [full], [full], [full]]
51-
toCells J = [[empty,full], [empty,full], [full,full]]
52-
toCells L = [[full,empty], [full,empty], [full,full]]
53-
toCells O = [[full,full], [full,full]]
54-
toCells S = [[empty, full, full], [full, full, empty]]
55-
toCells T = [[full,full,full], [empty,full,empty]]
56-
toCells Z = [[full, full, empty], [empty, full, full]]
50+
(e,f) = (emptyCell, fullCell)
51+
toCells I = [[f], [f], [f], [f]]
52+
toCells J = [[e,f], [e,f], [f,f]]
53+
toCells L = [[f,e], [f,e], [f,f]]
54+
toCells O = [[f,f], [f,f]]
55+
toCells S = [[e,f,f], [f,f,e]]
56+
toCells T = [[f,f,f], [e,f,e]]
57+
toCells Z = [[f,f,e], [e,f,f]]
5758

5859
board :: [[Cell]] -> Board
5960
board cs = Board { boardCells = concat cs
@@ -64,16 +65,16 @@ board cs = Board { boardCells = concat cs
6465
boardRows :: Board -> [[Cell]]
6566
boardRows b = mapChunks id (boardWidth b) (boardCells b)
6667

67-
empty,full :: Cell
68-
empty = Cell False
69-
full = Cell True
68+
emptyCell,fullCell :: Cell
69+
emptyCell = Cell False
70+
fullCell = Cell True
7071

7172
clearLines :: Board -> (Int, Board)
7273
clearLines b = clearLines' 0 (boardWidth b) [] (boardRows b)
7374

7475
clearLines' :: Int -> Int -> [[Cell]] -> [[Cell]] -> (Int, Board)
7576
clearLines' n w acc [] = (n, board (pad acc))
76-
where pad acc = replicate n (replicate w empty) ++ (reverse acc)
77+
where pad acc = replicate n (replicate w emptyCell) ++ (reverse acc)
7778
clearLines' n w acc (r:rs) = if allFull r
7879
then clearLines' (n+1) w acc rs
7980
else clearLines' n w (r:acc) rs
@@ -102,10 +103,11 @@ isLegalOffset b1 (x,y) b2
102103

103104
paddedTo :: Board -> (Int, Int) -> Board -> [Cell]
104105
paddedTo parent (x,y) child =
105-
let rowWidth = boardWidth parent
106-
emptyRow = take rowWidth (repeat empty)
107-
paddingFront = take x (repeat empty)
108-
padRow r = take rowWidth (paddingFront ++ r ++ (repeat empty))
106+
let emptyCells = (repeat emptyCell)
107+
rowWidth = boardWidth parent
108+
emptyRow = take rowWidth emptyCells
109+
paddingFront = take x emptyCells
110+
padRow r = take rowWidth (paddingFront ++ r ++ emptyCells)
109111
paddedChildRows = concatMap padRow (boardRows child)
110112
in concat (take y (repeat emptyRow))
111113
++ paddedChildRows

src/Tetris/Model/Game.hs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ freshGame randomSeed =
2424
, gameBlockRot = 0, gameTick = 1
2525
, gameScore = 0, gameOver = False
2626
, gameNewBlockNeeded = False}
27-
where cells = repeat empty
27+
where cells = repeat emptyCell
2828
rows = repeat (take defaultWidth cells)
2929
defaultBoard = board $ take defaultHeight rows
3030
(defaultWidth, defaultHeight) = (10,20)

src/Tetris/View.hs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module Tetris.View (showGame) where
22
import Tetris.Chunks (mapChunks)
33
import Tetris.Model.Game (Game(..))
4-
import Tetris.Model.Board (Board(..), full, rotated, spliceBoardAt)
4+
import Tetris.Model.Board (Board(..), fullCell, rotated, spliceBoardAt)
55

66
showGame :: Game -> String
77
showGame g@Game{..} =
@@ -13,4 +13,4 @@ showBoard :: Board -> String
1313
showBoard b@Board{..} = concat $ mapChunks showRow boardWidth boardCells
1414
where
1515
showRow cs = map showCell cs ++ "\n"
16-
showCell c = if c == full then '\9608' else '.'
16+
showCell c = if c == fullCell then '\9608' else '.'

test/Spec.hs

+63-59
Original file line numberDiff line numberDiff line change
@@ -26,87 +26,91 @@ runTests = mapM_ (\(Test expect expr)-> assert expect expr)
2626
spliceBoardAtTests :: [Test Board]
2727
spliceBoardAtTests =
2828
[
29-
board [[full, empty]] =?~ spliceBoardAt (board [[empty, empty]])
29+
board [[f, e]] =?~ spliceBoardAt (board [[e, e]])
3030
(0,0)
31-
(board [[full,empty]])
32-
, board [[full, full]] =?~ spliceBoardAt (board [[empty, full]])
31+
(board [[f,e]])
32+
, board [[f, f]] =?~ spliceBoardAt (board [[e, f]])
3333
(0,0)
34-
(board [[full, empty]])
35-
, board [[empty, empty]
36-
,[empty, full]
37-
,[empty, empty]]
38-
=?~ spliceBoardAt (board [[empty, empty]
39-
,[empty, empty]
40-
,[empty, empty]
34+
(board [[f, e]])
35+
, board [[e, e]
36+
,[e, f]
37+
,[e, e]]
38+
=?~ spliceBoardAt (board [[e, e]
39+
,[e, e]
40+
,[e, e]
4141
])
4242
(1,1)
43-
(board [[full]])
44-
, board [[empty, empty, empty]
45-
,[empty, full, full]
46-
,[empty, full, empty]
43+
(board [[f]])
44+
, board [[e, e, e]
45+
,[e, f, f]
46+
,[e, f, e]
4747
]
4848
=?~
49-
spliceBoardAt (board [[empty, empty, empty]
50-
,[empty, full, empty]
51-
,[empty, empty, empty]
49+
spliceBoardAt (board [[e, e, e]
50+
,[e, f, e]
51+
,[e, e, e]
5252
])
5353
(1,1)
54-
(board [[full,full]
55-
,[full,empty]])
54+
(board [[f,f]
55+
,[f,e]])
5656
]
5757

5858
overlapsAtTests =
5959
[
60-
True =?~ overlapsAt (board [[empty]]) (-1,0) (board [[full]])
61-
,True =?~ overlapsAt (board [[empty]]) (0,-1) (board [[full]])
62-
,True =?~ overlapsAt (board [[empty, empty]]) (1,0) (board [[full, full]])
63-
,True =?~ overlapsAt (board [[empty, empty]
64-
,[empty, empty]]) (0,1)
65-
(board [[empty, full]
66-
,[empty, full]])
67-
,False =?~ overlapsAt (board [[empty]]) (0,0) (board [[empty]])
68-
,False =?~ overlapsAt (board [[full]]) (0,0) (board [[empty]])
69-
,False =?~ overlapsAt (board [[full, full]
70-
,[empty, full]])
60+
True =?~ overlapsAt (board [[e]]) (-1,0) (board [[f]])
61+
,True =?~ overlapsAt (board [[e]]) (0,-1) (board [[f]])
62+
,True =?~ overlapsAt (board [[e, e]]) (1,0) (board [[f, f]])
63+
,True =?~ overlapsAt (board [[e, e]
64+
,[e, e]]) (0,1)
65+
(board [[e, f]
66+
,[e, f]])
67+
,False =?~ overlapsAt (board [[e]]) (0,0) (board [[e]])
68+
,False =?~ overlapsAt (board [[f]]) (0,0) (board [[e]])
69+
,False =?~ overlapsAt (board [[f, f]
70+
,[e, f]])
7171
(0,0)
72-
(board [[empty, empty]
73-
,[full, empty]])
74-
,True =?~ overlapsAt (board [[full]]) (0,0) (board [[full]])
75-
,False =?~ overlapsAt (board [[full, full, full]
76-
,[full, empty, full]
77-
,[full, full, full]
78-
]) (1,1) (board [[full]])
79-
,True =?~ overlapsAt (board [[full, full, full]
80-
,[full, empty, full]
81-
,[full, full, full]
82-
]) (1,2) (board [[full]])
72+
(board [[e, e]
73+
,[f, e]])
74+
,True =?~ overlapsAt (board [[f]]) (0,0) (board [[f]])
75+
,False =?~ overlapsAt (board [[f, f, f]
76+
,[f, e, f]
77+
,[f, f, f]
78+
]) (1,1) (board [[f]])
79+
,True =?~ overlapsAt (board [[f, f, f]
80+
,[f, e, f]
81+
,[f, f, f]
82+
]) (1,2) (board [[f]])
8383
]
8484

8585

8686
blocksTests =
8787
[
88-
board [[full, full],[full,full]] =?~ rotated 0 (Block O)
89-
,board [[full, full, full]
90-
,[empty, full, empty]] =?~ rotated 0 (Block T)
91-
,board [[full, empty]
92-
,[full, full]
93-
,[full, empty]] =?~ rotated 1 (Block T)
94-
,board [[empty, full, empty]
95-
,[full, full, full]] =?~ rotated 2 (Block T)
96-
,board [[empty, full]
97-
,[full, full]
98-
,[empty ,full]] =?~ rotated 3 (Block T)
88+
board [[f, f],[f,f]] =?~ rotated 0 (Block O)
89+
,board [[f, f, f]
90+
,[e, f, e]] =?~ rotated 0 (Block T)
91+
,board [[f, e]
92+
,[f, f]
93+
,[f, e]] =?~ rotated 1 (Block T)
94+
,board [[e, f, e]
95+
,[f, f, f]] =?~ rotated 2 (Block T)
96+
,board [[e, f]
97+
,[f, f]
98+
,[e ,f]] =?~ rotated 3 (Block T)
9999
,rotated 4 (Block T) =?~ rotated 0 (Block T)
100100
,rotated (-1) (Block T) =?~ rotated 3 (Block T)
101101
]
102102

103103
clearLinesTests =
104104
[
105-
(1, board [[empty, empty]]) =?~ clearLines (board [[full, full]])
106-
,(1, board [[empty, empty],
107-
[full, empty]]) =?~ clearLines (board [[full, empty]
108-
,[full, full]])
109-
,(2, board [[empty, empty],
110-
[empty, empty]]) =?~ clearLines (board [[full, full]
111-
,[full, full]])
105+
(1, board [[e, e]]) =?~ clearLines (board [[f, f]])
106+
,(1, board [[e, e],
107+
[f, e]]) =?~ clearLines (board [[f, e]
108+
,[f, f]])
109+
,(2, board [[e, e],
110+
[e, e]]) =?~ clearLines (board [[f, f]
111+
,[f, f]])
112112
]
113+
114+
115+
e = emptyCell
116+
f = fullCell

0 commit comments

Comments
 (0)