Created
April 23, 2020 00:47
-
-
Save huytd/1187443e36fa440c49e8b44ee5961a3b 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
// Run this code at: https://algorithm-pad.now.sh/ | |
const matrix = (n) => [ | |
[ (n >> 8) & 1, (n >> 7) & 1, (n >> 6) & 1 ], | |
[ (n >> 5) & 1, (n >> 4) & 1, (n >> 3) & 1 ], | |
[ (n >> 2) & 1, (n >> 1) & 1, (n >> 0) & 1 ] | |
]; | |
let board = 0b000100010; | |
const pawn = 0b000100000; | |
const knight = 0b000000010; | |
debug({ board: matrix(board), pawn: matrix(pawn), knight: matrix(knight) }); | |
log("Knight move to C3"); | |
let new_board = 0b001100000; | |
let move = new_board ^ board; | |
debug({ move: matrix(move) }); | |
const isKnightMove = move => (knight ^ (move & knight)) === 0; | |
const isPawnMove = move => (pawn ^ (move & pawn)) === 0; | |
debug({ isKnightMove: isKnightMove(move), isPawnMove: isPawnMove(move) }); | |
let start = move & knight; | |
let end = move ^ knight; | |
debug({ start: matrix(start), end: matrix(end) }); | |
log("Pawn move to A1"); | |
new_board = 0b000000110; | |
move = new_board ^ board; | |
debug({ move: matrix(move) }); | |
debug({ isKnightMove: isKnightMove(move), isPawnMove: isPawnMove(move) }); | |
start = move & pawn; | |
end = move ^ pawn; | |
debug({ start: matrix(start), end: matrix(end) }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment