Linq Challenge - Bishop Move
Bishop Move
we need to calculate the allowed move for Bishop by the given position
we start with a Bishop on c6, what positions can it reach in one move? => output should include b5, a4, b7, a8
// this function draw the chess positions
static IEnumerable<string> GetBoardPositions()
{
//draw rows
return Enumerable.Range('a', 8).
//draw columns
SelectMany(
x => Enumerable.Range('1', 8), (f, r) =>
// string format for showing position
String.Format("{0}{1}",(char)f, (char)r));
}
the 64 positions list
// calculate the allowed move by given (startPosition=> that can move)
// and targetPostion => that current position of Bishop
static bool BishopCanMoveTo(string startPos, string targetPos)
{
var dx = Math.Abs(startPos[0] - targetPos[0]);
var dy = Math.Abs(startPos[1] - targetPos[1]);
return dx == dy && dx != 0;
}
// the result
GetBoardPositions().Where(p => BishopCanMoveTo(p,"c6"))
the allowed position that Bishop can move