Skip to content

Computer Move: Random Available Cellλ︎

(defn computer-move
  "Takes a turn for the computer, adding an X shape to the board"
  (let [available-cells
        (for [row    (range board-dimension)
              column (range board-dimension)
              :when (=
                     (get-in (@app-state :board) [column row]))]
          [column row])

        next-move (when (seq available-cells)
                    (rand-nth available-cells))]

    (if next-move
        (prn "Computer move at:" next-move)
        (swap! app-state assoc-in [:board (first next-move) (second next-move)] :cross))
      (prn "Computer move: no more moves available"))))


If there are no more moves left, then a message could be displayed on the web page, so the user knows (although as the cells are all full, then its kind of implied)