What would be the fastest way of storing or calculating legal move sets for chess pieces?
- by ioSamurai
For example if a move is attempted I could just loop through a list of legal moves and compare the x,y but I have to write logic to calculate those at least every time the piece is moved.
Or, I can store in an array [,] and then check if x and y are not 0 then it is a legal move and then I save the data like this
[0][1][0][0] etc etc for each row where 1 is a legal move, but I still have to populate it.
I wonder what the fastest way to store and read a legal move on a piece object for calculation.
I could use matrix math as well I suppose but I don't know.
Basically I want to persist the rules for a given piece so I can assign a piece object a little template of all it's possible moves considering it is starting from it's current location, which should be just one table of data. But is it faster to loop or write LINQ queries or store arrays and matrices?
public class Move
{
public int x;
public int y;
}
public class ChessPiece : Move
{
private List<Move> possibleMoves { get; set; }
public bool LegalMove(int x, int y){
foreach(var p in possibleMoves)
{
if(p.x == x && p.y == y)
{ return true; }
}
}
}
Anyone know?