Created
September 21, 2018 20:53
-
-
Save nikonov91-dev/d95a962a71bd214f0f9a163c9255db41 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
https://www.codewars.com/kata/chess-fun-number-12-possible-rook-moves/train/javascript | |
function possibleRookMoves(b, square, flag) { | |
const letter = square[0].toLowerCase(), | |
letterNum = letter.charCodeAt(0) - 97, | |
num = b.length - square[1], | |
arrayNum = num + 1, | |
enemy = flag === 1 ? -1 : 1, | |
ally = flag === 1 ? 1 : -1; | |
const row = definePosition(b[num],'hor', letterNum) | |
const col = definePosition(b, 'vert', num, letterNum) | |
return [].concat( | |
filterMoves(col, 'vert'), | |
filterMoves(row, 'hor')) | |
.sort() | |
function definePosition(arr, mode, num1, num2) { | |
return arr.map((e, i) => { | |
if(mode === 'vert') e = e[num2]; | |
if (i === num1) return 'rook'; | |
if (e === 0) return true; | |
if (e === enemy) return 'enemy'; | |
return 'ally' | |
}) | |
} | |
function filterMoves(arr, mode) { | |
let rookOccured = false; | |
let isEnd = false; | |
let move; | |
const moves = []; | |
arr.forEach((e, i) => { | |
if (isEnd) return; | |
//here is const move = letter + (col.length - i); | |
if (mode === 'hor') move = String.fromCharCode(i + 97) + square[1]; | |
if (mode === 'vert') move = letter + (col.length - i); | |
if (e === true && !rookOccured) | |
return moves.push(move); | |
if (e === 'enemy' && !rookOccured) | |
return moves.splice(0, moves.length, move); | |
if (e === 'ally' && !rookOccured) | |
return moves.splice(0, moves.length); | |
if (e === 'rook') | |
return rookOccured = true; | |
if (e === 'ally' && rookOccured) return isEnd = true; | |
if (e === true && rookOccured) return moves.push(move); | |
if (e === 'enemy' && rookOccured) { | |
isEnd = true; | |
return moves.push(move) | |
} | |
}) | |
return moves | |
} | |
} | |
console.log(possibleRookMoves([ | |
[0, 1, 0, -1, 0], | |
[0, 0, -1, 0, 0], | |
[0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0], | |
[-1, 0, 1, 1, 0]], "c5", 1), ["c4", "d5"]) | |
/* console.log(possibleRookMoves([ | |
[1, -1, 1, 0], | |
[1, 0, 1, 1], | |
[1, -1, 0, 0], | |
[0, 0, -1, 0]], "b1", 1), ["a1", "c1"]) | |
console.log(possibleRookMoves([ | |
[0, 0, 0], | |
[0, 0, 0], | |
[0, 1, 0]], "b2", 1), ["a2", "b3", "c2"]) | |
console.log(possibleRookMoves([ | |
[0, 1, 0, -1, 0], | |
[0, 0, -1, 0, 0], | |
[0, 0, 0, 0, 0], | |
[0, 0, 0, 0, 0], | |
[-1, 0, 1, 1, 0]], "c5", 1), ["c4", "d5"]) | |
console.log(possibleRookMoves([ | |
[0, 1, 0], | |
[0, -1, -1], | |
[0, 1, 0]], "a2", 1), ["a1", "a3", "b2"]) | |
console.log(possibleRookMoves([ | |
[0, 0, 1, 0], | |
[1, 1, 0, 1], | |
[0, 0, 1, 0], | |
[0, 0, 1, 0]], "c3", -1), ["b3", "c2", "c4", "d3"]) | |
console.log(possibleRookMoves([ | |
[0, 1, 0], | |
[1, 0, 1], | |
[0, 1, 0]], "b2", -1), ["a2", "b1", "b3", "c2"]) | |
console.log(possibleRookMoves([ | |
[0, 1, 0], | |
[1, 0, 1], | |
[0, 1, 0]], "b2", 1), []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment