-
-
Save ZachMassia/0cc4675b1ce7bbe6746c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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]))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*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