Skip to content

Instantly share code, notes, and snippets.

@kristianpedersen
Last active September 6, 2021 19:18
Show Gist options
  • Save kristianpedersen/b55449d87bb31d733e081727b6007a9d to your computer and use it in GitHub Desktop.
Save kristianpedersen/b55449d87bb31d733e081727b6007a9d to your computer and use it in GitHub Desktop.
JS works, Clojure logs empty list

Hi! Given a chess board position, I want to return all moves that have a distance of (x = 1 and y = 2) or (x = 2 and y = 1).

I have solved this in JavaScript, and I'm trying to implement it in Clojure, but my Clojure function just returns an empty list (). What am I missing?

Also, if you have any comments on readability or making the code more idiomatic, that's of course appreciated.

Here they are - first the JavaScript implementation, and then the Clojure one:

const originalBoard = [
	[
		{ position: { x: 0, y: 0 }, name: "A1", visited: false },
		{ position: { x: 1, y: 0 }, name: "B1", visited: false },
		{ position: { x: 2, y: 0 }, name: "C1", visited: false },
		{ position: { x: 3, y: 0 }, name: "D1", visited: false },
		{ position: { x: 4, y: 0 }, name: "E1", visited: false },
		{ position: { x: 5, y: 0 }, name: "F1", visited: false },
		{ position: { x: 6, y: 0 }, name: "G1", visited: false },
		{ position: { x: 7, y: 0 }, name: "H1", visited: false }],
	[
		{ position: { x: 0, y: 1 }, name: "A2", visited: false },
		{ position: { x: 1, y: 1 }, name: "B2", visited: false },
		{ position: { x: 2, y: 1 }, name: "C2", visited: false },
		{ position: { x: 3, y: 1 }, name: "D2", visited: false },
		{ position: { x: 4, y: 1 }, name: "E2", visited: false },
		{ position: { x: 5, y: 1 }, name: "F2", visited: false },
		{ position: { x: 6, y: 1 }, name: "G2", visited: false },
		{ position: { x: 7, y: 1 }, name: "H2", visited: false }],
	[
		{ position: { x: 0, y: 2 }, name: "A3", visited: false },
		{ position: { x: 1, y: 2 }, name: "B3", visited: false },
		{ position: { x: 2, y: 2 }, name: "C3", visited: false },
		{ position: { x: 3, y: 2 }, name: "D3", visited: false },
		{ position: { x: 4, y: 2 }, name: "E3", visited: false },
		{ position: { x: 5, y: 2 }, name: "F3", visited: false },
		{ position: { x: 6, y: 2 }, name: "G3", visited: false },
		{ position: { x: 7, y: 2 }, name: "H3", visited: false }],
	[
		{ position: { x: 0, y: 3 }, name: "A4", visited: false },
		{ position: { x: 1, y: 3 }, name: "B4", visited: false },
		{ position: { x: 2, y: 3 }, name: "C4", visited: false },
		{ position: { x: 3, y: 3 }, name: "D4", visited: false },
		{ position: { x: 4, y: 3 }, name: "E4", visited: false },
		{ position: { x: 5, y: 3 }, name: "F4", visited: false },
		{ position: { x: 6, y: 3 }, name: "G4", visited: false },
		{ position: { x: 7, y: 3 }, name: "H4", visited: false }],
	[
		{ position: { x: 0, y: 4 }, name: "A5", visited: false },
		{ position: { x: 1, y: 4 }, name: "B5", visited: false },
		{ position: { x: 2, y: 4 }, name: "C5", visited: false },
		{ position: { x: 3, y: 4 }, name: "D5", visited: false },
		{ position: { x: 4, y: 4 }, name: "E5", visited: false },
		{ position: { x: 5, y: 4 }, name: "F5", visited: false },
		{ position: { x: 6, y: 4 }, name: "G5", visited: false },
		{ position: { x: 7, y: 4 }, name: "H5", visited: false }],
	[
		{ position: { x: 0, y: 5 }, name: "A6", visited: false },
		{ position: { x: 1, y: 5 }, name: "B6", visited: false },
		{ position: { x: 2, y: 5 }, name: "C6", visited: false },
		{ position: { x: 3, y: 5 }, name: "D6", visited: false },
		{ position: { x: 4, y: 5 }, name: "E6", visited: false },
		{ position: { x: 5, y: 5 }, name: "F6", visited: false },
		{ position: { x: 6, y: 5 }, name: "G6", visited: false },
		{ position: { x: 7, y: 5 }, name: "H6", visited: false }],
	[
		{ position: { x: 0, y: 6 }, name: "A7", visited: false },
		{ position: { x: 1, y: 6 }, name: "B7", visited: false },
		{ position: { x: 2, y: 6 }, name: "C7", visited: false },
		{ position: { x: 3, y: 6 }, name: "D7", visited: false },
		{ position: { x: 4, y: 6 }, name: "E7", visited: false },
		{ position: { x: 5, y: 6 }, name: "F7", visited: false },
		{ position: { x: 6, y: 6 }, name: "G7", visited: false },
		{ position: { x: 7, y: 6 }, name: "H7", visited: false }],
	[
		{ position: { x: 0, y: 7 }, name: "A8", visited: false },
		{ position: { x: 1, y: 7 }, name: "B8", visited: false },
		{ position: { x: 2, y: 7 }, name: "C8", visited: false },
		{ position: { x: 3, y: 7 }, name: "D8", visited: false },
		{ position: { x: 4, y: 7 }, name: "E8", visited: false },
		{ position: { x: 5, y: 7 }, name: "F8", visited: false },
		{ position: { x: 6, y: 7 }, name: "G8", visited: false },
		{ position: { x: 7, y: 7 }, name: "H8", visited: false }]];


const board = originalBoard.flat();

function getValidMoves(move, board) {
	return board.filter(cell => {
		const xDistance = Math.abs(move.position.x - cell.position.x);
		const yDistance = Math.abs(move.position.y - cell.position.y);
		const isValid = (
			xDistance === 2 && yDistance === 1
			||
			xDistance === 1 && yDistance === 2
		);

		return isValid;
	});
}

console.log(getValidMoves(board[20], board));

/* console.log output:
[ { position: { x: 3, y: 0 }, name: 'D1', visited: false },
  { position: { x: 5, y: 0 }, name: 'F1', visited: false },
  { position: { x: 2, y: 1 }, name: 'C2', visited: false },
  { position: { x: 6, y: 1 }, name: 'G2', visited: false },
  { position: { x: 2, y: 3 }, name: 'C4', visited: false },
  { position: { x: 6, y: 3 }, name: 'G4', visited: false },
  { position: { x: 3, y: 4 }, name: 'D5', visited: false },
  { position: { x: 5, y: 4 }, name: 'F5', visited: false } ]
 */
(def original-board [[{:position {:x 0 :y 0}, :name "A1", :visited false}
                      {:position {:x 1 :y 0}, :name "B1", :visited false}
                      {:position {:x 2 :y 0}, :name "C1", :visited false}
                      {:position {:x 3 :y 0}, :name "D1", :visited false}
                      {:position {:x 4 :y 0}, :name "E1", :visited false}
                      {:position {:x 5 :y 0}, :name "F1", :visited false}
                      {:position {:x 6 :y 0}, :name "G1", :visited false}
                      {:position {:x 7 :y 0}, :name "H1", :visited false}]
                     [{:position {:x 0 :y 1}, :name "A2", :visited false}
                      {:position {:x 1 :y 1}, :name "B2", :visited false}
                      {:position {:x 2 :y 1}, :name "C2", :visited false}
                      {:position {:x 3 :y 1}, :name "D2", :visited false}
                      {:position {:x 4 :y 1}, :name "E2", :visited false}
                      {:position {:x 5 :y 1}, :name "F2", :visited false}
                      {:position {:x 6 :y 1}, :name "G2", :visited false}
                      {:position {:x 7 :y 1}, :name "H2", :visited false}]
                     [{:position {:x 0 :y 2}, :name "A3", :visited false}
                      {:position {:x 1 :y 2}, :name "B3", :visited false}
                      {:position {:x 2 :y 2}, :name "C3", :visited false}
                      {:position {:x 3 :y 2}, :name "D3", :visited false}
                      {:position {:x 4 :y 2}, :name "E3", :visited false}
                      {:position {:x 5 :y 2}, :name "F3", :visited false}
                      {:position {:x 6 :y 2}, :name "G3", :visited false}
                      {:position {:x 7 :y 2}, :name "H3", :visited false}]
                     [{:position {:x 0 :y 3}, :name "A4", :visited false}
                      {:position {:x 1 :y 3}, :name "B4", :visited false}
                      {:position {:x 2 :y 3}, :name "C4", :visited false}
                      {:position {:x 3 :y 3}, :name "D4", :visited false}
                      {:position {:x 4 :y 3}, :name "E4", :visited false}
                      {:position {:x 5 :y 3}, :name "F4", :visited false}
                      {:position {:x 6 :y 3}, :name "G4", :visited false}
                      {:position {:x 7 :y 3}, :name "H4", :visited false}]
                     [{:position {:x 0 :y 4}, :name "A5", :visited false}
                      {:position {:x 1 :y 4}, :name "B5", :visited false}
                      {:position {:x 2 :y 4}, :name "C5", :visited false}
                      {:position {:x 3 :y 4}, :name "D5", :visited false}
                      {:position {:x 4 :y 4}, :name "E5", :visited false}
                      {:position {:x 5 :y 4}, :name "F5", :visited false}
                      {:position {:x 6 :y 4}, :name "G5", :visited false}
                      {:position {:x 7 :y 4}, :name "H5", :visited false}]
                     [{:position {:x 0 :y 5}, :name "A6", :visited false}
                      {:position {:x 1 :y 5}, :name "B6", :visited false}
                      {:position {:x 2 :y 5}, :name "C6", :visited false}
                      {:position {:x 3 :y 5}, :name "D6", :visited false}
                      {:position {:x 4 :y 5}, :name "E6", :visited false}
                      {:position {:x 5 :y 5}, :name "F6", :visited false}
                      {:position {:x 6 :y 5}, :name "G6", :visited false}
                      {:position {:x 7 :y 5}, :name "H6", :visited false}]
                     [{:position {:x 0 :y 6}, :name "A7", :visited false}
                      {:position {:x 1 :y 6}, :name "B7", :visited false}
                      {:position {:x 2 :y 6}, :name "C7", :visited false}
                      {:position {:x 3 :y 6}, :name "D7", :visited false}
                      {:position {:x 4 :y 6}, :name "E7", :visited false}
                      {:position {:x 5 :y 6}, :name "F7", :visited false}
                      {:position {:x 6 :y 6}, :name "G7", :visited false}
                      {:position {:x 7 :y 6}, :name "H7", :visited false}]
                     [{:position {:x 0 :y 7}, :name "A8", :visited false}
                      {:position {:x 1 :y 7}, :name "B8", :visited false}
                      {:position {:x 2 :y 7}, :name "C8", :visited false}
                      {:position {:x 3 :y 7}, :name "D8", :visited false}
                      {:position {:x 4 :y 7}, :name "E8", :visited false}
                      {:position {:x 5 :y 7}, :name "F8", :visited false}
                      {:position {:x 6 :y 7}, :name "G8", :visited false}
                      {:position {:x 7 :y 7}, :name "H8", :visited false}]])

(def board (flatten original-board))

(defn get-valid-moves [move board]
  (filter (fn [cell]
            (let [x-distance (Math/abs (- (:x (move :position)) (:x (cell :position))))
                  y-distance (Math/abs (- (:y (move :position)) (:y (cell :position))))
                  is-valid (or
                            (and (= x-distance 2) (= y-distance 1))
                            (and (= x-distance 1) (= y-distance 2)))]
              is-valid))
          board))

(println (get-valid-moves (nth board 20) board) ); ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment