I am trying to write a small c# program that determines the correct move of a queen in chess. The coordinates of the initial and final position of the move are parsed by the string in the usual chess notation, for example, "a1". It seems to be easy, one just needs to add the isCorrectMove() method, but I don’t understand how we can convert the string to an integer without having a number and not even converting it to a number. That is, what the strings mean: var dx = Math.Abs (to [0] - from [0]); and so forth.
Here is an example code:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
public static void Main()
{
TestMove("a1", "d4");
TestMove("f4", "e7");
TestMove("a1", "a4");
TestMove("a1", "a1");
}
public static void TestMove(string from, string to)
{
Console.WriteLine("{0}-{1} {2}", from, to, IsCorrectMove(from, to));
}
public static bool IsCorrectMove(string from, string to)
{
var dx = Math.Abs(to[0] - from[0]); //horizontal move
var dy = Math.Abs(to[1] - from[1]); //vertical move
return (dx == dy || dx == 0 || dy == 0 && from != to); // this move, however does not yield appropriate result for a1-a1 Queen's move as it must be a false move or "no move".
}
}
the condition described by the line return (dx == dy || dx == 0 || dy == 0 && from != to); does not yield proper result, as boolean check says that it is a correct move. I do not understand how to express this condition using logical operators in C#. Please suggest your solution or how to find it. In chess, the acceptable moves by a queen are the same either as for bishop or as for the rook but how to express "no move"?