Skip to content

Commit

Permalink
refactor: addresses elm review rules
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmcguin committed Mar 25, 2024
1 parent 7df6778 commit 582a3ef
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 50 deletions.
88 changes: 53 additions & 35 deletions src/Game.elm
Original file line number Diff line number Diff line change
Expand Up @@ -100,20 +100,23 @@ update msg model =
case ( model, msg ) of
( InProgress gameState, KeyPress key ) ->
let
guessWritable : Bool
guessWritable =
List.length gameState.currentGuess < 5

row =
getRow : Int -> List Letter
getRow idx =
gameState.board
|> LE.getAt gameState.currentRow
|> LE.getAt idx
|> Maybe.withDefault []

cell =
row
|> find (\( char, _ ) -> char == ' ')
findCell : List ( Char, b ) -> Maybe ( Int, ( Char, b ) )
findCell =
find (\( char, _ ) -> char == ' ')

updatedRow =
case cell of
updatedRow : List Letter -> List ( Char, LetterState )
updatedRow row =
case findCell row of
Just c ->
row
|> LE.updateAt (Tuple.first c) (\_ -> ( key, Pending ))
Expand All @@ -130,7 +133,7 @@ update msg model =
|> List.indexedMap
(\idx letterRow ->
if idx == gameState.currentRow then
updatedRow
updatedRow (getRow gameState.currentRow)

else
letterRow
Expand All @@ -153,33 +156,40 @@ update msg model =
_ ->
False

isSubmittable : Bool
isSubmittable =
gameState.board
|> LE.getAt gameState.currentRow
|> Maybe.map (List.all isPending)
|> Maybe.withDefault False

guess : String
guess =
gameState.board
|> LE.getAt gameState.currentRow
|> Maybe.map (List.map Tuple.first)
|> Maybe.map String.fromList
|> Maybe.withDefault ""

gameLost =
isSubmittable && guess /= gameState.solution && gameState.currentRow == 5 && wordIsValid guess
gameLost : String -> Bool
gameLost solution =
isSubmittable && guess /= solution && gameState.currentRow == 5 && wordIsValid guess

progressNextRow =
isSubmittable && guess /= gameState.solution && gameState.currentRow < 6 && wordIsValid guess
progressNextRow : Int -> Bool
progressNextRow currentRow =
isSubmittable && guess /= gameState.solution && currentRow < 6 && wordIsValid guess

shouldApplyGuess : Bool
shouldApplyGuess =
isSubmittable && gameState.currentRow < 6 && wordIsValid guess

isUnsupportedWord =
isSubmittable && guess /= gameState.solution && not (wordIsValid guess)
isUnsupportedWord : String -> Bool
isUnsupportedWord guessedWord =
isSubmittable && guessedWord /= gameState.solution && not (wordIsValid guessedWord)

message =
if isUnsupportedWord then
getMessage : Bool -> Maybe String
getMessage isUnsupported =
if isUnsupported then
Just "Not in word list"

else if not isSubmittable then
Expand All @@ -188,19 +198,22 @@ update msg model =
else
Nothing

board : List KeyboardRow
board =
if shouldApplyGuess then
applyGuess gameState

else
gameState.board

gameWon : Bool
gameWon =
board
|> LE.getAt gameState.currentRow
|> Maybe.map (List.all (\( _, state ) -> state == Correct))
|> Maybe.withDefault False

newDict : KeyboardDictionary
newDict =
updateKeyboardDict gameState.currentGuess gameState.keyboardDictionary gameState.solution
in
Expand All @@ -216,7 +229,7 @@ update msg model =
, showEndGameMessage
)

else if gameLost then
else if gameLost gameState.solution then
( GameEnd
{ solution = gameState.solution
, board = board
Expand All @@ -229,31 +242,35 @@ update msg model =
)

else
let
message =
getMessage (isUnsupportedWord guess)
in
( InProgress
{ gameState
| currentRow =
if progressNextRow then
if progressNextRow gameState.currentRow then
gameState.currentRow + 1

else
gameState.currentRow
, board = board
, currentGuess =
if not isUnsupportedWord then
if not (isUnsupportedWord guess) then
[]

else
gameState.currentGuess
, shakeRow =
if isUnsupportedWord || not isSubmittable then
if isUnsupportedWord guess || not isSubmittable then
Just gameState.currentRow

else
Nothing
, message = message
, keyboardDictionary = newDict
}
, Cmd.batch [ clearAnimation (isUnsupportedWord || not isSubmittable), clearAlert message ]
, Cmd.batch [ clearAnimation (isUnsupportedWord guess || not isSubmittable), clearAlert message ]
)

( InProgress gameState, Delete ) ->
Expand Down Expand Up @@ -333,7 +350,7 @@ update msg model =
)

_ ->
Debug.todo "handle unexpected transition"
( model, Cmd.none )



Expand Down Expand Up @@ -570,7 +587,7 @@ checkCorrectChar ch idx word currentLetterState =
String.indexes charAsString word

isCorrect =
List.any (\occ -> occ == idx) occurrences
List.member idx occurrences
in
if isCorrect then
Correct
Expand All @@ -582,15 +599,15 @@ checkCorrectChar ch idx word currentLetterState =
checkOtherStatesChar : Char -> String -> LetterState -> LetterState
checkOtherStatesChar ch word markedLetterState =
let
charAsString =
String.fromChar ch

occurrences =
String.indexes charAsString word

isPresent =
List.length occurrences > 0
isPresent : Char -> String -> Bool
isPresent char string =
char
|> String.fromChar
|> String.indexes string
|> List.length
|> (\len -> len > 0)

currentlyCorrect : Bool
currentlyCorrect =
case markedLetterState of
Correct ->
Expand All @@ -602,7 +619,7 @@ checkOtherStatesChar ch word markedLetterState =
if currentlyCorrect then
Correct

else if isPresent then
else if isPresent ch word then
Present

else
Expand Down Expand Up @@ -641,8 +658,9 @@ markOtherTiles activeGameRow solution boardIdx tiles =
_ ->
solutionChar

calculatedSolution =
List.map2 calcNewSolution tiles (String.toList solution) |> String.fromList |> String.trim
calculatedSolution : String -> String
calculatedSolution currentSolution =
List.map2 calcNewSolution tiles (String.toList currentSolution) |> String.fromList |> String.trim
in
if boardIdx == activeGameRow then
tiles
Expand All @@ -653,7 +671,7 @@ markOtherTiles activeGameRow solution boardIdx tiles =
tile

newLetterState =
checkOtherStatesChar char calculatedSolution currentLetterState
checkOtherStatesChar char (calculatedSolution solution) currentLetterState
in
( char, newLetterState )
)
Expand Down
14 changes: 1 addition & 13 deletions src/Main.elm
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,13 @@ main =


type Model
= Initial
| Game Game.Model
= Game Game.Model


init : D.Value -> ( Model, Cmd Msg )
init flags =
case D.decodeValue flagsDecoder flags of
Ok result ->
-- ( Game { game = Game.init result.word }, Cmd.none )
( Game <| Game.init result.word, Cmd.none )

Err _ ->
Expand All @@ -58,13 +56,6 @@ type Msg
update : Msg -> Model -> ( Model, Cmd Msg )
update msg model =
case ( model, msg ) of
( Initial, ToGame gameMsg ) ->
let
( gameModel, _ ) =
Game.init "" |> Game.update gameMsg
in
( Game gameModel, Cmd.none )

( Game gameState, ToGame gameMsg ) ->
let
( gameModel, gameCmd ) =
Expand Down Expand Up @@ -135,8 +126,5 @@ toKey key =
view : Model -> Html Msg
view model =
case model of
Initial ->
Game.init "" |> Game.view |> Html.map ToGame

Game gameState ->
Game.view gameState |> Html.map ToGame
4 changes: 2 additions & 2 deletions src/Words.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Words exposing (..)
module Words exposing (wordIsValid, getRandom, wordLength)

import Set exposing (Set(..))
import Set exposing (Set)
import List.Extra as LE


Expand Down

0 comments on commit 582a3ef

Please sign in to comment.