The bulk of the game exists in the board manager class. It is responsible for tracking who's turn it is, location of the pieces, and if the king is in check. The latter data is stored as part of a 3D array. The first two dimensions are x and y positions of the board. The 3rd dimension has 2 layers. Piece type and piece color. The bottom left of the grid rests at (0,0,0). When the player clicks on the screen it sends a raycast towards the grid and returns the cooridnates of the collision. These values are divided by the size of each grid space then rounded down. The board manager then checks to see what piece is in that position and proceeds accordingly.
To define movement behaviour the CreateMarker method is called for each space that piece can move to. The method has 3 arguments. Relative X position, Relative Y position, and Type. The type has 3 states. 2 is the most common. This is used for any space a piece can move to, and take at. The others are primarly used for pawns. 0 is used for any space a piece can move to, but cannot take at. 1 is used for a space a piece can ONLY take at, but cannot normally move to.
Example Piece Definitions
public class King : Piece {
public override void Movement() {
base.CreateMarker(0, 1, 2);
base.CreateMarker(0, -1, 2);
base.CreateMarker(1, 0, 2);
base.CreateMarker(-1, 0, 2);
base.CreateMarker(1, 1, 2);
base.CreateMarker(1, -1, 2);
base.CreateMarker(-1, 1, 2);
base.CreateMarker(-1, -1, 2);
}
}
public class Pawn : Piece {
public override void Movement() {
if(color == 0) {
base.CreateMarker(0, 1, 0);
base.CreateMarker(-1, 1, 1);
base.CreateMarker(1, 1, 1);
}
//Snip//
}
}
public class Rook : Piece {
public override void Movement() {
int i = 1;
int stopped = 0;
while (stopped==0) { //up
base.CreateMarker(0, i, 2);
//If next is out of bounds, or colliding with another piece
if (y + i >= 9) or (GameObject.FindWithTag("Grid").GetComponent().boardState[x, y + i, 0] != 0)
stopped = 1;
i++;
}
//Snip//
}
}