timothypratley/tictactoe

remove duplicated direction checks

Opened this issue · 0 comments

I learned a lot from your blog and just give a little suggestion.

(defn no-need-check-direction [board x y dx dy n]
  (not (get-in board [(+ (* dx (- n 1)) x) (+ (* dy (- n 1)) y)])))

(defn win? [owner board n]
  (some true?
        (for [x (range board-size)
              y (range board-size)
              [dx dy] [[1 0] [0 1] [1 1] [1 -1]]]
          ;; remove duplicated checks for some location points.
          ;; for example: location point [2,0] only need check direction ->(0,1)
          (if (no-need-check-direction board x y dx dy n)
            false
            (straight owner board [x y] [dx dy] n)))))


(deftest remove-duplicated-checks-test
  (is (true?
       (no-need-check-direction [["C" "C"] ["C" "C"]] 1 0 1 1 2))))