<commit_before>(ns triboard.logic.turn
  (:require
    [cljs.spec :as s :include-macros true]
    [triboard.logic.constants :as cst]
    [triboard.logic.board :as board]
    [triboard.logic.move :as move]
    [triboard.logic.scores :as scores]
    ))


;; -----------------------------------------
;; Private
;; -----------------------------------------

(defn- with-available-moves
  "Add the available moves on the board"
  [{:keys [board] :as turn}]
  (assoc turn :moves (move/all-available-moves board)))

(defn- next-player
  [player]
  (case player
    :blue :red
    :red :green
    :green :blue))

(defn- next-3-players
  [player]
  (take 3 (iterate next-player (next-player player))))

(defn- with-next-player
  "Find the next player to act - dismiss those that cannot play any move"
  [{:keys [moves player] :as turn}]
  (let [who-can-play (filter #(get moves %) (next-3-players player))]
    (assoc turn :player (first who-can-play))
    ))

(defn- apply-moves
  [turn moves]
  (let [new-board (reduce move/apply-conversion (:board turn) moves)
        new-scores (reduce scores/update-scores (:scores turn) moves)]
    (-> turn
      (assoc :board new-board)
      (assoc :scores new-scores)
      )))


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

(s/def ::turn
  (s/keys :req-un
    [::board/board
     ::cst/player
     ::move/available-moves
     ::scores/scores]))

(defn new-init-turn []
  (-> {:board (board/new-board)
       :player (rand-nth cst/players)
       :moves {}
       :scores scores/initial-scores}
    with-available-moves
    with-next-player))

(defn get-player [turn] (:player turn))
(defn get-board [turn] (:board turn))
(defn get-scores [turn] (:scores turn))
(defn get-moves [turn] (:moves turn))

(defn get-moves-of
  "Access the available moves for the provided player, by coordinates"
  [turn player]
  (get (get-moves turn) player))

(s/fdef play-move
  :args (s/cat :turn ::turn :point ::board/coord)
  :ret ::turn)

(defn play-move
  "On player playing the move [x y] - update all the game state accordingly"
  [{:keys [player board] :as turn} point]
  (if-let [moves (get (get-moves-of turn player) point)]
    (-> turn
      (apply-moves (conj moves (move/empty-cell-conversion player point)))
      (with-available-moves)
      (with-next-player))
    turn))<commit_msg>remove useless code<commit_after>(ns triboard.logic.turn
  (:require
    [cljs.spec :as s :include-macros true]
    [triboard.logic.constants :as cst]
    [triboard.logic.board :as board]
    [triboard.logic.move :as move]
    [triboard.logic.scores :as scores]
    ))


;; -----------------------------------------
;; Private
;; -----------------------------------------

(defn- with-available-moves
  "Add the available moves on the board"
  [{:keys [board] :as turn}]
  (assoc turn :moves (move/all-available-moves board)))

(defn- next-player
  [player]
  (case player
    :blue :red
    :red :green
    :green :blue))

(defn- next-3-players
  [player]
  (take 3 (iterate next-player (next-player player))))

(defn- with-next-player
  "Find the next player to act - dismiss those that cannot play any move"
  [{:keys [moves player] :as turn}]
  (let [who-can-play (filter #(get moves %) (next-3-players player))]
    (assoc turn :player (first who-can-play))
    ))

(defn- apply-moves
  [turn moves]
  (let [new-board (reduce move/apply-conversion (:board turn) moves)
        new-scores (reduce scores/update-scores (:scores turn) moves)]
    (-> turn
      (assoc :board new-board)
      (assoc :scores new-scores)
      )))


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

(s/def ::turn
  (s/keys :req-un
    [::board/board
     ::cst/player
     ::move/available-moves
     ::scores/scores]))

(defn new-init-turn []
  (-> {:board (board/new-board)
       :player (rand-nth cst/players)
       :moves {}
       :scores scores/initial-scores}
    with-available-moves
    with-next-player))

(defn get-player [turn] (:player turn))
(defn get-board [turn] (:board turn))
(defn get-scores [turn] (:scores turn))

(defn get-moves-of
  "Access the available moves for the provided player, by coordinates"
  [turn player]
  (get (get-moves turn) player))

(s/fdef play-move
  :args (s/cat :turn ::turn :point ::board/coord)
  :ret ::turn)

(defn play-move
  "On player playing the move [x y] - update all the game state accordingly"
  [{:keys [player board] :as turn} point]
  (if-let [moves (get (get-moves-of turn player) point)]
    (-> turn
      (apply-moves (conj moves (move/empty-cell-conversion player point)))
      (with-available-moves)
      (with-next-player))
    turn))
{'fraction_non_alphanumeric': 0.21464860287891618, 'fraction_numerical': 0.0012701100762066045, 'mean_word_length': 2.343949044585987, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1536760', 'n_tokens_mistral': 1634, 'n_tokens_neox': 1572, 'n_words': 444}