Ejemplo n.º 1
0
        private void CellSelected(object sender, RoutedEventArgs e)
        {
            var cell = e.ButtonTag <Cell>();

            if (cell.Figure.Side == Core.Board.Side.Empty)
            {
                MakeMove(this, null);
            }
            else
            {
                AvailableMoves.Clear();
                if (WalkMoves.TryGetValue(cell.Figure, out var figureMoves) && figureMoves.Length > 0)
                {
                    foreach (var move in figureMoves.Select((gameMove, i) => new MovesModel($"{i + 1}", i, gameMove)))
                    {
                        AvailableMoves.Add(move);
                    }
                }
            }

            SelectedFigure = cell.Figure;

            foreach (var c in Cells)
            {
                if (c.Type == PieceType.None)
                {
                    c.Active = false;
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Adds the move in list of available moves for the specific match
 /// </summary>
 /// <returns><c>true</c>, if move was added, <c>false</c> otherwise.</returns>
 /// <param name="move">Move.</param>
 public bool AddMove(Move move)
 {
     if (AvailableMoves.FindIndex(a => a.Label.ToLower() == move.Label.ToLower()) == -1)
     {
         AvailableMoves.Add(move);
         return(true);
     }
     else
     {
         WriteLine("Move with label {0} exists!", move.Label);
     }
     return(false);
 }
Ejemplo n.º 3
0
    public override void GeneratePossibleMoves(ChessGridInfo grid, ChessGridInfo gridCopy, ChessPlayer playerCopy,
                                               ChessPlayer enemyCopy, bool checkMovesValidity, bool checkSpecialMoves)
    {
        AvailableMoves.Clear();
        Vector2Int newSquare = Vector2Int.zero;

        for (int i = 0; i < possibleDirections.GetLength(0); i++)
        {
            if (i == 1 && MovedFirstTime)
            {
                break;
            }

            int newX = SquareIndex.x + possibleDirections[i, 0];
            int newY = SquareIndex.y + possibleDirections[i, 1];

            newSquare.Set(newX, newY);

            if (grid.CheckIfSquareIndexIsValid(newSquare))
            {
                Piece piece = grid.GetPieceOnSquareIndex(newSquare);
                Move  move  = CheckPromotionFlag(newSquare, piece);

                if (checkMovesValidity && piece == null)
                {
                    Piece attackedPieceCopy = piece == null ? null : gridCopy.GetPieceOnSquareIndex(piece.SquareIndex);
                    Move  moveCopy          = new Move(gridCopy.GetPieceOnSquareIndex(SquareIndex), newSquare, attackedPieceCopy, move.PromotionFlag);
                    bool  areMovesValid     = CheckIfMoveIsValid(gridCopy, playerCopy, enemyCopy, moveCopy);

                    if (!areMovesValid)
                    {
                        continue;
                    }
                }

                if (piece == null)
                {
                    AvailableMoves.Add(move);
                }
                else
                {
                    break;
                }
            }
        }

        PieceAttack(grid, gridCopy, playerCopy, enemyCopy, checkMovesValidity);
        EnPassant(grid, gridCopy, playerCopy, enemyCopy, checkMovesValidity);
    }
Ejemplo n.º 4
0
    public void AddMove(CheckMovesData checkMovesData, int offset)
    {
        if (!checkMovesData._valid)
        {
            return;
        }

        Move thisMove = new Move();

        thisMove.startPosition = position;
        thisMove.endPosition   = position + checkMovesData.i * offset;
        thisMove.piece         = this;

        AvailableMoves.Add(thisMove);
    }
Ejemplo n.º 5
0
    public override void GeneratePossibleMoves(ChessGridInfo grid, ChessGridInfo gridCopy, ChessPlayer playerCopy,
                                               ChessPlayer enemyCopy, bool checkMovesValidity, bool checkSpecialMoves)
    {
        AvailableMoves.Clear();
        Vector2Int newSquare = Vector2Int.zero;

        for (int i = 0; i < possibleDirections.GetLength(0); i++)
        {
            int newX = SquareIndex.x + possibleDirections[i, 0];
            int newY = SquareIndex.y + possibleDirections[i, 1];

            newSquare.Set(newX, newY);

            if (grid.CheckIfSquareIndexIsValid(newSquare))
            {
                Piece piece = grid.GetPieceOnSquareIndex(newSquare);
                Move  move  = new Move(this, newSquare, piece);

                if (checkMovesValidity && piece != null && piece.PieceType == PieceType.King)
                {
                    continue;
                }

                if (checkMovesValidity && (piece == null || (piece != null && piece.TeamColor != TeamColor)))
                {
                    Piece attackedPieceCopy = piece == null ? null : gridCopy.GetPieceOnSquareIndex(piece.SquareIndex);
                    Move  moveCopy          = new Move(gridCopy.GetPieceOnSquareIndex(SquareIndex), newSquare, attackedPieceCopy);
                    bool  areMovesValid     = CheckIfMoveIsValid(gridCopy, playerCopy, enemyCopy, moveCopy);

                    if (!areMovesValid)
                    {
                        continue;
                    }
                }

                if (piece == null)
                {
                    AvailableMoves.Add(move);
                }
                else if (piece != null && TeamColor != piece.TeamColor)
                {
                    AvailableMoves.Add(move);
                }
            }
        }
    }
Ejemplo n.º 6
0
    private void PieceAttack(ChessGridInfo grid, ChessGridInfo gridCopy, ChessPlayer playerCopy,
                             ChessPlayer enemyCopy, bool checkMovesValidity)
    {
        Vector2Int newSquare = Vector2Int.zero;

        for (int i = 0; i < possibleAttacks.GetLength(0); i++)
        {
            int newX = SquareIndex.x + possibleAttacks[i, 0];
            int newY = SquareIndex.y + possibleAttacks[i, 1];

            newSquare.Set(newX, newY);

            if (grid.CheckIfSquareIndexIsValid(newSquare))
            {
                Piece piece = grid.GetPieceOnSquareIndex(newSquare);
                Move  move  = CheckPromotionFlag(newSquare, piece);

                if (checkMovesValidity && piece != null && piece.PieceType == PieceType.King)
                {
                    continue;
                }

                if (checkMovesValidity && (piece != null && piece.TeamColor != TeamColor))
                {
                    Piece attackedPieceCopy = piece == null ? null : gridCopy.GetPieceOnSquareIndex(piece.SquareIndex);
                    Move  moveCopy          = new Move(gridCopy.GetPieceOnSquareIndex(SquareIndex), newSquare, attackedPieceCopy, move.PromotionFlag);
                    bool  areMovesValid     = CheckIfMoveIsValid(gridCopy, playerCopy, enemyCopy, moveCopy);

                    if (!areMovesValid)
                    {
                        continue;
                    }
                }

                if (piece != null && TeamColor != piece.TeamColor)
                {
                    AvailableMoves.Add(move);
                }
            }
        }
    }
Ejemplo n.º 7
0
    private void EnPassant(ChessGridInfo grid, ChessGridInfo gridCopy, ChessPlayer playerCopy,
                           ChessPlayer enemyCopy, bool checkMovesValidity)
    {
        int[,] directions = { { -1, 0 }, { 1, 0 } };
        Vector2Int newSquare = Vector2Int.zero;

        for (int i = 0; i < directions.GetLength(0); i++)
        {
            int newX = SquareIndex.x + directions[i, 0];
            int newY = SquareIndex.y + directions[i, 1];

            newSquare.Set(newX, newY);

            if (grid.CheckIfSquareIndexIsValid(newSquare))
            {
                Piece      piece  = grid.GetPieceOnSquareIndex(newSquare);
                Vector2Int square = new Vector2Int(SquareIndex.x + enPassantMoves[i, 0], SquareIndex.y + enPassantMoves[i, 1]);
                Move       move   = CheckPromotionFlag(square, piece);

                if (checkMovesValidity && piece != null && piece.PieceType == PieceType.Pawn && piece == grid.PieceMovedInPreviousTurn &&
                    Mathf.Abs(piece.SquareIndex.y - piece.PreviousSquareIndex.y) == 2 && piece.SquareIndex.x == piece.PreviousSquareIndex.x)
                {
                    Piece attackedPieceCopy = piece == null ? null : gridCopy.GetPieceOnSquareIndex(piece.SquareIndex);
                    Move  moveCopy          = new Move(gridCopy.GetPieceOnSquareIndex(SquareIndex), square, attackedPieceCopy, move.PromotionFlag);
                    bool  areMovesValid     = CheckIfMoveIsValid(gridCopy, playerCopy, enemyCopy, moveCopy);

                    if (!areMovesValid)
                    {
                        continue;
                    }
                }

                if (piece != null && piece.PieceType == PieceType.Pawn && piece == grid.PieceMovedInPreviousTurn &&
                    Mathf.Abs(piece.SquareIndex.y - piece.PreviousSquareIndex.y) == 2 && piece.SquareIndex.x == piece.PreviousSquareIndex.x)
                {
                    AvailableMoves.Add(move);
                    break;
                }
            }
        }
    }
Ejemplo n.º 8
0
    private void Castling(ChessGridInfo grid, ChessGridInfo gridCopy, ChessPlayer enemyCopy)
    {
        if (MovedFirstTime)
        {
            return;
        }

        enemyCopy.GenerateAllPossibleMoves(gridCopy, false, false);

        if (enemyCopy.IsFieldUnderAttack(SquareIndex))
        {
            return;
        }

        int   kingRow   = SquareIndex.y;
        Piece leftRook  = grid.GetPieceOnSquareIndex(new Vector2Int(0, kingRow));
        Piece rightRook = grid.GetPieceOnSquareIndex(new Vector2Int(ChessGridInfo.GRID_SIZE - 1, kingRow));

        if (leftRook != null && leftRook.PieceType == PieceType.Rook && leftRook.TeamColor == TeamColor &&
            !leftRook.MovedFirstTime && leftRook.SquareIndex.y == SquareIndex.y)
        {
            bool       isLeftCastlingPossible = true;
            Vector2Int square = Vector2Int.zero;

            for (int i = leftRook.SquareIndex.x + 1; i < SquareIndex.x; i++)
            {
                square.Set(i, kingRow);
                if (gridCopy.GetPieceOnSquareIndex(square) != null ||
                    (i > 1 && enemyCopy.IsFieldUnderAttack(square)))
                {
                    isLeftCastlingPossible = false;
                    break;
                }
            }

            if (isLeftCastlingPossible)
            {
                Move move = new Move(this, new Vector2Int(2, kingRow), null, false, true, leftRook, new Vector2Int(3, kingRow));
                AvailableMoves.Add(move);
            }
        }

        if (rightRook != null && rightRook.PieceType == PieceType.Rook && rightRook.TeamColor == TeamColor &&
            !rightRook.MovedFirstTime && rightRook.SquareIndex.y == SquareIndex.y)
        {
            bool       isRightCastlingPossible = true;
            Vector2Int square = Vector2Int.zero;

            for (int i = SquareIndex.x + 1; i < rightRook.SquareIndex.x; i++)
            {
                square.Set(i, kingRow);
                if (gridCopy.GetPieceOnSquareIndex(square) != null || enemyCopy.IsFieldUnderAttack(square))
                {
                    isRightCastlingPossible = false;
                    break;
                }
            }

            if (isRightCastlingPossible)
            {
                Move move = new Move(this, new Vector2Int(6, kingRow), null, false, true, rightRook, new Vector2Int(5, kingRow));
                AvailableMoves.Add(move);
            }
        }
    }