Skip to content

Commit

Permalink
Add message when player clicks buttons out of turn. (#64)
Browse files Browse the repository at this point in the history
* feat(Game): Add message that appears when the player attempts to select a piece when it is not their turn to do so - I48

* Add status message reset to ComputerSelectedPiece piece, InPlay Computer ChoosingPiece
  • Loading branch information
dominicduffin1 authored Nov 29, 2020
1 parent 58faafd commit f4e4bed
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/Game.elm
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ module Game exposing
, Msg(..)
, Player(..)
, Turn(..)
, StatusMessage(..)
, currentStatus
, gameboard
, init
, nameToString
, pieceToString
, playerToString
, remainingPieces
, currentStatusMessage
, update
)

Expand Down Expand Up @@ -68,9 +70,13 @@ type GameStatus
| Won Winner
| Draw

type StatusMessage
= NoMessage
| SomePiecePlayedWhenNotPlayersTurn


type Model
= Model { board : Board, status : GameStatus }
= Model { board : Board, status : GameStatus, statusMessage : StatusMessage }



Expand All @@ -82,9 +88,13 @@ initStatus =
InPlay Human ChoosingPiece


initStatusMessage : StatusMessage
initStatusMessage = NoMessage


init : Model
init =
Model { board = Board.init, status = initStatus }
Model { board = Board.init, status = initStatus, statusMessage = initStatusMessage }



Expand Down Expand Up @@ -113,6 +123,10 @@ update msg (Model model) =
|> map (nextPlayerStartsPlaying Human piece)
|> andThen (computerChooses ComputerSelectedCell Board.openCells)

( HumanSelectedPiece piece, _ ) ->
Model { model | statusMessage = SomePiecePlayedWhenNotPlayersTurn }
|> noCmds

( ComputerSelectedCell name, InPlay Computer (ChoosingCellToPlay piece) ) ->
Model model
|> noCmds
Expand All @@ -127,12 +141,12 @@ update msg (Model model) =
)

( ComputerSelectedPiece piece, InPlay Computer ChoosingPiece ) ->
Model model
Model { model | statusMessage = NoMessage }
|> noCmds
|> map (nextPlayerStartsPlaying Computer piece)

( HumanSelectedCell name, InPlay Human (ChoosingCellToPlay piece) ) ->
Model model
Model { model | statusMessage = NoMessage }
|> noCmds
|> map (playerTryPlay name piece)
|> (\( maybeModel, c ) ->
Expand Down Expand Up @@ -270,6 +284,11 @@ currentStatus (Model model) =
model.status


currentStatusMessage : Model -> StatusMessage
currentStatusMessage (Model model) =
model.statusMessage


playerToString : Player -> String
playerToString player =
case player of
Expand Down
10 changes: 10 additions & 0 deletions src/Pages/Top.elm
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import Game
, Msg(..)
, Player(..)
, Turn(..)
, StatusMessage(..)
)
import Game.Core
exposing
Expand Down Expand Up @@ -154,6 +155,7 @@ view model =
, viewRemainingPieces (Game.remainingPieces model.game)
]
, viewGamestatus (Game.currentStatus model.game) model.dimensions
, viewStatusMessage (Game.currentStatusMessage model.game)
]
]
}
Expand Down Expand Up @@ -222,6 +224,14 @@ viewGamestatus gamestatus dimensions =
[ script ]


viewStatusMessage : StatusMessage -> Element Msg
viewStatusMessage statusMessage =
case statusMessage of
NoMessage ->
Element.el [] (Element.text "")
SomePiecePlayedWhenNotPlayersTurn ->
Element.el [ centerX, Font.center, Region.announce ] (Element.text "It's not your turn to choose a piece!")

viewCell : Cell -> Element Msg
viewCell { name, status } =
case status of
Expand Down

0 comments on commit f4e4bed

Please sign in to comment.