Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring from class! #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/com/github/dbasner/this_or_that/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
[goog.functions :as gfunctions]
[oops.core :refer [ocall]]
[com.github.dbasner.this-or-that.game-logic]
[com.github.dbasner.this-or-that.interaction :refer [trigger-render! init-watchers! init-dom-events!]]))
[com.github.dbasner.this-or-that.interaction :refer [trigger-render! init-watchers! attach-dom-events!]]))

(def init!
(gfunctions/once
(fn []
(js/console.log "Initializing This-Or-That!!!!")
(init-dom-events!)
(attach-dom-events!)
(init-watchers!)
(trigger-render!))))

Expand All @@ -18,4 +18,4 @@
(trigger-render!))


(ocall js/window "addEventListener" "load" init!)
(ocall js/window "addEventListener" "load" init!)
210 changes: 140 additions & 70 deletions src/main/com/github/dbasner/this_or_that/game_logic.cljs
Original file line number Diff line number Diff line change
@@ -1,71 +1,141 @@
(ns com.github.dbasner.this-or-that.game-logic
(:require [ajax.core :refer [GET POST]]))

;;OK so how does this game work?
;; what is it's core?
;; generate two possibilities
;; a or b
;; would you rather
;; [verb] [count] [nouns] or [verb] [count] [nouns] ?

;; start by doing just generating a basic string

(def verbs ["eat" "cook" "punch" "get punched by"
"get kicked by" "kick" "date" "kiss"
"marry" "love" "get laughed at by"
"live with" "barbecue" "get yelled at by"
"upset" "bother" "take a roadtrip with"
"fly sitting next to" "fall for"
"get trapped on a desert island with"
"become conjoined twins with"
"be trapped with" "be looked up to by"
"settle down with" "burn" "get wasted with"])

(def count-modifiers ["roughly" "about" "precisely" "exactly" "almost" "just about"])

(def nouns ["sharks" "people" "dogs" "turtles"
"chairs" "tables" "dinosaurs" "computers"
"men" "women" "cats" "rats" "couches"
"airplanes" "cars" "haters" "phones"
"jugglers" "clowns" "doctors" "therapists"
"lawyers" "artists" "hipsters"])

(defn rand-verb [] (rand-nth verbs))
(defn rand-count-modifier [] (rand-nth count-modifiers))
(defn rand-situation-count [n] (+ (rand-int n) 2))
(defn rand-noun [] (rand-nth nouns))

(defn generate-situation []
{:verb (rand-verb)
:count-modifier (rand-count-modifier)
:count (rand-situation-count 20)
:noun (rand-noun)})

(defn handler [response]
(.log js/console (str response)))

(defn error-handler [{:keys [status status-text]}]
(.log js/console (str "something bad happened: " status " " status-text)))


(comment
(GET "https://wordsapiv1.p.rapidapi.com/words/?partOfSpeech=noun?random=true"
{:headers {:x-rapidapi-key "YOUR_API_KEY_HERE"
:x-rapidapi-host "wordsapiv1.p.rapidapi.com"}}
:handler handler
:error-handler error-handler)

(GET "https://webknox-words.p.rapidapi.com/words/{WORD}/plural"
{:headers {:x-rapidapi-key "YOUR_API_KEY_HERE"
:x-rapidapi-host "wordsapiv1.p.rapidapi.com"}}
:handler handler
:error-handler error-handler))









"core game logic"
(:require
[ajax.core :refer [GET POST]]
[com.github.dbasner.this-or-that.game-logic.situation-generator :refer [random-situation]]
[com.github.dbasner.this-or-that.state :refer [state initial-state]]))

(defn add-vote
[state player-id situation-id]
(update-in state [:current-round-votes situation-id] conj player-id))

(defn rotate-to-next-player
"increment the current player index or reset to 0 if we are ready for the next round"
[state]
(let [num-players (-> state :player-ids count)
max-player-idx (dec num-players)
voter-index (:current-voter-index state)]
(if (< voter-index max-player-idx)
(update-in state [:current-voter-index] inc)
(assoc state :current-voter-index 0))))

(defn increment-score [state player-id]
(update-in state [:scores player-id] inc))

(defn increment-player-scores
"for a collection of player ids, increment the score"
[state player-ids]
(reduce #(increment-score %1 %2) state player-ids))

(defn add-scores
[old-state]
(let [situationA-votes (get-in old-state [:current-round-votes :situationA])
;; FYI - these are all the same
; situationB-votes (:situationB (:current-round-votes old-state))
; situationB-votes (-> old-state :current-round-votes :situationB)
situationB-votes (get-in old-state [:current-round-votes :situationB])

situationA-count (count situationA-votes)
situationB-count (count situationB-votes)]
(cond
(= situationA-count situationB-count) (increment-player-scores old-state (:player-ids state))
(> situationA-count situationB-count) (increment-player-scores old-state situationA-votes)
(< situationA-count situationB-count) (increment-player-scores old-state situationB-votes)
:else old-state)))

(defn generate-new-situations
[state]
(-> state
(assoc :situationA (random-situation))
(assoc :situationB (random-situation))))

(defn reset-votes
[state]
(-> state
(assoc-in [:current-round-votes :situationA] [])
(assoc-in [:current-round-votes :situationB] [])))

;; FYI - another way to write this
; (update-in state [:current-round-votes] merge {:situationA []
; :situationB []}))

(defn dec-rounds-left
[state]
(update-in state [:rounds-left] dec))

(defn decide-winners
[state]
(let [scores (:scores state)
max-value (apply max (vals scores))
winners (filter #(= (second %) max-value) scores)]
(assoc-in state [:winners] (keys winners))))

(defn prepare-next-round [state]
(-> state
add-scores
dec-rounds-left
reset-votes
generate-new-situations))

(defn apply-votes-and-rotate-player [state player-id situation-id]
(-> state
(add-vote player-id situation-id)
rotate-to-next-player))

(defn process-vote
"processes voting"
[state player-id situation-id]
(let [default-next-state (apply-votes-and-rotate-player state player-id situation-id)
is-new-round? (zero? (:current-voter-index default-next-state))
new-round-state (prepare-next-round default-next-state)
last-round? (zero? (:rounds-left new-round-state))
game-over? (and is-new-round? last-round?)
start-next-round? (and is-new-round? (not last-round?))]
(cond
game-over? (decide-winners new-round-state)
start-next-round? new-round-state
;; else switch to the next player
:else default-next-state)))

(defn generate-player-ids
"returns a Vector of player id Strings: ['Player-1', 'Player-2', 'Player-3']"
[player-count]
(mapv #(str "Player-" (inc %)) (range player-count)))
;; Original:
; (vec (for [n (range player-count)]
; (str "Player-" (+ n 1)))))

(defn generate-new-game-scores [player-ids]
(apply assoc {} (interleave player-ids
(repeat (count player-ids) 0))))

(defn reset-game-state
"reset the game to a fresh state"
[state player-count]
(let [player-ids (generate-player-ids player-count)]
(-> state
(assoc-in [:player-ids] player-ids)
(assoc-in [:scores] (generate-new-game-scores player-ids))
(assoc-in [:current-voter-index] 0)
(generate-new-situations))))


; (defn handler [response]
; (.log js/console (str response)))
;
; (defn error-handler [{:keys [status status-text]}]
; (.log js/console (str "something bad happened: " status " " status-text)))
;
;
; (comment
; (GET "https://wordsapiv1.p.rapidapi.com/words/?partOfSpeech=noun?random=true"
; {:headers {:x-rapidapi-key "YOUR_API_KEY_HERE"
; :x-rapidapi-host "wordsapiv1.p.rapidapi.com"}}
; :handler handler
; :error-handler error-handler)
;
; (GET "https://webknox-words.p.rapidapi.com/words/{WORD}/plural"
; {:headers {:x-rapidapi-key "YOUR_API_KEY_HERE"
; :x-rapidapi-host "wordsapiv1.p.rapidapi.com"}}
; :handler handler
; :error-handler error-handler))
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
(ns com.github.dbasner.this-or-that.game-logic.situation-generator
"generate the random Situation sentences")

(def verbs ["eat" "cook" "punch" "get punched by"
"get kicked by" "kick" "date" "kiss"
"marry" "love" "get laughed at by"
"live with" "barbecue" "get yelled at by"
"upset" "bother" "take a roadtrip with"
"fly sitting next to" "fall for"
"get trapped on a desert island with"
"become conjoined twins with"
"be trapped with" "be looked up to by"
"settle down with" "burn" "get wasted with"])

(def count-modifiers ["roughly" "about" "precisely" "exactly" "almost" "just about"])

(def nouns ["sharks" "people" "dogs" "turtles"
"chairs" "tables" "dinosaurs" "computers"
"men" "women" "cats" "rats" "couches"
"airplanes" "cars" "haters" "phones"
"jugglers" "clowns" "doctors" "therapists"
"lawyers" "artists" "hipsters"])

(defn rand-verb [] (rand-nth verbs))
(defn rand-count-modifier [] (rand-nth count-modifiers))
(defn rand-situation-count [n] (+ (rand-int n) 2))
(defn rand-noun [] (rand-nth nouns))

;; -----------------------------------------------------------------------------
;; Public API

(defn random-situation
[]
{:verb (rand-verb)
:count-modifier (rand-count-modifier)
:count (rand-situation-count 20)
:noun (rand-noun)})
10 changes: 4 additions & 6 deletions src/main/com/github/dbasner/this_or_that/html.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
:name "new-game-player-count"
:value 3
:min 2}]
[:input#startGameBtn {:class "button **is-large is-success is-rounded**" :type "submit" :value "Start Game!"}]])
[:input#startGameBtn.button.is-success.is-rounded
{:type "submit" :value "Start Game!"}]])


(defn DescribeSituation
Expand Down Expand Up @@ -97,8 +98,8 @@
[app-state]
(let
[result (:winners app-state)
game-started? (not= 0 (count (:player-ids app-state)))
game-over? (not= 0 (count (:winners app-state)))
game-started? (not (zero? (count (:player-ids app-state))))
game-over? (not (zero? (count (:winners app-state))))
player-ids (:player-ids app-state)
player-index (:current-voter-index app-state)]
[:main.text-center
Expand All @@ -112,6 +113,3 @@
[:div
(PlayerTurnBanner (nth player-ids player-index))
(SituationOptions (:situationA app-state) (:situationB app-state))])])]))



Loading