Esempio n. 1
0
        //
        // Summary:
        //     Make the move, this also includes catching a possible piece in the process.
        //
        // Parameters:
        //   origin:
        //     The piece that will make its move.
        //
        //   target:
        //     The position of the final destination of the Piece origin.
        public void MakeMovement(Piece origin, BoardPosition target)
        {
            TwoDimensionsArrayPosition originArrPos = origin.BoardPosition.ToArrayPosition(Board.Height);
            TwoDimensionsArrayPosition targetArrPos = target.ToArrayPosition(Board.Height);

            // If the target results in a capture
            if (Math.Abs(originArrPos.Row - targetArrPos.Row) >= 2)
            {
                // The 'P' represents a piece, not a Pawn itself or a Dame, and the 'E' an enemy piece.
                //
                // 4---1
                // -E-E-
                // --P--
                // -E-E-
                // 3---2

                TwoDimensionsArrayPosition piece2BCapPos;
                // Locate and set the position of the piece to be captured
                if ((originArrPos.Column - targetArrPos.Column) <= -2) // NE && SE
                {
                    if ((originArrPos.Row - targetArrPos.Row) >= 2)    // NE (1)
                    {
                        piece2BCapPos = new TwoDimensionsArrayPosition(targetArrPos.Row + 1, targetArrPos.Column - 1);
                    }
                    else // SE (2)
                    {
                        piece2BCapPos = new TwoDimensionsArrayPosition(targetArrPos.Row - 1, targetArrPos.Column - 1);
                    }
                }
                else // SW && NW
                {
                    if ((originArrPos.Row - targetArrPos.Row) <= -2) // SW (3)
                    {
                        piece2BCapPos = new TwoDimensionsArrayPosition(targetArrPos.Row - 1, targetArrPos.Column + 1);
                    }
                    else // NW (4)
                    {
                        piece2BCapPos = new TwoDimensionsArrayPosition(targetArrPos.Row + 1, targetArrPos.Column + 1);
                    }
                }


                if (TurnPlayer == Team.Red)
                {
                    WhitePieces.Remove(Board.TakePiece(piece2BCapPos));
                }
                else
                {
                    RedPieces.Remove(Board.TakePiece(piece2BCapPos));
                }

                // Take the origin from the board and place it in the target
                Board.PlacePiece(Board.TakePiece(origin.BoardPosition), target);
            }
            else
            {
                // Take the origin from the board and place it in the target
                Board.PlacePiece(Board.TakePiece(origin.BoardPosition), target);
            }

            // If the Red pawn or a White pawn is in its promotion area it'll be promoted
            switch (Board.Pieces(target))
            {
            case Pawn _ when Board.Pieces(target).Team == Team.Red && target.Row == 1:
            case Pawn _ when Board.Pieces(target).Team == Team.White && target.Row == 10:
                Promotion(Board.Pieces(target) as Pawn);

                break;
            }
        }
Esempio n. 2
0
        //
        // Summary:
        //     Create an array with the movements actually avaliable to the Pawn.
        //
        // Returns:
        //     An array of booleans with the same size of the board where the
        //     positions contain a true value if it is a possible movement;
        //     otherwise it contains false.
        public override bool[,] PossibleTargets()
        {
            // Red team
            // 4---1
            // -----
            // --P--
            // -3-2-
            //
            // White team
            // -4-1-
            // --P--
            // -----
            // 3---2

            bool[,] possibleTargets = new bool[Board.Height, Board.Width];
            TwoDimensionsArrayPosition arrPos = BoardPosition.ToArrayPosition(Board.Height);

            if (Team == Team.Red) // Red pawn movement
            {
                // Indicates if there're any targets that results in captures.
                bool withoutCaptures = true;

                // NE (1) (Only captures avaliable)
                if ((arrPos.Row - 2) >= 0 && (arrPos.Column + 2) < Board.Width && Board.Pieces(arrPos.Row - 1, arrPos.Column + 1) != null && Board.Pieces(arrPos.Row - 1, arrPos.Column + 1).Team != Team)
                {
                    if (Board.Pieces(arrPos.Row - 2, arrPos.Column + 2) == null)
                    {
                        withoutCaptures = false;
                        possibleTargets[arrPos.Row - 2, arrPos.Column + 2] = true;
                    }
                }

                // SE (2) (Only captures avaliable)
                if ((arrPos.Row + 1) < Board.Height && (arrPos.Column + 1) < Board.Width && Board.Pieces(arrPos.Row + 1, arrPos.Column + 1) != null && Board.Pieces(arrPos.Row + 1, arrPos.Column + 1).Team != Team)
                {
                    if ((arrPos.Row + 2) < Board.Height && (arrPos.Column + 2) < Board.Width && Board.Pieces(arrPos.Row + 2, arrPos.Column + 2) == null)
                    {
                        withoutCaptures = false;
                        possibleTargets[arrPos.Row + 2, arrPos.Column + 2] = true;
                    }
                }

                // SW (3) (Only captures avaliable)
                if ((arrPos.Row + 1) < Board.Height && (arrPos.Column - 1) >= 0 && Board.Pieces(arrPos.Row + 1, arrPos.Column - 1) != null && Board.Pieces(arrPos.Row + 1, arrPos.Column - 1).Team != Team)
                {
                    if ((arrPos.Row + 2) < Board.Height && (arrPos.Column - 2) >= 0 && Board.Pieces(arrPos.Row + 2, arrPos.Column - 2) == null)
                    {
                        withoutCaptures = false;
                        possibleTargets[arrPos.Row + 2, arrPos.Column - 2] = true;
                    }
                }

                // NW (4) (Only captures avaliable)
                if ((arrPos.Row - 2) >= 0 && (arrPos.Column - 2) >= 0 && Board.Pieces(arrPos.Row - 1, arrPos.Column - 1) != null && Board.Pieces(arrPos.Row - 1, arrPos.Column - 1).Team != Team)
                {
                    if (Board.Pieces(arrPos.Row - 2, arrPos.Column - 2) == null)
                    {
                        withoutCaptures = false;
                        possibleTargets[arrPos.Row - 2, arrPos.Column - 2] = true;
                    }
                }

                if (withoutCaptures)
                {
                    // SE (2)
                    if ((arrPos.Row + 1) < Board.Height && (arrPos.Column + 1) < Board.Width && Board.Pieces(arrPos.Row + 1, arrPos.Column + 1) == null)
                    {
                        possibleTargets[arrPos.Row + 1, arrPos.Column + 1] = true;
                    }
                    // SW (3)
                    if ((arrPos.Row + 1) < Board.Height && (arrPos.Column - 1) >= 0 && Board.Pieces(arrPos.Row + 1, arrPos.Column - 1) == null)
                    {
                        possibleTargets[arrPos.Row + 1, arrPos.Column - 1] = true;
                    }
                }
            }
            else // White pawn movement
            {
                // Indicates if there're any targets that results in captures.
                bool withoutCaptures = true;

                // NE (1) (Only captures avaliable)
                if ((arrPos.Row - 1) >= 0 && (arrPos.Column + 1) < Board.Width && Board.Pieces(arrPos.Row - 1, arrPos.Column + 1) != null && Board.Pieces(arrPos.Row - 1, arrPos.Column + 1).Team != Team)
                {
                    if (((arrPos.Row - 2) >= 0 && (arrPos.Column + 2) < Board.Width) && Board.Pieces(arrPos.Row - 2, arrPos.Column + 2) == null)
                    {
                        withoutCaptures = false;
                        possibleTargets[arrPos.Row - 2, arrPos.Column + 2] = true;
                    }
                }

                // SE (2) (Only captures avaliable)
                if ((arrPos.Row + 2) < Board.Height && (arrPos.Column + 2) < Board.Width && Board.Pieces(arrPos.Row + 1, arrPos.Column + 1) != null && Board.Pieces(arrPos.Row + 1, arrPos.Column + 1).Team != Team)
                {
                    if (Board.Pieces(arrPos.Row + 2, arrPos.Column + 2) == null)
                    {
                        withoutCaptures = false;
                        possibleTargets[arrPos.Row + 2, arrPos.Column + 2] = true;
                    }
                }

                // SW (3) (Only captures avaliable)
                if ((arrPos.Row + 2) < Board.Height && (arrPos.Column - 2) >= 0 && Board.Pieces(arrPos.Row + 1, arrPos.Column - 1) != null && Board.Pieces(arrPos.Row + 1, arrPos.Column - 1).Team != Team)
                {
                    if (Board.Pieces(arrPos.Row + 2, arrPos.Column - 2) == null)
                    {
                        withoutCaptures = false;
                        possibleTargets[arrPos.Row + 2, arrPos.Column - 2] = true;
                    }
                }

                // NW (4) (Only captures avaliable)
                if ((arrPos.Row - 1) >= 0 && (arrPos.Column - 1) >= 0 && Board.Pieces(arrPos.Row - 1, arrPos.Column - 1) != null && Board.Pieces(arrPos.Row - 1, arrPos.Column - 1).Team != Team)
                {
                    if (((arrPos.Row - 2) >= 0 && (arrPos.Column - 2) >= 0) && Board.Pieces(arrPos.Row - 2, arrPos.Column - 2) == null)
                    {
                        withoutCaptures = false;
                        possibleTargets[arrPos.Row - 2, arrPos.Column - 2] = true;
                    }
                }

                if (withoutCaptures)
                {
                    // NE (1)
                    if ((arrPos.Row - 1) >= 0 && (arrPos.Column + 1) < Board.Width && Board.Pieces(arrPos.Row - 1, arrPos.Column + 1) == null)
                    {
                        possibleTargets[arrPos.Row - 1, arrPos.Column + 1] = true;
                    }
                    // NW (4)
                    if ((arrPos.Row - 1) >= 0 && (arrPos.Column - 1) >= 0 && Board.Pieces(arrPos.Row - 1, arrPos.Column - 1) == null)
                    {
                        possibleTargets[arrPos.Row - 1, arrPos.Column - 1] = true;
                    }
                }
            }

            return(possibleTargets);
        }
Esempio n. 3
0
        //
        // Summary:
        //     Create an array with the movements actually avaliable to the Dame.
        //
        // Returns:
        //     An array of booleans with the same size of the board where the
        //     positions contain a true value if it is a possible movement;
        //     otherwise it contains false.
        public override bool[,] PossibleTargets()
        {
            // 4-----1
            // -4---1-
            // --8-5--
            // ---D---
            // --7-6--
            // -3---2-
            // 3-----2

            bool[,] possibleTargets = new bool[Board.Height, Board.Width];
            TwoDimensionsArrayPosition arrPos = BoardPosition.ToArrayPosition(Board.Height);

            bool withoutCaptures = true;

            // NE (1) (Only catch avaliable)
            for (int arrRow = arrPos.Row - 1, arrColumn = arrPos.Column + 1; arrRow >= 0 && arrColumn < Board.Width; arrRow--, arrColumn++)
            {
                if (Board.Pieces(arrRow, arrColumn) != null)
                {
                    if (Board.Pieces(arrRow, arrColumn).Team != Team && arrRow - 1 >= 0 && arrColumn + 1 < Board.Width && Board.Pieces(arrRow - 1, arrColumn + 1) == null)
                    {
                        possibleTargets[arrRow - 1, arrColumn + 1] = true;
                        withoutCaptures = false;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            // SE (2) (Only catch avaliable)
            for (int arrRow = arrPos.Row + 1, arrColumn = arrPos.Column + 1; arrRow < Board.Height && arrColumn < Board.Width; arrRow++, arrColumn++)
            {
                if (Board.Pieces(arrRow, arrColumn) != null)
                {
                    if (Board.Pieces(arrRow, arrColumn).Team != Team && arrRow + 1 < Board.Height && arrColumn + 1 < Board.Width && Board.Pieces(arrRow + 1, arrColumn + 1) == null)
                    {
                        possibleTargets[arrRow + 1, arrColumn + 1] = true;
                        withoutCaptures = false;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            // SW (3) (Only catch avaliable)
            for (int arrRow = arrPos.Row + 1, arrColumn = arrPos.Column - 1; arrRow < Board.Height && arrColumn >= 0; arrRow++, arrColumn--)
            {
                if (Board.Pieces(arrRow, arrColumn) != null)
                {
                    if (Board.Pieces(arrRow, arrColumn).Team != Team && arrRow + 1 < Board.Height && arrColumn - 1 >= 0 && Board.Pieces(arrRow + 1, arrColumn - 1) == null)
                    {
                        possibleTargets[arrRow + 1, arrColumn - 1] = true;
                        withoutCaptures = false;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            // NW (4) (Only catch avaliable)
            for (int arrRow = arrPos.Row - 1, arrColumn = arrPos.Column - 1; arrRow >= 0 && arrColumn >= 0; arrRow--, arrColumn--)
            {
                if (Board.Pieces(arrRow, arrColumn) != null)
                {
                    if (Board.Pieces(arrRow, arrColumn).Team != Team && arrRow - 1 >= 0 && arrColumn - 1 >= 0 && Board.Pieces(arrRow - 1, arrColumn - 1) == null)
                    {
                        possibleTargets[arrRow - 1, arrColumn - 1] = true;
                        withoutCaptures = false;
                        break;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            if (withoutCaptures)
            {
                // NE (5) (Without catch case)
                if (arrPos.Row - 1 >= 0 && arrPos.Column + 1 < Board.Width && Board.Pieces(arrPos.Row - 1, arrPos.Column + 1) == null)
                {
                    possibleTargets[arrPos.Row - 1, arrPos.Column + 1] = true;
                }

                // SE (6) (Without catch case)
                if (arrPos.Row + 1 < Board.Height && arrPos.Column + 1 < Board.Width && Board.Pieces(arrPos.Row + 1, arrPos.Column + 1) == null)
                {
                    possibleTargets[arrPos.Row + 1, arrPos.Column + 1] = true;
                }

                // SW (7) (Without catch case)
                if (arrPos.Row + 1 < Board.Height && arrPos.Column - 1 >= 0 && Board.Pieces(arrPos.Row + 1, arrPos.Column - 1) == null)
                {
                    possibleTargets[arrPos.Row + 1, arrPos.Column - 1] = true;
                }

                // NW (8) (Without catch case)
                if (arrPos.Row - 1 >= 0 && arrPos.Column - 1 >= 0 && Board.Pieces(arrPos.Row - 1, arrPos.Column - 1) == null)
                {
                    possibleTargets[arrPos.Row - 1, arrPos.Column - 1] = true;
                }
            }

            return(possibleTargets);
        }