Created
October 10, 2016 13:26
-
-
Save mitchazj/db57956f9e41c1bd5d22b0d47d42626c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace Johnson_Chess_Champion | |
{ | |
public class Legality | |
{ | |
private Square[] testPosition = new Square[64]; | |
/// <summary> | |
/// Returns the squares allowed to move to for a piece | |
/// </summary> | |
/// <param name="Position">The Square array specifying the position</param> | |
/// <param name="piece"></param> | |
/// <returns></returns> | |
private int[] getSquaresLegalForSquare(Square[] Position, int Square, bool isAKingCheck) | |
{ | |
if (Square > Position.Length) | |
{ | |
return new int[0]; | |
} | |
if (Position[Square].Piece.Type == Types.None) | |
{ | |
return new int[0]; | |
} | |
if (Position[Square].Piece.Type == Types.Pawn) | |
{ | |
if (Position[Square].Piece.Color == PieceColors.White) | |
{ | |
return getSquaresLegalForWhitePawn(Position, Square, isAKingCheck); | |
} | |
if (Position[Square].Piece.Color == PieceColors.Black) | |
{ | |
return getSquaresLegalForBlackPawn(Position, Square, isAKingCheck); | |
} | |
else | |
{ | |
return new int[0]; | |
} | |
} | |
if (Position[Square].Piece.Type == Types.Knight) | |
{ | |
return getSquaresLegalForKnight(Position, Square, isAKingCheck); | |
} | |
if (Position[Square].Piece.Type == Types.Bishop) | |
{ | |
return getSquaresLegalForBishop(Position, Square, isAKingCheck); | |
} | |
if (Position[Square].Piece.Type == Types.Rook) | |
{ | |
return getSquaresLegalForRook(Position, Square, isAKingCheck); | |
} | |
if (Position[Square].Piece.Type == Types.Queen) | |
{ | |
return getSquaresLegalForQueen(Position, Square, isAKingCheck); | |
} | |
if (Position[Square].Piece.Type == Types.King) | |
{ | |
return getSquaresLegalForKing(Position, Square); | |
} | |
else | |
{ | |
return new int[0]; | |
} | |
} | |
/// <summary> | |
/// Returns the squares legal in a position | |
/// </summary> | |
/// <param name="Position"></param> | |
/// <param name="Square"></param> | |
/// <returns></returns> | |
public int[] getSquaresLegalForSquareInPosition(Square[] Position, int Square, PieceColors whoseTurn) | |
{ | |
if (Position[Square].Piece.Color == whoseTurn) | |
{ | |
//Is it a king or not? | |
if (Position[Square].Piece.Type != Types.King) | |
{ | |
#region When Check for Black King (For comments see the white one) | |
if (isPositionCheckForBlackKing(Position) == true) | |
{ | |
int[] legal = getSquaresLegalForSquare(Position, Square, false); | |
int[] otherLegal = new int[64]; | |
for (int j = 0; j < 64; ++j) | |
{ | |
otherLegal[j] = 100; | |
} | |
bool isCheckRemoving = false; | |
for (int j = 0; j < legal.Length; ++j) | |
{ | |
setMemoryToPosition(Position); | |
if (legal[j] < 64) | |
{ | |
testPosition[legal[j]].Piece.Type = Position[Square].Piece.Type; | |
testPosition[legal[j]].Piece.Color = Position[Square].Piece.Color; | |
} | |
if (isPositionCheckForBlackKing(testPosition) == true) | |
{ | |
//Skip | |
} | |
else | |
{ | |
isCheckRemoving = true; | |
otherLegal[j] = legal[j]; | |
} | |
} | |
//Is it still not check removing? | |
if (isCheckRemoving == false) | |
{ | |
return new int[0]; | |
} | |
return otherLegal; | |
} | |
#endregion | |
#region When Check for White King | |
if (isPositionCheckForWhiteKing(Position) == true) | |
{ | |
//The theoretically legal moves | |
int[] legal = getSquaresLegalForSquare(Position, Square, false); | |
//The legal moves to be returned | |
int[] otherLegal = new int[64]; | |
//Set these legal moves to be returned | |
for (int j = 0; j < 64; ++j) | |
{ | |
otherLegal[j] = 100; | |
} | |
//Declare the boolean | |
bool isCheckRemoving = false; | |
//Check the theory moves and add and check stopping ones to the legal list | |
for (int j = 0; j < legal.Length; ++j) | |
{ | |
setMemoryToPosition(Position); | |
if (legal[j] < 64) | |
{ | |
testPosition[legal[j]].Piece.Type = Position[Square].Piece.Type; | |
testPosition[legal[j]].Piece.Color = Position[Square].Piece.Color; | |
} | |
if (isPositionCheckForWhiteKing(testPosition) == true) | |
{ | |
//Skip | |
} | |
else | |
{ | |
isCheckRemoving = true; | |
otherLegal[j] = legal[j]; | |
} | |
} | |
//Is it still not check removing? | |
if (isCheckRemoving == false) | |
{ | |
//Return a emtpy | |
return new int[0]; | |
} | |
//Return the legal moves found | |
return otherLegal; | |
} | |
#endregion | |
else | |
{ | |
//No Check! (But it may be pinned where moving will result in it...) | |
//Set memory for analysis | |
setMemoryToPosition(Position); | |
//Remove the piece | |
testPosition[Square].Piece.Type = Types.None; | |
testPosition[Square].Piece.Color = PieceColors.Empty; | |
//Check if removing the piece results in check. | |
bool foundCheckGuilty = false; | |
if (isPositionCheckForBlackKing(testPosition) == true) | |
{ | |
//Was it a black piece removed? | |
if (Position[Square].Piece.Color == PieceColors.Black) | |
{ | |
//Moving will result in check! | |
foundCheckGuilty = true; | |
} | |
} | |
if (isPositionCheckForWhiteKing(testPosition) == true) | |
{ | |
//Was it a white piece removed? | |
if (Position[Square].Piece.Color == PieceColors.White) | |
{ | |
//Moving will result in check! | |
foundCheckGuilty = true; | |
} | |
} | |
if (foundCheckGuilty == true) | |
{ | |
//Pinned! Yike! | |
//Are there any moves which rid the pin, such as capturing the pinning piece, etc? | |
//The hopefully legal moves | |
int[] legal = getSquaresLegalForSquare(Position, Square, false); | |
//The legal moves to be returned | |
int[] otherLegal = new int[64]; | |
//Set these legal moves to be returned | |
for (int j = 0; j < 64; ++j) | |
{ | |
otherLegal[j] = 100; | |
} | |
//Declare the boolean | |
bool isPinRemoving = false; | |
//Check the theory moves and add the pin-stopping ones to the legal list | |
for (int j = 0; j < legal.Length; ++j) | |
{ | |
setMemoryToPosition(Position); | |
if (legal[j] < 64) | |
{ | |
testPosition[legal[j]].Piece.Type = Position[Square].Piece.Type; | |
testPosition[legal[j]].Piece.Color = Position[Square].Piece.Color; | |
} | |
if (isPositionCheckForWhiteKing(testPosition) == true) | |
{ | |
if (Position[Square].Piece.Color == PieceColors.White) | |
{ | |
//Skip | |
} | |
else | |
{ | |
isPinRemoving = true; | |
otherLegal[j] = legal[j]; | |
} | |
} | |
if (isPositionCheckForBlackKing(testPosition) == true) | |
{ | |
if (Position[Square].Piece.Color == PieceColors.Black) | |
{ | |
//Skip | |
} | |
else | |
{ | |
isPinRemoving = true; | |
otherLegal[j] = legal[j]; | |
} | |
} | |
else | |
{ | |
isPinRemoving = true; | |
otherLegal[j] = legal[j]; | |
} | |
} | |
//Is it still not pin removing? | |
if (isPinRemoving == false) | |
{ | |
//Return a emtpy | |
return new int[0]; | |
} | |
//Return the legal moves found | |
return otherLegal; | |
} | |
else | |
{ | |
//Nope. Legal. | |
return getSquaresLegalForSquare(Position, Square, false); | |
} | |
} | |
} | |
else | |
{ | |
//It's a king! | |
#region White King? | |
if (Position[Square].Piece.Color == PieceColors.White) | |
{ | |
if (isPositionCheckForBlackKing(Position) == true) | |
{ | |
//In Check! Halt! | |
return new int[0]; | |
} | |
//This removes any possivble leftovers that sneaked in... | |
int[] legalMoves = getSquaresLegalForSquare(Position, Square, false); | |
for (int m = 0; m < legalMoves.Length; ++m) | |
{ | |
if (legalMoves[m] != 100) | |
{ | |
setMemoryToPosition(Position); | |
testPosition[legalMoves[m]].Piece.Type = testPosition[Square].Piece.Type; | |
testPosition[legalMoves[m]].Piece.Color = testPosition[Square].Piece.Color; | |
testPosition[Square].Piece.Type = Types.None; | |
testPosition[Square].Piece.Color = PieceColors.Empty; | |
if (isPositionCheckForWhiteKing(testPosition) == true) | |
{ | |
legalMoves[m] = 100; | |
} | |
} | |
} | |
return legalMoves; | |
} | |
#endregion | |
#region Black king? | |
if (Position[Square].Piece.Color == PieceColors.Black) | |
{ | |
if (isPositionCheckForWhiteKing(Position) == true) | |
{ | |
//In Check! Halt! | |
return new int[0]; | |
} | |
//This removes any possible leftovers that sneaked in... | |
int[] legalMoves = getSquaresLegalForSquare(Position, Square, false); | |
for (int m = 0; m < legalMoves.Length; ++m) | |
{ | |
if (legalMoves[m] != 100) | |
{ | |
setMemoryToPosition(Position); | |
testPosition[legalMoves[m]].Piece.Type = testPosition[Square].Piece.Type; | |
testPosition[legalMoves[m]].Piece.Color = testPosition[Square].Piece.Color; | |
testPosition[Square].Piece.Type = Types.None; | |
testPosition[Square].Piece.Color = PieceColors.Empty; | |
if (isPositionCheckForBlackKing(testPosition) == true) | |
{ | |
legalMoves[m] = 100; | |
} | |
} | |
} | |
return legalMoves; | |
} | |
#endregion | |
else | |
{ | |
return new int[0]; | |
} | |
} | |
} | |
else | |
{ | |
return new int[0]; | |
} | |
} | |
/// <summary> | |
/// Returns true if and only if the white king is in check | |
/// </summary> | |
/// <param name="Position"></param> | |
/// <returns></returns> | |
public bool isPositionCheckForBlackKing(Square[] Position) | |
{ | |
bool isInCheck = false; | |
int kingBlackSquare = 100; | |
//Determine the location of the kings | |
for (int k = 0; k < Position.Length; ++k) | |
{ | |
if (Position[k].Piece.Type == Types.King) | |
{ | |
if (Position[k].Piece.Color == PieceColors.Black) | |
{ | |
kingBlackSquare = k; | |
} | |
} | |
} | |
for (int j = 0; j < Position.Length; ++j) | |
{ | |
if (getSquaresLegalForSquare(Position, j, false).Contains(kingBlackSquare) == true) | |
{ | |
isInCheck = true; | |
} | |
} | |
return isInCheck; | |
} | |
/// <summary> | |
/// Returns true if and only if the black king is in check | |
/// </summary> | |
/// <param name="Position"></param> | |
/// <returns></returns> | |
public bool isPositionCheckForWhiteKing(Square[] Position) | |
{ | |
bool isInCheck = false; | |
int kingWhiteSquare = 100; | |
//Determine the location of the kings | |
for (int k = 0; k < Position.Length; ++k) | |
{ | |
if (Position[k].Piece.Type == Types.King) | |
{ | |
if (Position[k].Piece.Color == PieceColors.White) | |
{ | |
kingWhiteSquare = k; | |
} | |
} | |
} | |
for (int j = 0; j < Position.Length; ++j) | |
{ | |
if (getSquaresLegalForSquare(Position, j, false).Contains(kingWhiteSquare) == true) | |
{ | |
isInCheck = true; | |
} | |
} | |
return isInCheck; | |
} | |
/// <summary> | |
/// Returns 0 if usual, 1 if stalemate, and 2 if checkmate | |
/// </summary> | |
/// <param name="Position"></param> | |
/// <returns></returns> | |
public int isPositionSomething(Square[] Position) | |
{ | |
int[] Legal; | |
bool legalMoveFound = false; | |
for (int j = 0; j < 64; ++j) | |
{ | |
Legal = getSquaresLegalForSquareInPosition(Position, j, Position[j].Piece.Color); | |
if (Legal != new int[0]) | |
{ | |
for (int k = 0; k < Legal.Length; ++k) | |
{ | |
if (k != 100) | |
{ | |
legalMoveFound = true; | |
break; | |
} | |
} | |
} | |
} | |
if (legalMoveFound == false) | |
{ | |
if (isPositionCheckForBlackKing(Position) == true) | |
{ | |
//Checkmate | |
return 2; | |
} | |
if (isPositionCheckForWhiteKing(Position) == true) | |
{ | |
//Checkmate | |
return 2; | |
} | |
else | |
{ | |
//Stalemate | |
return 1; | |
} | |
} | |
else | |
{ | |
return 0; | |
} | |
} | |
/// <summary> | |
/// Sets the memory position to the position specified | |
/// </summary> | |
/// <param name="Position">The position to set the memory to</param> | |
private void setMemoryToPosition(Square[] Position) | |
{ | |
for (int j = 0; j < 64; ++j) | |
{ | |
testPosition[j].Piece.Type = Position[j].Piece.Type; | |
testPosition[j].Piece.Color = Position[j].Piece.Color; | |
} | |
} | |
#region King | |
private int[] getSquaresLegalForKing(Square[] Position, int Square) | |
{ | |
int[] Legal = new int[8]; | |
//This sets the default | |
for (int k = 0; k < 8; ++k) | |
{ | |
Legal[k] = 100; | |
} | |
#region -1 | |
//Is it off the board? | |
if ((Square - 1 > 0) ^ (Square - 1 == 0)) | |
{ | |
//Does it hit the edge? | |
if (Position[Square - 1].File == Position[Square].File - 1) | |
{ | |
//Is it free or enemy occupied? | |
if (Position[Square - 1].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//Is this square gaurded? | |
bool isInGuarded = false; | |
for (int j = 0; j < Position.Length; ++j) | |
{ | |
if (Position[j].Piece.Type != Types.King) | |
{ | |
if (Position[j].Piece.Color != Position[Square].Piece.Color) | |
{ | |
if (Position[j].Piece.Color != PieceColors.Empty) | |
{ | |
if (getSquaresLegalForSquare(Position, j, true).Contains(Square - 1) == true) | |
{ | |
//Yep. Bother... | |
isInGuarded = true; | |
} | |
} | |
} | |
} | |
} | |
if (isInGuarded == true) | |
{ | |
//Don't add it! Illegal! | |
} | |
if (isInGuarded == false) | |
{ | |
//No! All clear! | |
Legal[0] = Square - 1; | |
} | |
} | |
} | |
} | |
#endregion | |
#region -9 | |
//Is it off the board? | |
if (Square - 9 > 0) | |
{ | |
//Does it hit the edge? | |
if (Position[Square - 9].File == Position[Square].File - 1) | |
{ | |
//Is it free or enemy occupied? | |
if (Position[Square - 9].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//Is this square gaurded? | |
bool isInGuarded = false; | |
for (int j = 0; j < Position.Length; ++j) | |
{ | |
if (Position[j].Piece.Type != Types.King) | |
{ | |
if (Position[j].Piece.Color != Position[Square].Piece.Color) | |
{ | |
if (Position[j].Piece.Color != PieceColors.Empty) | |
{ | |
if (getSquaresLegalForSquare(Position, j, true).Contains(Square - 9) == true) | |
{ | |
//Yep. Bother... | |
isInGuarded = true; | |
} | |
} | |
} | |
} | |
} | |
if (isInGuarded == true) | |
{ | |
//Don't add it! | |
} | |
if (isInGuarded == false) | |
{ | |
//No! All clear! | |
Legal[1] = Square - 9; | |
} | |
} | |
} | |
} | |
#endregion | |
#region -8 | |
//Is it off the board? | |
if (Square - 8 > 0) | |
{ | |
//Does it hit the edge? | |
if (Position[Square - 8].File == Position[Square].File) | |
{ | |
//Is it free or enemy occupied? | |
if (Position[Square - 8].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//Is this square gaurded? | |
bool isInGuarded = false; | |
for (int j = 0; j < Position.Length; ++j) | |
{ | |
if (Position[j].Piece.Type != Types.King) | |
{ | |
if (Position[j].Piece.Color != Position[Square].Piece.Color) | |
{ | |
if (Position[j].Piece.Color != PieceColors.Empty) | |
{ | |
if (getSquaresLegalForSquare(Position, j, true).Contains(Square - 8) == true) | |
{ | |
//Yep. Bother... | |
isInGuarded = true; | |
} | |
} | |
} | |
} | |
} | |
if (isInGuarded == true) | |
{ | |
//Don't add it! | |
} | |
if (isInGuarded == false) | |
{ | |
//No! All clear! | |
Legal[2] = Square - 8; | |
} | |
} | |
} | |
} | |
#endregion | |
#region -7 | |
//Is it off the board? | |
if (Square - 7 > 0) | |
{ | |
//Does it hit the edge? | |
if (Position[Square - 7].File == Position[Square].File + 1) | |
{ | |
//Is it free or enemy occupied? | |
if (Position[Square - 7].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//Is this square gaurded? | |
bool isInGuarded = false; | |
for (int j = 0; j < Position.Length; ++j) | |
{ | |
if (Position[j].Piece.Type != Types.King) | |
{ | |
if (Position[j].Piece.Color != Position[Square].Piece.Color) | |
{ | |
if (Position[j].Piece.Color != PieceColors.Empty) | |
{ | |
if (getSquaresLegalForSquare(Position, j, true).Contains(Square - 7) == true) | |
{ | |
//Yep. Bother... | |
isInGuarded = true; | |
} | |
} | |
} | |
} | |
} | |
if (isInGuarded == true) | |
{ | |
//Don't add it! | |
} | |
if (isInGuarded == false) | |
{ | |
//No! All clear! | |
Legal[3] = Square - 7; | |
} | |
} | |
} | |
} | |
#endregion | |
#region +1 | |
//Is it off the board? | |
if (Square + 1 < 64) | |
{ | |
//Does it hit the edge? | |
if (Position[Square + 1].File == Position[Square].File + 1) | |
{ | |
//Is it free or enemy occupied? | |
if (Position[Square + 1].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//Is this square gaurded? | |
bool isInGuarded = false; | |
for (int j = 0; j < Position.Length; ++j) | |
{ | |
if (Position[j].Piece.Type != Types.King) | |
{ | |
if (Position[j].Piece.Color != Position[Square].Piece.Color) | |
{ | |
if (Position[j].Piece.Color != PieceColors.Empty) | |
{ | |
if (getSquaresLegalForSquare(Position, j, true).Contains(Square + 1) == true) | |
{ | |
//Yep. Bother... | |
isInGuarded = true; | |
} | |
} | |
} | |
} | |
} | |
if (isInGuarded == true) | |
{ | |
//Don't add it! | |
} | |
if (isInGuarded == false) | |
{ | |
//No! All clear! | |
Legal[4] = Square + 1; | |
} | |
} | |
} | |
} | |
#endregion | |
#region +9 | |
//Is it off the board? | |
if (Square + 9 < 64) | |
{ | |
//Does it hit the edge? | |
if (Position[Square + 9].File == Position[Square].File + 1) | |
{ | |
//Is it free or enemy occupied? | |
if (Position[Square + 9].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//Is this square gaurded? | |
bool isInGuarded = false; | |
for (int j = 0; j < Position.Length; ++j) | |
{ | |
if (Position[j].Piece.Type != Types.King) | |
{ | |
if (Position[j].Piece.Color != Position[Square].Piece.Color) | |
{ | |
if (Position[j].Piece.Color != PieceColors.Empty) | |
{ | |
if (getSquaresLegalForSquare(Position, j, true).Contains(Square + 9) == true) | |
{ | |
//Yep. Bother... | |
isInGuarded = true; | |
} | |
} | |
} | |
} | |
} | |
if (isInGuarded == true) | |
{ | |
//Don't add it! | |
} | |
if (isInGuarded == false) | |
{ | |
//No! All clear! | |
Legal[5] = Square + 9; | |
} | |
} | |
} | |
} | |
#endregion | |
#region +8 | |
//Is it off the board? | |
if (Square + 8 < 64) | |
{ | |
//Does it hit the edge? | |
if (Position[Square + 8].File == Position[Square].File) | |
{ | |
//Is it free or enemy occupied? | |
if (Position[Square + 8].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//Is this square gaurded? | |
bool isInGuarded = false; | |
for (int j = 0; j < Position.Length; ++j) | |
{ | |
if (Position[j].Piece.Type != Types.King) | |
{ | |
if (Position[j].Piece.Color != Position[Square].Piece.Color) | |
{ | |
if (Position[j].Piece.Color != PieceColors.Empty) | |
{ | |
if (getSquaresLegalForSquare(Position, j, true).Contains(Square + 8) == true) | |
{ | |
//Yep. Bother... | |
isInGuarded = true; | |
} | |
} | |
} | |
} | |
} | |
if (isInGuarded == true) | |
{ | |
//Don't add it! | |
} | |
if (isInGuarded == false) | |
{ | |
//No! All clear! | |
Legal[6] = Square + 8; | |
} | |
} | |
} | |
} | |
#endregion | |
#region +7 | |
//Is it off the board? | |
if (Square + 7 < 64) | |
{ | |
//Does it hit the edge? | |
if (Position[Square + 7].File == Position[Square].File - 1) | |
{ | |
//Is it free or enemy occupied? | |
if (Position[Square + 7].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//Is this square gaurded? | |
bool isInGuarded = false; | |
for (int j = 0; j < Position.Length; ++j) | |
{ | |
if (Position[j].Piece.Type != Types.King) | |
{ | |
if (Position[j].Piece.Color != Position[Square].Piece.Color) | |
{ | |
if (Position[j].Piece.Color != PieceColors.Empty) | |
{ | |
if (getSquaresLegalForSquare(Position, j, true).Contains(Square + 7) == true) | |
{ | |
//Yep. Bother... | |
isInGuarded = true; | |
} | |
} | |
} | |
} | |
} | |
if (isInGuarded == true) | |
{ | |
//Don't add it! | |
} | |
if (isInGuarded == false) | |
{ | |
//No! All clear! | |
Legal[7] = Square + 7; | |
} | |
} | |
} | |
} | |
#endregion | |
return Legal; | |
} | |
#endregion | |
#region Queen | |
private int[] getSquaresLegalForQueen(Square[] Position, int Square, bool isKingCheck) | |
{ | |
int[] Legal = new int[40]; | |
int dial = 0; // Determines motion | |
int previousDial = 0; //For logic on hitting the edge of the board | |
int amountSet = 0; //For Logic | |
//This sets the default | |
for (int k = 0; k < 40; ++k) | |
{ | |
Legal[k] = 100; | |
} | |
#region Up | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial + 8 > 63) | |
{ | |
amountSet = j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial + 8; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].Rank == Position[Square + previousDial].Rank + 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Right | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial + 1 > 63) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial + 1; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].File == Position[Square + previousDial].File + 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Left | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial - 1 < 0) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial - 1; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].File == Position[Square + previousDial].File - 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Down | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial - 8 < 0) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial - 8; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].Rank == Position[Square + previousDial].Rank - 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Left + Up | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial + 7 > 63) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial + 7; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].File == Position[Square + previousDial].File - 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Right + Up | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial + 9 > 63) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial + 9; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].File == Position[Square + previousDial].File + 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Right + Down | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial - 9 < 0) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial - 9; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].File == Position[Square + previousDial].File - 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Left + Down | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial - 7 < 0) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial - 7; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].File == Position[Square + previousDial].File + 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Return computed data | |
return Legal; | |
} | |
#endregion | |
#region Rook | |
private int[] getSquaresLegalForRook(Square[] Position, int Square, bool isKingCheck) | |
{ | |
int[] Legal = new int[20]; | |
int dial = 0; // Determines motion | |
int previousDial = 0; //For logic on hitting the edge of the board | |
int amountSet = 0; //For Logic | |
//This sets the default | |
for (int k = 0; k < 20; ++k) | |
{ | |
Legal[k] = 100; | |
} | |
#region Up | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial + 8 > 63) | |
{ | |
amountSet = j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial + 8; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].Rank == Position[Square + previousDial].Rank + 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Right | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial + 1 > 63) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial + 1; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].File == Position[Square + previousDial].File + 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Left | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial - 1 < 0) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial - 1; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].File == Position[Square + previousDial].File - 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Down | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial - 8 < 0) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial - 8; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].Rank == Position[Square + previousDial].Rank - 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Return computed data | |
return Legal; | |
} | |
#endregion | |
#region Bishop | |
private int[] getSquaresLegalForBishop(Square[] Position, int Square, bool isKingCheck) | |
{ | |
int[] Legal = new int[20]; | |
int dial = 0; // Determines motion | |
int previousDial = 0; //For logic on hitting the edge of the board | |
int amountSet = 0; //For Logic | |
//This sets the default | |
for (int k = 0; k < 20; ++k) | |
{ | |
Legal[k] = 100; | |
} | |
#region Left + Up | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial + 7 > 63) | |
{ | |
amountSet = j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial + 7; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].File == Position[Square + previousDial].File - 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Right + Up | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial + 9 > 63) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial + 9; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].File == Position[Square + previousDial].File + 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Right + Down | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial - 9 < 0) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial - 9; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].File == Position[Square + previousDial].File - 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Reset | |
dial = 0; | |
previousDial = 0; | |
++amountSet; | |
#region Left + Down | |
for (int j = 0; j < 8; ++j) | |
{ | |
if (Square + dial - 7 < 0) | |
{ | |
amountSet = amountSet + j; | |
break; | |
} | |
//Increment | |
previousDial = dial; | |
dial = dial - 7; // <- THIS IS THE LINE THAT DETERMINES MOTION | |
//Has it hit the edge of the board? | |
if (Position[Square + dial].File == Position[Square + previousDial].File + 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
//Would the bishop be a traitor to take this piece? | |
if (Position[Square + dial].Piece.Color != Position[Square].Piece.Color) | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
else | |
{ | |
//No! The square is either empty or (!) enemy occupied | |
Legal[j + amountSet] = Square + dial; //Add it to legal squares | |
if (Position[Square + dial].Piece.Color == PieceColors.Empty) | |
{ | |
//it was empty | |
} | |
else | |
{ | |
//Can't go beyond a piece, so ... | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
} | |
else | |
{ | |
//Yes, it has hit the edge! | |
amountSet = amountSet + j; | |
break; | |
} | |
} | |
#endregion | |
//Return computed data | |
return Legal; | |
} | |
#endregion | |
#region Knight | |
private int[] getSquaresLegalForKnight(Square[] Position, int Square, bool isKingCheck) | |
{ | |
int[] Legal = new int[8]; | |
for (int j = 0; j < 8; ++j) | |
{ | |
Legal[j] = 100; | |
} | |
if (Square - 6 > -1) | |
{ | |
if (Position[Square - 6].Rank != Position[Square].Rank) | |
{ | |
if (isKingCheck == false) | |
{ | |
if (Position[Square - 6].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[0] = Square - 6; | |
} | |
} | |
else | |
{ | |
Legal[0] = Square - 6; | |
} | |
} | |
} | |
if (Square - 15 > -1) | |
{ | |
if (Position[Square - 15].Rank != Position[Square].Rank - 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
if (Position[Square - 15].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[1] = Square - 15; | |
} | |
} | |
else | |
{ | |
Legal[1] = Square - 15; | |
} | |
} | |
} | |
if (Square - 17 > -1) | |
{ | |
if (Position[Square - 17].Rank != Position[Square].Rank - 3) | |
{ | |
if (isKingCheck == false) | |
{ | |
if (Position[Square - 17].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[2] = Square - 17; | |
} | |
} | |
else | |
{ | |
Legal[2] = Square - 17; | |
} | |
} | |
} | |
if (Square - 10 > -1) | |
{ | |
if (Position[Square - 10].Rank != Position[Square].Rank - 2) | |
{ | |
if (isKingCheck == false) | |
{ | |
if (Position[Square - 10].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[3] = Square - 10; | |
} | |
} | |
else | |
{ | |
Legal[3] = Square - 10; | |
} | |
} | |
} | |
if (Square + 10 < 64) | |
{ | |
if (Position[Square + 10].Rank != Position[Square].Rank + 2) | |
{ | |
if (isKingCheck == false) | |
{ | |
if (Position[Square + 10].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[4] = Square + 10; | |
} | |
} | |
else | |
{ | |
Legal[4] = Square + 10; | |
} | |
} | |
} | |
if (Square + 17 < 64) | |
{ | |
if (Position[Square + 17].Rank != Position[Square].Rank + 3) | |
{ | |
if (isKingCheck == false) | |
{ | |
if (Position[Square + 17].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[5] = Square + 17; | |
} | |
} | |
else | |
{ | |
Legal[5] = Square + 17; | |
} | |
} | |
} | |
if (Square + 15 < 64) | |
{ | |
if (Position[Square + 15].Rank != Position[Square].Rank + 1) | |
{ | |
if (isKingCheck == false) | |
{ | |
if (Position[Square + 15].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[6] = Square + 15; | |
} | |
} | |
else | |
{ | |
Legal[6] = Square + 15; | |
} | |
} | |
} | |
if (Square + 6 < 64) | |
{ | |
if (Position[Square + 6].Rank != Position[Square].Rank) | |
{ | |
if (isKingCheck == false) | |
{ | |
if (Position[Square + 6].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[7] = Square + 6; | |
} | |
} | |
else | |
{ | |
Legal[7] = Square + 6; | |
} | |
} | |
} | |
return Legal; | |
} | |
#endregion | |
#region Pawns | |
private int[] getSquaresLegalForBlackPawn(Square[] Position, int Square, bool isKingCheck) | |
{ | |
int[] Legal = new int[4]; | |
for (int j = 0; j < 4; ++j) | |
{ | |
Legal[j] = 100; | |
} | |
if (Position[Square].Rank == 0) | |
{ | |
return new int[0]; | |
} | |
if (isKingCheck == false) | |
{ | |
if ((Square - 8 > 0) ^ (Square - 8 == 0)) | |
{ | |
if (Position[Square - 8].Piece.Type == Types.None) | |
{ | |
if (Position[Square - 8].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[0] = Square - 8; | |
} | |
} | |
} | |
if ((Square - 7 > 0) ^ (Square - 7 == 0)) | |
{ | |
if (Position[Square - 7].Piece.Type != Types.None) | |
{ | |
if (Position[Square - 7].File == Position[Square].File + 1) | |
{ | |
if (Position[Square - 7].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[1] = Square - 7; | |
} | |
} | |
} | |
} | |
if ((Square - 9 > 0) ^ (Square - 9 == 0)) | |
{ | |
if (Position[Square - 9].Piece.Type != Types.None) | |
{ | |
if (Position[Square - 9].File == Position[Square].File - 1) | |
{ | |
if (Position[Square - 9].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[2] = Square - 9; | |
} | |
} | |
} | |
} | |
if (Position[Square].Rank == 6) | |
{ | |
if (Position[Square - 8].Piece.Type == Types.None) | |
{ | |
if (Position[Square - 16].Piece.Type == Types.None) | |
{ | |
if (Position[Square - 16].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[3] = Square - 16; | |
} | |
} | |
} | |
} | |
} | |
if (isKingCheck == true) | |
{ | |
if ((Square - 7 > 0) ^ (Square - 7 == 0)) | |
{ | |
if (Position[Square - 7].File == Position[Square].File + 1) | |
{ | |
Legal[1] = Square - 7; | |
} | |
} | |
if ((Square - 9 > 0) ^ (Square - 9 == 0)) | |
{ | |
if (Position[Square - 9].File == Position[Square].File - 1) | |
{ | |
Legal[2] = Square - 9; | |
} | |
} | |
} | |
return Legal; | |
} | |
private int[] getSquaresLegalForWhitePawn(Square[] Position, int Square, bool isKingCheck) | |
{ | |
int[] Legal = new int[4]; | |
for (int j = 0; j < 4; ++j) | |
{ | |
Legal[j] = 100; | |
} | |
if (Position[Square].Rank == 7) | |
{ | |
return new int[0]; | |
} | |
if (isKingCheck == false) | |
{ | |
if (Square + 8 < 64) | |
{ | |
if (Position[Square + 8].Piece.Type == Types.None) | |
{ | |
if (Position[Square + 8].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[0] = Square + 8; | |
} | |
} | |
} | |
if (Square + 7 < 64) | |
{ | |
if (Position[Square + 7].Piece.Type != Types.None) | |
{ | |
if (Position[Square + 7].File == Position[Square].File - 1) | |
{ | |
if (Position[Square + 7].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[1] = Square + 7; | |
} | |
} | |
} | |
} | |
if (Square + 9 < 64) | |
{ | |
if (Position[Square + 9].Piece.Type != Types.None) | |
{ | |
if (Position[Square + 9].File == Position[Square].File + 1) | |
{ | |
if (Position[Square + 9].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[2] = Square + 9; | |
} | |
} | |
} | |
} | |
if (Position[Square].Rank == 1) | |
{ | |
if (Position[Square + 8].Piece.Type == Types.None) | |
{ | |
if (Position[Square + 16].Piece.Type == Types.None) | |
{ | |
if (Position[Square + 16].Piece.Color != Position[Square].Piece.Color) | |
{ | |
Legal[3] = Square + 16; | |
} | |
} | |
} | |
} | |
} | |
if (isKingCheck == true) | |
{ | |
if (Square + 7 < 64) | |
{ | |
if (Position[Square + 7].File == Position[Square].File - 1) | |
{ | |
Legal[1] = Square + 7; | |
} | |
} | |
if (Square + 9 < 64) | |
{ | |
if (Position[Square + 9].File == Position[Square].File + 1) | |
{ | |
Legal[2] = Square + 9; | |
} | |
} | |
} | |
return Legal; | |
} | |
#endregion | |
/// <summary> | |
/// Create a new legality class | |
/// </summary> | |
public Legality() | |
{ | |
#region Initialize Memory Position | |
int Rank = 0; | |
int File = 0; | |
bool blackWhite = false; | |
for (int anInteger = 0; anInteger < 64; ++anInteger) | |
{ | |
testPosition[anInteger] = new Square(); | |
} | |
for (int anInteger = 0; anInteger < 64; ++anInteger) | |
{ | |
if (File == 8) | |
{ | |
File = 0; | |
} | |
testPosition[anInteger].File = File; | |
++File; | |
} | |
File = 0; | |
for (int anInteger = 0; anInteger < 64; ++anInteger) | |
{ | |
if (testPosition[anInteger].File == 7) | |
{ | |
testPosition[anInteger].Rank = Rank; | |
++Rank; | |
} | |
else | |
{ | |
testPosition[anInteger].Rank = Rank; | |
} | |
} | |
for (int anInteger = 0; anInteger < 64; ++anInteger) | |
{ | |
if (File == 8) | |
{ | |
File = 0; | |
if (blackWhite == true) | |
{ | |
blackWhite = false; | |
} | |
else | |
{ | |
blackWhite = true; | |
} | |
} | |
testPosition[anInteger].Color = blackWhite; | |
if (blackWhite == true) | |
{ | |
blackWhite = false; | |
} | |
else | |
{ | |
blackWhite = true; | |
} | |
++File; | |
} | |
#endregion | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment