Learning Javascript — chessKnight

Ismail Akkila
Jul 26, 2017 · 2 min read

Another chess related challenge on codefights.com. Here it is:


Given a position of a knight on the standard chessboard, find the number of different moves the knight can perform.

The knight can move to a square that is two squares horizontally and one square vertically, or two squares vertically and one square horizontally away from it. The complete move therefore looks like the letter L. Check out the image below to see all valid moves for a knight piece that is placed on one of the central squares.

Example

  • For cell = "c2", the output should be
    chessKnight(cell) = 6.

My solution:

function chessKnight(cell) {
var possibleCoordinates = [];
var xCoordinates = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'];
var cellX = xCoordinates.indexOf(cell[0]) + 1; //The X Position
var cellY = parseInt(cell[1]); //The Y Position

//Find all possible X Positions
var cellXpositions = [cellX + 2, cellX - 2, cellX + 1, cellX - 1].filter(function(cellPosition) {
return (cellPosition > 0 && cellPosition < 9);
})

//Find all possible Y Positions
var cellYpositions = [cellY + 2, cellY - 2, cellY + 1, cellY - 1].filter(function(cellPosition) {
return (cellPosition > 0 && cellPosition < 9);
})

//We now have 2 seperate arrays: One for X Positions, One for Y Positions.
//Go thru every combination possible and if it is a valid position then push it
//to the possible coordinates array - if not present already.
//The trick is to validate the position pair (X, Y) by making sure that
//the net X distance plus net Y distance is 3

for (var i = 0; i < cellXpositions.length; i++) {
for (var j = 0; j < cellYpositions.length; j++) {
if (Math.abs(cellX - cellXpositions[i]) + Math.abs(cellY - cellYpositions[j]) === 3) {
console.log('This is a valid coordinate: ', [cellXpositions[i], cellYpositions[j]]);
if (!possibleCoordinates.includes([cellXpositions[i], cellYpositions[j]])) {
possibleCoordinates.push([cellXpositions[i], cellYpositions[j]]);
}
}
}
}
console.log('Possible Coordinates:', possibleCoordinates);
console.log ('Possible Moves:', possibleCoordinates.length);
return possibleCoordinates.length;
}

Here is an example output for the function above:

Input: cell: "g6"
Output: 6
Expected Output: 6
Console Output:
This is a valid coordinate: [ 5, 7 ]
This is a valid coordinate: [ 5, 5 ]
This is a valid coordinate: [ 8, 8 ]
This is a valid coordinate: [ 8, 4 ]
This is a valid coordinate: [ 6, 8 ]
This is a valid coordinate: [ 6, 4 ]
Possible Coordinates: [ [ 5, 7 ], [ 5, 5 ], [ 8, 8 ], [ 8, 4 ], [ 6, 8 ], [ 6, 4 ] ]
Possible Moves: 6

Feels good to be getting better at this :)

Ismail Akkila

Written by

I live and breathe technology. Curious about programming, cryptocurrencies and cybersecurity.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade