;; Later this can be substituted with a database call to pull a list of words.
(def word-collection #{"babyproof" "afar" "affray"
"afro" "arbor" "array"
"arroyo" "barb" "barf"
"boar" "boor" "bray"
"farro" "fora" "foray"
"fray" "parry" "poor"
"pray" "proof" "prop"
"pyro" "roar" "roof"
"ropy"})
(defn find-common-letter [word-set]
(reduce
set/intersection
(map set (seq word-set))))
(defn validate-word
"Checks the given word against the current word list and letter set to see if it is valid. Gives the following keywords as a result.
:submit-ok :too-short :invalid :no-common :not-in-list :other"
[word word-list letters common-letter]
(cond
(contains? word-list word) :submit-ok ; first check if the word is in the word-collection
(> 4 (count (seq word))) :too-short ; check length, notify if less than 3 letters
(not (every? letters (set word))) :invalid ; check if every letter in the word is in letters set
(not (contains? (set word) (first common-letter))) :no-common ; if it does not contain the common letter
(contains? (set word) (first common-letter)) :not-in-list ; then check if the word at least contains common letter
:else :other)) ; generic if it somehow manages to not match one of the above
(rf/reg-event-db ::submit-word
(fn [db [_ word]]
(let [letters (:letters db)
common-letter (:common-letter db)
words (:words db)
point-val (point-formula word letters)]
(case (validate-word word words letters common-letter)
:submit-ok (if (contains? (:found-words db) word)
(assoc db :message "You've already found that word!")
(-> db
(update :found-words conj word)
(update :score + point-val)
(assoc :message (str "Great job! You found " word ", worth a score of " point-val "!")))) ; add the valid word to found words
:too-short (assoc db :message "Only words with 4 letters or more count.")
:not-in-list (assoc db :message (str "Sorry, " word " isn't in the word list today."))
:no-common (assoc db :message "Nice try, but the word needs to contain the common letter.")
:invalid (assoc db :message "All letters in the word must be from the given letter set.")
:other (assoc db :message "Try again.")))))
(defn spawn-words-button
"Starts the game with a preset set of words."
[]
(let [game-started (rf/subscribe [::game-started])]
(when-not @game-started
[:button
{:on-click #(rf/dispatch [::set-words-and-letters word-collection])
:style button-style}
"Get Letters!"])))
(defn text-input
"Field for the user to input a word of their choosing."
[]
(let [letters (rf/subscribe [::letters])
common-letter (rf/subscribe [::common-letter])
input-value (rf/subscribe [::current-input])]
[:input {:type "text"
:placeholder "Type here!"
:value @input-value
:on-change #(rf/dispatch [::assoc-current-input (-> % .-target .-value)])
:style input-style}]))
(defn shuffle-order-button
"Shuffles the order of the letters displayed."
[display-letters]
[:button {:on-click #(rf/dispatch [::shuffle-letter-order display-letters])
:style button-style}
"Shuffle letters"])
(defn init []
(install-global-key-listeners) ; listen for keypress events
(rf/dispatch-sync [::initialize-db]) ; get re-frame atom initialized
(stylefy/init {:dom (stylefy-reagent/init)}) ; set up css
(dev-setup)
(mount-root))
I'm trying to get the input field's letters to change colour based on their validity.
To change the color of the input field's letters based on their validity according to certain conditions, you need to apply styles dynamically. You can achieve this by creating a validation function and using the letter-style function you've already defined.
Here is an example of how you can implement dynamic styling for the input letters:
First, ensure you have a utility function that checks if a letter is valid:
Next, you need to use the validate-letter function to apply styles to each letter. To do this within text-input, you have to transform the input value into individual characters, apply the validation to each character, and then display each character with a styled span.
Let's redefine the text-input function to incorporate dynamic styling:
clojure
(defn text-input
"Field for the user to input a word of their choosing."
[]
(let [letters @(rf/subscribe [::letters])
common-letter @(rf/subscribe [::common-letter])
input-value @(rf/subscribe [::current-input])]
[:div
[:input {:type "text"
:placeholder "Type here!"
:value input-value
:on-change #(rf/dispatch [::assoc-current-input (-> % .-target .-value)])
:style input-style}]
[:div
(for [letter (seq input-value)]
[:span (use-style (letter-style (validate-letter letter letters common-letter)))
letter])]))
This will cause each letter in the input field to be displayed as a span, and each span will have styles applied according to the result of validate-letter. As far as I can understand you are not using the function validate-letter, so I will provide an example of how it is used:
Replace the part in the text-input function that renders the input with individual styled spans by calling this styled-letter function within a for loop:
This will generate styled spans for each character typed in the input field, and each character's style will dynamically reflect its validity based on your game's rules.
Lastly, for this to work correctly you'll want to ensure that your validate-letter function is correct and that letter-style is applying styles as expected. You will be able to see the styled letters displayed below the input field as the user types. If you wanted these letters to replace the text inside the input, you would need to use a content-editable div or some JavaScript/CSS trickery since standard inputs only have a single style for the entire text content.
;; Later this can be substituted with a database call to pull a list of words.
(def word-collection #{"babyproof" "afar" "affray"
"afro" "arbor" "array"
"arroyo" "barb" "barf"
"boar" "boor" "bray"
"farro" "fora" "foray"
"fray" "parry" "poor"
"pray" "proof" "prop"
"pyro" "roar" "roof"
"ropy"})
(defn find-common-letter [word-set]
(reduce
set/intersection
(map set (seq word-set))))
(defn validate-word
"Checks the given word against the current word list and letter set to see if it is valid. Gives the following keywords as a result.
:submit-ok :too-short :invalid :no-common :not-in-list :other"
[word word-list letters common-letter]
(cond
(contains? word-list word) :submit-ok ; first check if the word is in the word-collection
(> 4 (count (seq word))) :too-short ; check length, notify if less than 3 letters
(not (every? letters (set word))) :invalid ; check if every letter in the word is in letters set
(not (contains? (set word) (first common-letter))) :no-common ; if it does not contain the common letter
(contains? (set word) (first common-letter)) :not-in-list ; then check if the word at least contains common letter
:else :other)) ; generic if it somehow manages to not match one of the above
(rf/reg-event-db ::submit-word
(fn [db [_ word]]
(let [letters (:letters db)
common-letter (:common-letter db)
words (:words db)
point-val (point-formula word letters)]
(case (validate-word word words letters common-letter)
:submit-ok (if (contains? (:found-words db) word)
(assoc db :message "You've already found that word!")
(-> db
(update :found-words conj word)
(update :score + point-val)
(assoc :message (str "Great job! You found " word ", worth a score of " point-val "!")))) ; add the valid word to found words
:too-short (assoc db :message "Only words with 4 letters or more count.")
:not-in-list (assoc db :message (str "Sorry, " word " isn't in the word list today."))
:no-common (assoc db :message "Nice try, but the word needs to contain the common letter.")
:invalid (assoc db :message "All letters in the word must be from the given letter set.")
:other (assoc db :message "Try again.")))))
(defn spawn-words-button!
"Starts the game with a preset set of words."
[]
(let [game-started (rf/subscribe [::game-started])]
(when-not @game-started
[:button
{:on-click #(rf/dispatch [::set-words-and-letters word-collection])
:style button-style}
"Get Letters!"])))
(defn text-input
"Field for the user to input a word of their choosing."
[]
(let [;letters (rf/subscribe [::letters])
;common-letter (rf/subscribe [::common-letter])
input-value (rf/subscribe [::current-input])]
[:input {:type "text"
:placeholder "Type here!"
:value @input-value
:on-change #(rf/dispatch [::assoc-current-input (-> % .-target .-value)])
:style input-style}]))
(defn shuffle-order-button!
"Shuffles the order of the letters displayed."
[display-letters]
[:button {:on-click #(rf/dispatch [::shuffle-letter-order display-letters])
:style button-style}
"Shuffle letters"])
(defn init []
(install-global-key-listeners) ; listen for keypress events
(rf/dispatch-sync [::initialize-db]) ; get re-frame atom initialized
(stylefy/init {:dom (stylefy-reagent/init)}) ; set up css
(dev-setup)
(mount-root))
How do I add key listener events for enter and backspace and have those submit the answer and remove one character?
To handle the "Enter" and "Backspace" key events, you can modify the global-key-handler function to listen for these specific keys and perform the corresponding actions. The Enter key should trigger the submission of the current input, while the Backspace key should remove the last character from the current input.
Here is how you can alter the global-key-handler function to handle these keys:
In the code above, we've modified the function to check if the key pressed is "Enter" or "Backspace". If it's "Enter", we dispatch the ::submit-word event with the current input value and then clear the input field. If it's "Backspace", we dispatch the ::assoc-current-input event with the input value minus its last character.
Now, you must attach this handler to the keypress event for regular keys and keydown event for special keys such as "Enter" and "Backspace". This is because the keypress event doesn't get fired for non-printable keys like "Backspace".
Let's update the install-global-key-listeners function to attach the global-key-handler to the keydown event:
Lastly, ensure that you replace the assoc with update in ::update-current-input if you want to concatenate the new letter to the existing input, rather than replace it. For appending single characters, you can simplify the ::update-current-input event handler like this:
Now the event will append the character to the current input value. Be sure the letter passed to the dispatch call should be a single character string.
These changes will make your application listen for keypresses of "Enter" and "Backspace" and handle them accordingly. When a user presses "Enter", the word will be submitted, and when they press "Backspace", the last character of the current input will be removed.