public bool IsPawnInitialPosition(string pawnPosition) { SquareConfiguration pawnSquareConfiguration = BoardConfiguration.Instance.GetPieceAtSquare(pawnPosition); return(pawnSquareConfiguration.MovingDirection == -1 && pawnPosition[1] == '2' || pawnSquareConfiguration.MovingDirection == 1 && pawnPosition[1] == '7'); }
public void SetPiecePosition(char piece, bool color, string square, int movingDirection) { SquareConfiguration newSquareConfiguration = new SquareConfiguration(piece, color, movingDirection); newSquareConfiguration.MovingDirection = movingDirection; Config[square] = newSquareConfiguration; }
public void TestSquareConfigurationConstructor() { SquareConfiguration squareConfiguration = new SquareConfiguration('K', false); Assert.True(squareConfiguration.Color == false); Assert.True(squareConfiguration.Piece == 'K'); }
public List <string> GetNextPossiblePositionsForPieceAtSquare(string piecePosition) { SquareConfiguration squareConfiguration = BoardConfiguration.Instance.GetPieceAtSquare(piecePosition); if (squareConfiguration == null) { return(null); } List <List <Vector2> > allowedMovesDeltas = MovesList.Instance.AllowedMovesIndexes[squareConfiguration.Piece]; Vector2 origin = Utils.ConvertToCartesian(piecePosition[1], piecePosition[0]); List <string> nextPossiblePositions = new List <string>(); foreach (List <Vector2> deltasForDirection in allowedMovesDeltas) { foreach (Vector2 delta in deltasForDirection) { Vector2 nextPosition; nextPosition.x = origin.x + squareConfiguration.MovingDirection * delta.x; nextPosition.y = origin.y + delta.y; if (Utils.IsInsideBoard((int)nextPosition.x, (int)nextPosition.y)) { string nextSquare = Utils.ConverToAlgebraicNotation((int)nextPosition.x, (int)nextPosition.y); SquareConfiguration nextSquareConfiguration = BoardConfiguration.Instance.GetPieceAtSquare(nextSquare); if (nextSquareConfiguration == null || (nextSquareConfiguration.Color != squareConfiguration.Color && squareConfiguration.Piece != 'P')) { nextPossiblePositions.Add(nextSquare); } if (nextSquareConfiguration != null) { break; } } } } // Handle the special cases for pawn separately if (squareConfiguration.Piece == 'P') { nextPossiblePositions.AddRange(GetDiagonalMovesForPawn(piecePosition)); AddTwoSquareStepMoveForPawn(piecePosition, nextPossiblePositions); } return(nextPossiblePositions); }
private void AddTwoSquareStepMoveForPawn(string piecePosition, List <string> allowedMovesForPawn) { if (IsPawnInitialPosition(piecePosition)) { SquareConfiguration squareConfiguration = BoardConfiguration.Instance.GetPieceAtSquare(piecePosition); Vector2 pawnCartesianCoordinates = Utils.ConvertToCartesian(piecePosition[1], piecePosition[0]); bool pawnColor = BoardConfiguration.Instance.GetPieceAtSquare(piecePosition).Color; Vector2 twoSquareMove = pawnCartesianCoordinates + new Vector2(2 * squareConfiguration.MovingDirection, 0); Vector2 oneSquareMove = pawnCartesianCoordinates + new Vector2(squareConfiguration.MovingDirection, 0); SquareConfiguration twoSquareMoveConfiguration = BoardConfiguration.Instance.GetPieceAtSquare(Utils.ConvertCartesianToAlgebraic(twoSquareMove)); SquareConfiguration oneSquareMoveConfiguration = BoardConfiguration.Instance.GetPieceAtSquare(Utils.ConvertCartesianToAlgebraic(oneSquareMove)); if (Utils.IsInsideBoard((int)twoSquareMove.x, (int)twoSquareMove.y) && twoSquareMoveConfiguration == null && oneSquareMoveConfiguration == null) { allowedMovesForPawn.Add(Utils.ConvertCartesianToAlgebraic(twoSquareMove)); } } }
public List <string> GetDiagonalMovesForPawn(string piecePosition) { SquareConfiguration squareConfiguration = BoardConfiguration.Instance.GetPieceAtSquare(piecePosition); if (squareConfiguration.Piece != 'P') { return(null); } List <string> allowedMovesForPawn = new List <string>(); Vector2 pawnCartesianCoordinates = Utils.ConvertToCartesian(piecePosition[1], piecePosition[0]); bool pawnColor = BoardConfiguration.Instance.GetPieceAtSquare(piecePosition).Color; Vector2 upLeftMove = pawnCartesianCoordinates + new Vector2(squareConfiguration.MovingDirection, -1); Vector2 upRightMove = pawnCartesianCoordinates + new Vector2(squareConfiguration.MovingDirection, 1); if (Utils.IsInsideBoard((int)upLeftMove.x, (int)upLeftMove.y) == true) { string upLeftMoveAlgebraicCoordinates = Utils.ConvertCartesianToAlgebraic(upLeftMove); if (CanPawnCapturePiece(upLeftMoveAlgebraicCoordinates, pawnColor)) { allowedMovesForPawn.Add(upLeftMoveAlgebraicCoordinates); } } if (Utils.IsInsideBoard((int)upRightMove.x, (int)upRightMove.y) == true) { string upRightMoveAlgebraicCoordinates = Utils.ConvertCartesianToAlgebraic(upRightMove); if (CanPawnCapturePiece(upRightMoveAlgebraicCoordinates, pawnColor)) { allowedMovesForPawn.Add(upRightMoveAlgebraicCoordinates); } } return(allowedMovesForPawn); }
public bool IsCheckMateForPlayer(bool playerColor) { //Test if the player is in check if (IsCheckForPlayer(playerColor) == false) { //Debug.Log("The king is not in check"); return(false); } //Test if there is one move that could prevent the checkmate foreach (string squarePosition in BoardConfiguration.SquareAlgebraicNotations) { SquareConfiguration value = BoardConfiguration.Instance.GetPieceAtSquare(squarePosition); if (value != null && value.Color == playerColor) { List <string> possibleMovesForCurrentPiece = GetNextPossiblePositionsForPieceAtSquare(squarePosition); foreach (string possibleMove in possibleMovesForCurrentPiece) { //Debug.Log("Trying to move " + value.Piece + " from " + squarePosition + "to " + possibleMove); SquareConfiguration pieceAtPossibleMove = BoardConfiguration.Instance.GetPieceAtSquare(possibleMove); BoardConfiguration.Instance.MovePiece(squarePosition, possibleMove); if (IsCheckForPlayer(playerColor) == false) { //Debug.Log("King can escape by moving to: " + possibleMove); BoardConfiguration.Instance.MovePiece(possibleMove, squarePosition); return(false); } BoardConfiguration.Instance.MovePiece(possibleMove, squarePosition); BoardConfiguration.Config[possibleMove] = pieceAtPossibleMove; } } } return(true); }
public static bool IsPawnInitialPosition(string square) { SquareConfiguration pawnPiece = BoardConfiguration.Instance.GetPieceAtSquare(square); return(pawnPiece.MovingDirection == 1 && square[1] != '7' || pawnPiece.MovingDirection == -1 && square[1] != '2'); }
private bool CanPawnCapturePiece(string pieceLocation, bool pawnColor) { SquareConfiguration pieceSquareConfiguration = BoardConfiguration.Instance.GetPieceAtSquare(pieceLocation); return(pieceSquareConfiguration != null && pieceSquareConfiguration.Color != pawnColor); }