public bool isSquareAvailable(Vector2Int square, ChessPiece pieceToMove, PieceMoveRestriction pieceMoveRestriction = PieceMoveRestriction.OnlyWhenPositionFreeOrOccupiedByOpponent, bool bSameColorOverride = false) { //Check if square is in bounds. if (square.x > 7 || square.x < 0 || square.y > 7 || square.y < 0) { return(false); //Out of bounds } Square squareDestination = squares[square.x, square.y]; if (pieceMoveRestriction == PieceMoveRestriction.OnlyWhenPositionCannotResultInPossibleDeath) { List <Vector2Int> squaresOverride = new List <Vector2Int>(); squaresOverride.Add(square); List <Vector2Int> allAvailableMovesFromOpponent = ChessBoard.getInstance().GetAllAvailableMovesFromThisOrientation(pieceToMove.getOrientation() * -1, squaresOverride, true); for (int i = 0; i < allAvailableMovesFromOpponent.Count; i++) { if (allAvailableMovesFromOpponent[i] == square) { return(false); } } } if (squareDestination.isOccupied) { if ((squareDestination.piece.getOrientation() != pieceToMove.getOrientation() && (pieceMoveRestriction <= PieceMoveRestriction.OnlyWhenPositionOccupiedByOpponent || pieceMoveRestriction == PieceMoveRestriction.OnlyWhenPositionCannotResultInPossibleDeath)) || bSameColorOverride) { return(true); } } else { if (pieceMoveRestriction == PieceMoveRestriction.OnlyWhenPositionFreeOrOccupiedByOpponent || pieceMoveRestriction == PieceMoveRestriction.OnlyWhenPositionFree || pieceMoveRestriction == PieceMoveRestriction.OnlyWhenPositionCannotResultInPossibleDeath) { return(true); } } return(false); }
public override List <Vector2Int> getAvailableMoves(List <Vector2Int> squaresOverride = default(List <Vector2Int>), PieceMoveRestriction pieceMoveRestriction = PieceMoveRestriction.OnlyWhenPositionFreeOrOccupiedByOpponent, bool bCheckSameColor = false, bool bFirstCheck = false) { List <Vector2Int> availableMoves = new List <Vector2Int>(); pieceMoveRestriction = PieceMoveRestriction.OnlyWhenPositionCannotResultInPossibleDeath; if (squaresOverride != null && squaresOverride.Count > 0) { pieceMoveRestriction = PieceMoveRestriction.OnlyWhenPositionFreeOrOccupiedByOpponent; } TryAddingAvailableMove(ref availableMoves, 0, 1, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, 1, 0, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, 1, 1, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, -1, -1, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, -1, 1, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, 1, -1, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, -1, 0, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, 0, -1, pieceMoveRestriction, bCheckSameColor); return(availableMoves); }
public override List <Vector2Int> getAvailableMoves(List <Vector2Int> squaresOverride = default(List <Vector2Int>), PieceMoveRestriction pieceMoveRestriction = PieceMoveRestriction.OnlyWhenPositionFreeOrOccupiedByOpponent, bool bCheckSameColor = false, bool bFirstCheck = false) { List <Vector2Int> availableMoves = new List <Vector2Int>(); TryAddingAvailableMove(ref availableMoves, 2, 1, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, 2, -1, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, 1, 2, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, 1, -2, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, -2, -1, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, -2, 1, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, -1, -2, pieceMoveRestriction, bCheckSameColor); TryAddingAvailableMove(ref availableMoves, -1, 2, pieceMoveRestriction, bCheckSameColor); if (bFirstCheck) { RemoveAvailableMovesIfTheyResultInACheck(ref availableMoves); } return(availableMoves); }
public virtual List <Vector2Int> getAvailableMoves(List <Vector2Int> squaresOverride = default(List <Vector2Int>), PieceMoveRestriction pieceMoveRestriction = PieceMoveRestriction.OnlyWhenPositionFreeOrOccupiedByOpponent, bool bSameColorOverride = false, bool bFirstCheck = false) { List <Vector2Int> availMoves = new List <Vector2Int>(); foreach (Vector2Int squareToCheck in m_MovementOffsets) { if (m_chessBoard.isSquareAvailable(squareToCheck + this.m_PlayerPos, this, pieceMoveRestriction, bSameColorOverride)) { availMoves.Add(squareToCheck); } if (squaresOverride != null && squaresOverride.Count > 0) { foreach (Vector2Int squareOveride in squaresOverride) { if (squareOveride == squareToCheck) { availMoves.Add(squareOveride); } } } } //Check Squares in direction foreach (Vector2Int direction in m_DirectionVectors) { int i = 1; bool checkDirection = true; bool bIgnoreFirstKingCheck = true; while (checkDirection) { Vector2Int squareToCheck = direction * i + this.m_PlayerPos; checkDirection = m_chessBoard.isSquareAvailable(squareToCheck, this, pieceMoveRestriction, bSameColorOverride); if (!checkDirection) { break; } availMoves.Add(squareToCheck); i++; ChessPiece chessPieceToCheck = m_chessBoard.GetChessPieceInThisSquare(squareToCheck); if (chessPieceToCheck != null && m_chessBoard.isSquareOccupied(squareToCheck, this)) { if (bSameColorOverride) { if (bIgnoreFirstKingCheck && chessPieceToCheck.getPieceType() == PieceType.King) { bIgnoreFirstKingCheck = false; } else { break; } } else { break; } } } } if (bFirstCheck) { RemoveAvailableMovesIfTheyResultInACheck(ref availMoves); } return(availMoves); }
public virtual bool TryAddingAvailableMove(ref List <Vector2Int> availableMoves, int x, int y, PieceMoveRestriction pieceMoveRestriction, bool bSameColorOverride = false) { Vector2Int newMove = new Vector2Int(m_PlayerPos.x + x * m_PlayerOrientation, m_PlayerPos.y + y * m_PlayerOrientation); if (m_chessBoard.isSquareAvailable(newMove, this, pieceMoveRestriction, bSameColorOverride)) { availableMoves.Add(newMove); return(true); } return(false); }
public override List <Vector2Int> getAvailableMoves(List <Vector2Int> squaresOverride = default(List <Vector2Int>), PieceMoveRestriction pieceMoveRestriction = PieceMoveRestriction.OnlyWhenPositionFreeOrOccupiedByOpponent, bool bSameColorOverride = false, bool bFirstCheck = false) { //Pawns are only able to move forward two squares on their first move, and one square forward on their subsquent moves. They can also only kill other pieces //one square forward diagonally to the left or right and they can only do that move while killing opponents piece. List <Vector2Int> availableMoves = new List <Vector2Int>(); if (squaresOverride != null && squaresOverride.Count > 0) { TryAddingAvailableMove(ref availableMoves, 1, 1, PieceMoveRestriction.OnlyWhenPositionFreeOrOccupiedByOpponent, bSameColorOverride); TryAddingAvailableMove(ref availableMoves, 1, -1, PieceMoveRestriction.OnlyWhenPositionFreeOrOccupiedByOpponent, bSameColorOverride); } else { if (TryAddingAvailableMove(ref availableMoves, 1, 0, PieceMoveRestriction.OnlyWhenPositionFree) && m_bFirstMove) { TryAddingAvailableMove(ref availableMoves, 2, 0, PieceMoveRestriction.OnlyWhenPositionFree); } TryAddingAvailableMove(ref availableMoves, 1, 1, PieceMoveRestriction.OnlyWhenPositionOccupiedByOpponent, bSameColorOverride); TryAddingAvailableMove(ref availableMoves, 1, -1, PieceMoveRestriction.OnlyWhenPositionOccupiedByOpponent, bSameColorOverride); } if (bFirstCheck) { RemoveAvailableMovesIfTheyResultInACheck(ref availableMoves); } return(availableMoves); }