Skip to content

Instantly share code, notes, and snippets.

@ZachMassia
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZachMassia/0cc4675b1ce7bbe6746c to your computer and use it in GitHub Desktop.
Save ZachMassia/0cc4675b1ce7bbe6746c to your computer and use it in GitHub Desktop.
(defn get-adjacent-coords
"Returns a vector of [i j] coords representing orthogonally adjacent points."
[board [i j :as pos]]
(let [size (:size board)
coords (transient [])]
(when (pos? i)
(conj! coords [(dec i) j]))
(when (pos? j)
(conj! coords [i (dec j)]))
(when (< i (dec size))
(conj! coords [(inc i) j]))
(when (< j (dec size))
(conj! coords [i (inc j)]))
(persistent! coords)))
(defn get-adjacent-coords2
"Returns a vector of [i j] coords representing orthogonally adjacent points."
[board [i j :as pos]]
(let [size (:size board)]
(for [x [-1 1] y [-1 1]
:let [new-x (+ i x)
new-y (+ j y)]
:when (every? #(< % size) [new-x new-y])]
[new-x new-y])))
/*
* Given a board position, returns a list of [i,j] coordinates representing
* orthogonally adjacent intersections
*/
Board.prototype.get_adjacent_intersections = function(i , j) {
var neighbors = [];
if (i > 0)
neighbors.push([i - 1, j]);
if (j < this.size - 1)
neighbors.push([i, j + 1]);
if (i < this.size - 1)
neighbors.push([i + 1, j]);
if (j > 0)
neighbors.push([i, j - 1]);
return neighbors;
};
*board has a size of 19*
------------------------
om-go.board> (get-adjacent-coords board [5 5])
[[4 5] [5 4] [6 5] [5 6]]
om-go.board> (get-adjacent-coords2 board [5 5])
([4 4] [4 6] [6 4] [6 6])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment