Skip to content

Commit

Permalink
Merge pull request #11 from pietroppeter/word-refactor
Browse files Browse the repository at this point in the history
Word type and model refactor (+ test data)
  • Loading branch information
pietroppeter authored Nov 26, 2023
2 parents 6f50db9 + 3257980 commit 8a4b059
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 24 deletions.
7 changes: 6 additions & 1 deletion notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ color-words
- [x] colors (grid)
- [x] background colors
- [x] border
- [x] font color (and size)
- [x] font color (and size)

refactor-word: goal is to refactor word to be able to show complete guess, current guess and empty words
- [x] new Word type
- [x] refactor model (add current)
- [x] new test data for init model
95 changes: 72 additions & 23 deletions src/Main.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Main exposing (..)
module Main exposing (main)

import Array exposing (Array)
import Browser
import Element exposing (..)
import Element.Background as Background
Expand All @@ -14,18 +15,20 @@ main =


type alias Model =
{ guesses : List (List Char)
{ guesses : List Word
, current : Word
, solution : List Char
}


init : Model
init =
{ guesses =
[ [ 'A', 'B', 'C', 'D', 'E' ]
, [ 'A', 'B', 'C' ]
[ testGuess1
, testGuess2
]
, solution = []
, current = testCurrent
, solution = testSolution
}


Expand All @@ -47,7 +50,7 @@ update msg model =
view : Model -> Html Msg
view model =
layout [ width fill, height fill ]
(column [ width (px 500), height fill, centerX, bgCyan ]
(column [ width (fill |> maximum 500), height fill, centerX, bgCyan ]
[ viewHeader
, viewGridArea model
, viewKeyboardArea
Expand Down Expand Up @@ -87,18 +90,22 @@ type Match
= No --grey
| Exact --green
| Almost --yellow
| Unmatched
| Unmatched --white


type alias MatchedTile =
type alias Letter =
{ char : Char
, match : Match
}


type Tile
= EmptyTile
| FilledTile MatchedTile
| FilledTile Letter


type alias Word =
List Tile


viewGridArea model =
Expand All @@ -109,8 +116,8 @@ emptyTile =
EmptyTile


defaultWord : List Tile
defaultWord =
testWord : Word
testWord =
[ FilledTile { char = 'A', match = No }
, FilledTile { char = 'B', match = Exact }
, FilledTile { char = 'C', match = Almost }
Expand All @@ -119,24 +126,59 @@ defaultWord =
]


testGuess1 =
[ FilledTile { char = 'P', match = No }
, FilledTile { char = 'O', match = No }
, FilledTile { char = 'S', match = No }
, FilledTile { char = 'T', match = No }
, FilledTile { char = 'A', match = Exact }
]


testGuess2 =
[ FilledTile { char = 'F', match = Almost }
, FilledTile { char = 'U', match = Exact }
, FilledTile { char = 'R', match = No }
, FilledTile { char = 'B', match = Almost }
, FilledTile { char = 'A', match = Exact }
]


testCurrent =
[ FilledTile { char = 'B', match = Unmatched }
, FilledTile { char = 'U', match = Unmatched }
, FilledTile { char = 'F', match = Unmatched }
]


testSolution =
[ 'B', 'U', 'F', 'F', 'A' ]


emptyWord =
List.repeat 5 emptyTile


padRightTake n padFill aList =
List.take n (aList ++ List.repeat n padFill)


getWords model =
padRightTake
6
emptyWord
(model.guesses ++ [ padRightTake 5 EmptyTile model.current ])


viewGrid model =
column [ centerX, centerY, spacing 5 ]
(List.map viewTileRow
(List.repeat 6 defaultWord)
{--
(List.take 6
(model.guesses ++ List.repeat 6 [])
)
-}
)
(List.map viewTileRow (getWords model))


viewTileRow word =
row [ spacing 5 ]
(List.map viewTile
(List.take 5
(word ++ List.repeat 5 emptyTile)
)
(padRightTake 5 emptyTile word)
)


Expand Down Expand Up @@ -215,7 +257,14 @@ viewTileChar tile =
el [ centerX, centerY ] (text (String.fromChar ' '))

FilledTile ftile ->
el [ centerX, centerY, Font.color (tileFontColor ftile.match), Font.size 32, Font.bold ] (text (String.fromChar ftile.char))
el
[ centerX
, centerY
, Font.color (tileFontColor ftile.match)
, Font.size 32
, Font.bold
]
(text (String.fromChar ftile.char))



Expand Down

0 comments on commit 8a4b059

Please sign in to comment.