Skip to content

Commit 995b593

Browse files
author
Przemek Kaminski
committed
Refactor to Game.HSnake domain, some code cleanup
1 parent 39aea6a commit 995b593

File tree

10 files changed

+70
-62
lines changed

10 files changed

+70
-62
lines changed

driver/Main.hs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ module Main where
22

33
import Control.Lens
44

5-
import HSnake.AI
6-
import HSnake.Basic
7-
import HSnake.Game
8-
import HSnake.Graphics
9-
import HSnake.Player
10-
import HSnake.Snake
5+
import Game.HSnake.AI
6+
import Game.HSnake.Basic
7+
import Game.HSnake.Game
8+
import Game.HSnake.Graphics
9+
import Game.HSnake.Player
10+
import Game.HSnake.Snake
1111

1212
import Control.Monad
1313
import Control.Monad.State
@@ -66,6 +66,7 @@ handleInputHuman gs (Just (SDL.Event _ (SDL.KeyboardEvent k))) =
6666
handleInputKeyboard gs k
6767
handleInputHuman _ _ = return ()
6868

69+
handleInputKeyboard :: MonadState GameState m => GameState -> SDL.KeyboardEventData -> m ()
6970
handleInputKeyboard gs (SDL.KeyboardEventData _ SDL.Pressed False keysym) =
7071
case SDL.keysymKeycode keysym of
7172
SDL.KeycodeDown -> do
@@ -104,11 +105,10 @@ loop = do
104105

105106
gameState <- get
106107

107-
let ap = gameState^.applePosition
108+
let apple = gameState^.applePosition
108109
let ps = gameState^.players
109110

110111
if checkPlayersCollision ps
111-
-- TODO: increase level
112112
then error "Game over"
113113
else return ()
114114

@@ -129,7 +129,7 @@ loop = do
129129

130130
drawGame
131131

132-
let appleEatersInd = findIndices (playerEatsApple ap) ps
132+
let appleEatersInd = findIndices (playerEatsApple apple) ps
133133
if (length appleEatersInd > 0)
134134
then do
135135
newApplePosition <- liftIO $ getRandomApple (totalPlayersPosition ps)
@@ -159,9 +159,8 @@ computeAINextMoves = do
159159
gameState <- get
160160
put $ players .~ movedAI gameState $ gameState
161161
where
162+
aiIndices gs = findIndices (not . isHuman) (gs^.players)
162163
movedAI gs = mapToIndices (\pl -> setNextPlayerDirection pl (computeAIPlayerMove pl gs)) (gs^.players) (aiIndices gs)
163-
where
164-
aiIndices gs = findIndices (not . isHuman) (gs^.players)
165164

166165

167166
updatePlayerDirections :: ReaderT AppConfig AppState ()
@@ -202,8 +201,7 @@ drawGame = do
202201

203202
showGameMessages window gameState
204203

205-
--SDL.flip window
206-
SDL.surfaceBlit gameScreen Nothing screen Nothing
204+
_ <- SDL.surfaceBlit gameScreen Nothing screen Nothing
207205

208206
SDL.updateWindowSurface window
209207

haskell-snake.cabal

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
--
33
-- see: https://github.com/sol/hpack
44
--
5-
-- hash: 192b2c53b1c47172c4eb5a55573b1c4f70e7f7b0154cf198cf6d353efb1a6134
5+
-- hash: 60583e315dd2851d455eb1c3beba4cfd260b5fecb8b670efc23c1f0c6070679a
66

77
name: haskell-snake
88
version: 1.0.0
@@ -26,13 +26,13 @@ source-repository head
2626

2727
library
2828
exposed-modules:
29-
HSnake.AI
30-
HSnake.Basic
31-
HSnake.Board
32-
HSnake.Game
33-
HSnake.Graphics
34-
HSnake.Player
35-
HSnake.Snake
29+
Game.HSnake.AI
30+
Game.HSnake.Basic
31+
Game.HSnake.Board
32+
Game.HSnake.Game
33+
Game.HSnake.Graphics
34+
Game.HSnake.Player
35+
Game.HSnake.Snake
3636
other-modules:
3737
Paths_haskell_snake
3838
hs-source-dirs:

package.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ dependencies:
5858
library:
5959
source-dirs: src
6060
exposed-modules:
61-
- HSnake.AI
62-
- HSnake.Basic
63-
- HSnake.Board
64-
- HSnake.Game
65-
- HSnake.Graphics
66-
- HSnake.Player
67-
- HSnake.Snake
61+
- Game.HSnake.AI
62+
- Game.HSnake.Basic
63+
- Game.HSnake.Board
64+
- Game.HSnake.Game
65+
- Game.HSnake.Graphics
66+
- Game.HSnake.Player
67+
- Game.HSnake.Snake
6868

6969
executable:
7070
main: Main.hs

src/HSnake/AI.hs renamed to src/Game/HSnake/AI.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
module HSnake.AI where
1+
module Game.HSnake.AI where
22

33
import Control.Lens
44

5-
import HSnake.Basic
6-
import HSnake.Game
7-
import HSnake.Player
8-
import HSnake.Snake
5+
import Game.HSnake.Basic
6+
import Game.HSnake.Game
7+
import Game.HSnake.Player
8+
import Game.HSnake.Snake
99

1010
computeAIPlayerMove :: Player -> GameState -> Direction
1111
computeAIPlayerMove pl gameState = circleAIPlayer pl

src/HSnake/Basic.hs renamed to src/Game/HSnake/Basic.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Basic.hs -- basic functions and datatypes
2-
module HSnake.Basic where
2+
module Game.HSnake.Basic where
33

44
import Control.Lens
55

src/HSnake/Board.hs renamed to src/Game/HSnake/Board.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
-- Board.hs -- board logic
2-
module HSnake.Board where
2+
module Game.HSnake.Board where
33

44
import Control.Lens
55

6-
import HSnake.Basic
6+
import Game.HSnake.Basic
77

88
data Board = Board {
99
_xSize :: Int,

src/HSnake/Game.hs renamed to src/Game/HSnake/Game.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
module HSnake.Game where
1+
module Game.HSnake.Game where
22

3-
import HSnake.Basic
4-
import HSnake.Board
5-
import HSnake.Player
6-
import HSnake.Snake
3+
import Game.HSnake.Basic
4+
import Game.HSnake.Board
5+
import Game.HSnake.Player
6+
import Game.HSnake.Snake
77

88
import Control.Lens
99
import Data.Word
1010

1111
data GameState = GameState {
1212
_players :: [Player],
13-
_applePosition :: HSnake.Basic.Point,
13+
_applePosition :: Game.HSnake.Basic.Point,
1414
_board :: Board,
1515
_level :: Int,
1616
_lastTick :: Word32
@@ -20,7 +20,7 @@ makeLenses ''GameState
2020
initialGameState :: GameState
2121
initialGameState = GameState
2222
[initialPlayer, initialComputer]
23-
(HSnake.Basic.Point 0 0)
23+
(Game.HSnake.Basic.Point 0 0)
2424
initialBoard
2525
1
2626
0

src/HSnake/Graphics.hs renamed to src/Game/HSnake/Graphics.hs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
module HSnake.Graphics where
1+
module Game.HSnake.Graphics where
22

3-
import HSnake.Basic
4-
import HSnake.Board
5-
import HSnake.Game
6-
import HSnake.Player
7-
import HSnake.Snake
3+
import Game.HSnake.Basic
4+
import Game.HSnake.Board
5+
import Game.HSnake.Game
6+
import Game.HSnake.Player
7+
import Game.HSnake.Snake
88

99

1010
import Control.Lens
@@ -57,8 +57,8 @@ rectHeight :: Int
5757
rectHeight = gameScreenHeight `div` numRectsY
5858

5959

60-
rectFromPoint :: HSnake.Basic.Point -> SDLRect
61-
rectFromPoint (HSnake.Basic.Point x y) = SDL.Rectangle startPoint size
60+
rectFromPoint :: Game.HSnake.Basic.Point -> SDLRect
61+
rectFromPoint (Game.HSnake.Basic.Point x y) = SDL.Rectangle startPoint size
6262
where
6363
startX = (x - 1)*rectWidth + 1
6464
startY = (y - 1)*rectHeight + 1
@@ -68,7 +68,7 @@ rectFromPoint (HSnake.Basic.Point x y) = SDL.Rectangle startPoint size
6868
size = V2 (fromIntegral width) (fromIntegral height)
6969

7070

71-
rects = [rectFromPoint (HSnake.Basic.Point ptx pty) | ptx <- [1..numRectsX], pty <- [1..numRectsY]]
71+
rects = [rectFromPoint (Game.HSnake.Basic.Point ptx pty) | ptx <- [1..numRectsX], pty <- [1..numRectsY]]
7272
rectsVec = DVS.generate (numRectsX * numRectsY)
7373
(\i -> rects !! i )
7474

@@ -82,7 +82,7 @@ paintBoard :: SDL.Renderer -> IO ()
8282
paintBoard r = do
8383
paintRects r White
8484

85-
paintApple :: SDL.Renderer -> HSnake.Basic.Point -> IO ()
85+
paintApple :: SDL.Renderer -> Game.HSnake.Basic.Point -> IO ()
8686
paintApple r ap = do
8787
setColor r Red
8888
SDL.fillRect r $ Just rect

src/HSnake/Player.hs renamed to src/Game/HSnake/Player.hs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
module HSnake.Player where
1+
module Game.HSnake.Player where
22

33
import Control.Lens
44

5-
import HSnake.Basic
6-
import HSnake.Snake
5+
import Game.HSnake.Basic
6+
import Game.HSnake.Snake
77

88
import Data.List
99

@@ -24,18 +24,21 @@ data Player = Player {
2424

2525
makeLenses ''Player
2626

27+
initialPlayer :: Player
2728
initialPlayer = Player
2829
Human
2930
initialSnake
3031
(initialSnake^.direction)
3132
Green
3233

34+
initialPlayerBottom :: Player
3335
initialPlayerBottom = Player
3436
Human
3537
initialSnakeBottom
3638
(initialSnakeBottom^.direction)
3739
Green
3840

41+
initialComputer :: Player
3942
initialComputer = Player
4043
AI
4144
initialSnakeBottom

src/HSnake/Snake.hs renamed to src/Game/HSnake/Snake.hs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
-- Snake.hs: Snake-control logic
2-
module HSnake.Snake where
2+
module Game.HSnake.Snake where
33

44
import Control.Lens
55

6-
import HSnake.Basic
6+
import Game.HSnake.Basic
77

88
import Data.List (nub, sort)
99

@@ -46,19 +46,26 @@ checkCollision snake = outOfBoundary || selfCollision
4646
selfCollision = (length (nub snakePosition)) /= (length snakePosition)
4747

4848
-- | Starting position & body
49+
initialSnakePosition :: [Point]
4950
initialSnakePosition = [Point 6 6]
51+
52+
initialSnakePositionBottom :: [Point]
5053
initialSnakePositionBottom = [Point 6 11]
54+
55+
initialSnake :: Snake
5156
initialSnake = Snake initialSnakePosition North 1
57+
58+
initialSnakeBottom :: Snake
5259
initialSnakeBottom = Snake initialSnakePositionBottom North 1
5360

5461
snakeEatsApple :: Snake -> Point -> Bool
5562
snakeEatsApple snake apple = apple `elem` (snake^.position)
5663

5764
-- | Don't allow to change to opposite direction immediately
5865
tryChangeSnakeDirection :: Direction -> Direction -> Direction
59-
tryChangeSnakeDirection direction snakeDirection
60-
| oppositeDirections direction snakeDirection = snakeDirection
61-
| otherwise = direction
66+
tryChangeSnakeDirection d sd
67+
| oppositeDirections d sd = sd
68+
| otherwise = d
6269

6370
changeSnakeDirection :: Snake -> Direction -> Snake
64-
changeSnakeDirection snake dir = direction .~ (tryChangeSnakeDirection dir (snake^.direction)) $ snake
71+
changeSnakeDirection s d = direction .~ (tryChangeSnakeDirection d (s^.direction)) $ s

0 commit comments

Comments
 (0)