Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns mastercly.core
  2.     (:require [reagent.core :as reagent :refer [atom]]
  3.               [secretary.core :as secretary :include-macros true]
  4.               [accountant.core :as accountant]))
  5.  
  6. ;; -------------------------
  7. ;; Views
  8.  
  9. (defn initial-state [] {:goal [:blue :blue :blue :red]
  10.                         :attempts []
  11.                         :current-attempt [:blue :blue :blue :blue]})
  12. (def *game (atom (initial-state)))
  13.  
  14. (def colors [:blue :red :yellow :orange :green])
  15. (def size 4)
  16.  
  17. (defn color-selector [x idx]
  18.   [:input {:type "button"
  19.            :value x
  20.            :on-click #(swap! *game update-in [:current-attempt idx] (fn [] x))
  21.            :style (if (= x (get-in @*game [:current-attempt idx]))
  22.                     {:background-color x}
  23.                     {})}])
  24.  
  25. (defn score-render [attempt gold]
  26.   [:span
  27.     (let [total-correct (reduce + (map (fn [a g] (if (= a g) 1 0)) attempt gold))
  28.         color-correct (reduce + (map (fn [c] (let [in-attempt (filter (fn [x] (= c x)) attempt)
  29.                                            in-goal (filter (fn [x] (= c x)) gold)]
  30.                                                (min (count in-attempt) (count in-goal)))) colors))]
  31.     (str (- color-correct total-correct) " " total-correct)
  32.     )])
  33.  
  34. (defn attempt-report [attempt]
  35.   [:div (score-render attempt (:goal @*game)) (map (fn [x] (str x)) attempt)])
  36.  
  37. (defn home-page []
  38.   [:div [:h2 "Mastermind :)"]
  39.    [:input {:type "button" :value "start"
  40.             :on-click #(reset! *game (initial-state))}]
  41.    [:div (str @*game)]
  42.  
  43.    (map (fn [idx] [:div (map (fn [x] (color-selector x idx)) colors)]) (range 4))
  44.    [:input {:type "button"
  45.             :value "attempt!"
  46.             :on-click #(swap! *game assoc :attempts (conj (:attempts @*game) (:current-attempt @*game)))
  47.            }]
  48.  
  49.    (map attempt-report (reverse (:attempts @*game)))
  50.    
  51.    [:div [:a {:href "/about"} "go to about page"]]])
  52.  
  53.  
  54. (defn about-page []
  55.   [:div [:h2 "About mastercly"]
  56.    [:div [:a {:href "/"} "go to the home page!!!"]]])
  57.  
  58. ;; -------------------------
  59. ;; Routes
  60.  
  61. (def page (atom #'home-page))
  62.  
  63. (defn current-page []
  64.   [:div [@page]])
  65.  
  66. (secretary/defroute "/" []
  67.   (reset! page #'home-page))
  68.  
  69. (secretary/defroute "/about" []
  70.   (reset! page #'about-page))
  71.  
  72. ;; -------------------------
  73. ;; Initialize app
  74.  
  75. (defn mount-root []
  76.   (reagent/render [current-page] (.getElementById js/document "app")))
  77.  
  78. (defn init! []
  79.   (accountant/configure-navigation!
  80.     {:nav-handler
  81.      (fn [path]
  82.        (secretary/dispatch! path))
  83.      :path-exists?
  84.      (fn [path]
  85.        (secretary/locate-route path))})
  86.   (accountant/dispatch-current!)
  87.   (mount-root))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement