Ejemplo n.º 1
0
        public override bool ScanForPiece(ChessBoard gb, Player player, int destX, int destY)
        {
            // ScanForPiece for Bishop checks diagonal lines for pieces //

            destX -= 1;
            destY -= 1;

            // If moving diagonally right and down
            if (destX > this.X && destY > this.Y)
            {
                // Iterate through the squares diagonally to find a piece.
                for (int i = (this.Y + 1), j = (this.X + 1); (i <= destY || j <= destX); ++i, ++j)
                {
                    if (gb.NodeArray[i, j].CurrentPiece is Piece)
                    {
                        return base.ScanForPiece(gb, player, destX, destY);
                    }
                }
            }
            // If moving left and up //
            else if (destX < this.X && destY < this.Y)
            {
                for (int i = (this.Y - 1), j = (this.X - 1); (i >= destY || j >= destX); --i, --j)
                {
                    if (gb.NodeArray[i, j].CurrentPiece is Piece)
                    {
                        return base.ScanForPiece(gb, player, destX, destY);
                    }
                }
            }
            // If moving right and up //
            else if (destX > this.X && destY < this.Y)
            {
                for (int i = (this.Y - 1), j = (this.X + 1); (i >= destY || j <= destX); --i, ++j)
                {
                    if (gb.NodeArray[i, j].CurrentPiece is Piece)
                        return base.ScanForPiece(gb, player, destX, destY);
                }
            }
            // If moving left and down //
            else if (destX < this.X && destY > this.Y)
            {
                for (int i = (this.Y + 1), j = (this.X - 1); (i <= destY || j >= destX); ++i, --j)
                {
                    if (gb.NodeArray[i, j].CurrentPiece is Piece)
                        return base.ScanForPiece(gb, player, destX, destY);
                }
            }

            return false;
        }
Ejemplo n.º 2
0
 public override bool Move(ChessBoard gb, Player player, int dX, int dY)
 {
     if (!ScanForPiece(gb, player, dX, dY))
     {
         // Check to see if the destination coords are diagonal movements //
         if ((dX - (this.X + 1) == dY - (this.Y + 1) ||
              (this.X + 1) - dX == dY - (this.Y + 1)))
         {
             return base.Move(gb, player, dX, dY);
         }
         else
             return false;
     }
     else
         return false;
 }
Ejemplo n.º 3
0
        public void Initialize()
        {
            /* INITIALIZE SETS UP PLAYER COLOURS AND PLAYER NAMES.
             * INITIALIZE ALSO SETS UP THE COLOUR SCHEME AS PER USER
             * REQUEST */

            playerOne = new Player("white");
            playerTwo = new Player("black");

            Console.Write("\n\n\n\n\n\n\n\n\n\n\n\n\n\n                            ");
            Console.Write("Select a colour theme:\n");
            Console.Write("                            [1] - Default\n");
            Console.Write("                            [2] - Christmas!\n");
            Console.Write("                            -> ");
            int colourChoice = int.Parse(Console.ReadLine());

            Console.Clear();
            Console.Write("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n                 ");
            Console.Write("Player 1 (white/red) enter a name -> ");
            playerOne.GetName();
            Console.Clear();
            Console.Write("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n                 ");
            Console.Write("Player 2 (black/green) enter a name -> ");
            playerTwo.GetName();

            switch (colourChoice)
            {
                case 1:
                    gb.Initialize("default");
                    break;

                case 2:
                    gb.Initialize("christmas");
                    break;

                default:
                    gb.Initialize("default");
                    break;
            }
        }
Ejemplo n.º 4
0
        public virtual bool Move(ChessBoard gb, Player player, int dX, int dY)
        {
            // MOVE PROCESSES THE MOVEMENTS OF PIECES. RETURNS TRUE FOR SUCCESSFUL OPERATIONS //
            // AND FALSE FOR UNSUCCESSFUL ONES //

            if (dX > 0)
                dX -= 1;

            if (dY > 0)
                dY -= 1;

            // create node objects to hold the current pieces from both sets of coordinates //
            Node piece = gb.NodeArray[this.Y, this.X].CurrentPiece;
            Node dest = gb.NodeArray[dY, dX].CurrentPiece;

            // swap the pieces //
            gb.NodeArray[dY, dX].CurrentPiece = piece;
            gb.NodeArray[this.Y, this.X].CurrentPiece = dest;

            // set the nodes x and y position to the new values //
            this.X = dX;
            this.Y = dY;

            return true;
        }
Ejemplo n.º 5
0
        public virtual bool ScanForPiece(ChessBoard gb, Player player, int destX, int destY)
        {
            /* This base version of ScanForPiece() is the last called. It checks to see if
             * the piece encountered is a piece of the other colour. If it is, it takes the
             * piece. Returns true for a failed operation, and false for a successful one */

            // If the current object in use is a Pawn, a separate branch of logic is needed //
            if (this is Pawn)
            {
                // If the pawn is trying to move forward, it cannot do anything with the piece //
                // ahead of it. //
                if (destX == this.X)
                {
                    if (gb.NodeArray[destY, destX].CurrentPiece is Piece)
                    {
                        // So return //
                        return true;
                    }
                }

                // Otherwise, if the pawn is trying to move diagonally, it can only do this //
                // if it can take the piece at the destination. //
                if (destX > this.X || destX < this.X)
                {
                    if (gb.NodeArray[destY, destX].CurrentPiece is Piece)
                    {
                        // If the piece at the destination coordinates is a white piece, and the //
                        // player colour is green (black), allow. //
                        if (gb.WhitePieces.Contains<Piece>(gb.NodeArray[destY, destX].CurrentPiece as Piece)
                            && player.Colour == "Green")
                        {
                            // Ensure the pawn is moving in the right direction according to piece //
                            // colour orientation //
                            if (destY < this.Y)
                            {
                                // Take the piece //
                                Take(gb, gb.NodeArray[destY, destX].CurrentPiece as Piece, 2);
                                // Replace it with a new EmptyNode
                                gb.NodeArray[destY, destX].CurrentPiece = new EmptyNode();
                                return false;
                            }
                            else
                                return true;
                        }
                        // If the piece at the destination coords is a black piece and the player //
                        // colour is red (white), allow. //
                        else if (gb.BlackPieces.Contains<Piece>(gb.NodeArray[destY, destX].CurrentPiece as Piece)
                                && player.Colour == "Red")
                        {
                            if (destY > this.Y)
                            {
                                Take(gb, gb.NodeArray[destY, destX].CurrentPiece as Piece, 1);
                                gb.NodeArray[destY, destX].CurrentPiece = new EmptyNode();
                                return false;
                            }
                            else
                                return true;
                        }
                    }
                    else
                        return true;
                }
                else
                    return false;
            }

            // Do the same thing but without the pawn rule check: //
            if (gb.WhitePieces.Contains<Piece>(gb.NodeArray[destY, destX].CurrentPiece as Piece)
                && player.Colour == "Green")
            {
                Take(gb, gb.NodeArray[destY, destX].CurrentPiece as Piece, 2);
                gb.NodeArray[destY, destX].CurrentPiece = new EmptyNode();
                return false;
            }
            else if (gb.BlackPieces.Contains<Piece>(gb.NodeArray[destY, destX].CurrentPiece as Piece)
                && player.Colour == "Red")
            {
                Take(gb, gb.NodeArray[destY, destX].CurrentPiece as Piece, 1);
                gb.NodeArray[destY, destX].CurrentPiece = new EmptyNode();
                return false;
            }

            return true;
        }
Ejemplo n.º 6
0
        public override bool Move(ChessBoard gb, Player player, int dX, int dY)
        {
            // This portion of move just passes control to the base and returns true //
            base.Move(gb, player, dX, dY);

            return true;
        }
Ejemplo n.º 7
0
        public override bool ScanForPiece(ChessBoard gb, Player player, int destX, int destY)
        {
            // Overidden version of ScanForPiece //
            destX -= 1;
            destY -= 1;

            // If the pawn is trying to move diagonally, call the base ScanForPiece to check //
            // if it is a piece that can be taken. Base ScanForPiece ultimately returns true //
            // unless otherwise stated //
            if (destX > this.X || destX < this.X)
            {
                return base.ScanForPiece(gb, player, destX, destY);
            }

            // If moving forward and there is a piece in the way, return the base //
            // ScanForPiece, which will return true and not allow a move //
            if (gb.NodeArray[destY, this.X].CurrentPiece is Piece)
            {
                return base.ScanForPiece(gb, player, destX, destY);
            }
            else
                return false;
        }
Ejemplo n.º 8
0
        public override bool Move(ChessBoard gb, Player player, int dX, int dY)
        {
            // Move is overidden for the rules of the Pawn piece //

            // Guard against improper move. If the pawn is trying to move diagonally //
            // and there is no piece, return false //
            if ((dX > (this.X + 1) || dX < (this.X + 1))
                  && !(gb.NodeArray[dY - 1, dX - 1].CurrentPiece is Piece))
                return false;

            // If ScanForPiece returns false, no piece is in the way and moves can happen //
            if (!ScanForPiece(gb, player, dX, dY))
            {
                // Depending on the colour of the piece being moved, different operations //
                // are needed //
                if (IsWhite(gb))
                {
                    // If the pawn is on the first move, it's allowed to move 2 squares at a time. //
                    if (firstMove)
                    {
                        if (dY - (this.Y + 1) > 0 && dY - (this.Y + 1) <= 2)
                        {
                            firstMove = false;
                            return base.Move(gb, player, dX, dY);
                        }
                        else
                            return false;
                    }
                    else
                    {
                        // Otherwise the pawn is only allowed to move one //
                        if (dY - (this.Y + 1) == 1)
                            return base.Move(gb, player, dX, dY);
                        else
                            return false;
                    }
                }
                else
                {
                    if (firstMove)
                    {
                        if ((this.Y + 1) - dY > 0 && (this.Y + 1) - dY <= 2)
                        {
                            firstMove = false;
                            return base.Move(gb, player, dX, dY);
                        }
                        else
                            return false;
                    }
                    else
                    {
                        if ((this.Y + 1) - dY == 1)
                            return base.Move(gb, player, dX, dY);
                        else
                            return false;
                    }
                }
            }
            else
                return false;
        }
Ejemplo n.º 9
0
        public override bool ScanForPiece(ChessBoard gb, Player player, int destX, int destY)
        {
            // Override for Horse is easy. If the destination coords contains a piece //
            // pass control to the base method and determine if it can be taken //
            destX -= 1;
            destY -= 1;

            if (gb.NodeArray[destY, destX].CurrentPiece is Piece)
                return base.ScanForPiece(gb, player, destX, destY);
            else
                return false;
        }
Ejemplo n.º 10
0
        public override bool Move(ChessBoard gb, Player player, int dX, int dY)
        {
            if (!ScanForPiece(gb, player, dX, dY))
            {
                // Check for valid move coordinates for horse. 2 down, one across //
                if ((dY - (this.Y + 1) == 2 || (this.Y + 1) - dY == 2) &&
                    (dX - (this.X + 1) == 1 || (this.X + 1) - dX == 1))
                {
                    return base.Move(gb, player, dX, dY);
                }
                // Check for 2 across, one down //
                else if ((dX - (this.X + 1) == 2 || (this.X + 1) - dX == 2) &&
                         (dY - (this.Y + 1) == 1 || (this.Y + 1) - dY == 1))
                {
                    return base.Move(gb, player, dX, dY);
                }

                return false;
            }
            else
                return false;
        }
Ejemplo n.º 11
0
        public override bool ScanForPiece(ChessBoard gb, Player player, int destX, int destY)
        {
            // Override for King checks destination square for a piece and passes control //
            // to the base method to determine if it can be taken //
            destX -= 1;
            destY -= 1;

            if (gb.NodeArray[destY, destX].CurrentPiece is Piece)
                return base.ScanForPiece(gb, player, destX, destY);
            else
                return false;
        }
Ejemplo n.º 12
0
 public override bool Move(ChessBoard gb, Player player, int dX, int dY)
 {
     if (!Check(gb, dX, dY))
     {
         if (!ScanForPiece(gb, player, dX, dY))
         {
             // Check for legal moves. One square in either direction //
             if ((dX - (this.X + 1) <= 1 && dX - (this.X + 1) > 0) ||
                     ((this.X + 1) - dX <= 1 && (this.X + 1) - dX > 0))
                 return base.Move(gb, player, dX, dY);
             else if ((dY - (this.Y + 1) <= 1 && dY - (this.Y + 1) > 0) ||
                         ((this.Y + 1) - dY <= 1 && (this.Y + 1) - dY > 0))
                 return base.Move(gb, player, dX, dY);
             else
                 return false;
         }
         else
             return false;
     }
     else
         return false;
 }
Ejemplo n.º 13
0
        public override bool ScanForPiece(ChessBoard gb, Player player, int destX, int destY)
        {
            // Rook ScanForPiece checks all vertical and horizontal movements along the destination //
            // axis' //

            destX -= 1;
            destY -= 1;

            // If destX is the same as the current x position, the rook is moving vertically //
            if (destX == this.X)
            {
                if ((this.Y + 1) == 8)
                {
                    // Check vertical up
                    for (int i = this.Y - 1; i >= destY; --i)
                    {
                        if (gb.NodeArray[i, this.X].CurrentPiece is Piece)
                        {
                            return base.ScanForPiece(gb, player, destX, destY);
                        }
                        else
                            continue; // If a piece hasn't been found, move to next iteration //
                    }
                }
                else
                {
                    // Check vertical down
                    for (int i = (this.Y + 1); i <= destY; ++i)
                    {
                        if (gb.NodeArray[i, this.X].CurrentPiece is Piece)
                        {
                            return base.ScanForPiece(gb, player, destX, destY);
                        }
                        else
                            continue;
                    }
                }
            }
            else if (destY == this.Y)
            {
                if ((this.X + 1) == 8)
                {
                    // Check horizontal left
                    for (int i = (this.X - 1); i >= destX; --i)
                    {
                        if (gb.NodeArray[this.Y, i].CurrentPiece is Piece)
                        {
                            return base.ScanForPiece(gb, player, destX, destY);
                        }
                        else
                            continue;
                    }
                }
                else
                {
                    // Check horizontal right
                    for (int i = (this.X + 1); i <= destX; ++i)
                    {
                        if (gb.NodeArray[this.Y, i].CurrentPiece is Piece)
                        {
                            return base.ScanForPiece(gb, player, destX, destY);
                        }
                        else
                            continue;
                    }
                }
            }

            return false;
        }
Ejemplo n.º 14
0
 public override bool Move(ChessBoard gb, Player player, int dX, int dY)
 {
     // If ScanForPiece returns false //
     if (!ScanForPiece(gb, player, dX, dY))
     {
         // Allow a move //
         if (dX == (this.X + 1) && dY > 0)
             return base.Move(gb, player, dX, dY);
         else if (dX > 0 && dY == (this.Y + 1))
             return base.Move(gb, player, dX, dY);
         else
             return false;
     }
     else
         return false;
 }
Ejemplo n.º 15
0
        public override bool ScanForPiece(ChessBoard gb, Player player, int destX, int destY)
        {
            destX -= 1;
            destY -= 1;

            // If moving vertically //
            if (destX == (this.X))
            {
                // If moving down //
                if (destY > this.Y)
                {
                    // Check squares along path //
                    for (int i = (this.Y + 1); i <= destY; ++i)
                    {
                        if (gb.NodeArray[i, this.X].CurrentPiece is Piece)
                            return base.ScanForPiece(gb, player, destX, destY);
                    }
                }
                // If moving up //
                else if (destY < (this.Y))
                {
                    for (int i = (this.Y - 1); i >= destY; --i)
                    {
                        if (gb.NodeArray[i, this.X].CurrentPiece is Piece)
                            return base.ScanForPiece(gb, player, destX, destY);
                    }
                }
            }
            // If moving horizontally //
            else if (destY == this.Y)
            {
                // If moving right //
                if (destX > this.X)
                {
                    // Check squares along path //
                    for (int i = (this.X + 1); i <= destX; ++i)
                    {
                        if (gb.NodeArray[this.Y, i].CurrentPiece is Piece)
                            return base.ScanForPiece(gb, player, destX, destY);
                    }
                }
                // If moving left //
                else if (destX < this.X)
                {
                    for (int i = (this.X - 1); i >= destX; --i)
                    {
                        if (gb.NodeArray[this.Y, i].CurrentPiece is Piece)
                            return base.ScanForPiece(gb, player, destX, destY);
                    }
                }
            }
            // If moving right and down //
            else if (destX > this.X && destY > this.Y)
            {
                for (int i = (this.Y + 1), j = (this.X + 1); (i <= destY || j <= destX); ++i, ++j)
                {
                    if (gb.NodeArray[i, j].CurrentPiece is Piece)
                    {
                        return base.ScanForPiece(gb, player, destX, destY);
                    }
                }
            }
            // If moving left and up //
            else if (destX < this.X && destY < this.Y)
            {
                for (int i = (this.Y - 1), j = (this.X - 1); (i >= destY || j >= destX); --i, --j)
                {
                    if (gb.NodeArray[i, j].CurrentPiece is Piece ||
                        gb.NodeArray[destY, destX].CurrentPiece is Piece)
                    {
                        return base.ScanForPiece(gb, player, destX, destY);
                    }
                }
            }
            // If moving right and up //
            else if (destX > this.X && destY < this.Y)
            {
                for (int i = (this.Y - 1), j = (this.X + 1); (i >= destY || j <= destX); --i, ++j)
                {
                    if (gb.NodeArray[i, j].CurrentPiece is Piece)
                        return base.ScanForPiece(gb, player, destX, destY);
                }
            }
            // If moving left and down //
            else if (destX < this.X && destY > this.Y)
            {
                for (int i = (this.Y + 1), j = (this.X - 1); (i <= destY || j >= destX); ++i, --j)
                {
                    if (gb.NodeArray[i, j].CurrentPiece is Piece)
                        return base.ScanForPiece(gb, player, destX, destY);
                }
            }

            return false;
        }
Ejemplo n.º 16
0
        public override bool Move(ChessBoard gb, Player player, int dX, int dY)
        {
            if (!ScanForPiece(gb, player, dX, dY))
            {
                // Queen can move freely throughout the board unless obstructed by a peice //

                // Check for valid moves //
                if ((dX - (this.X + 1) == dY - (this.Y + 1)) ||
                     ((this.X + 1) - dX == (this.Y + 1) - dY))
                    return base.Move(gb, player, dX, dY);
                else if ((dX - (this.X + 1) > 0 || dY - (this.Y + 1) > 0) ||
                          ((this.X + 1) - dX > 0 || (this.Y + 1) - dY > 0))
                    return base.Move(gb, player, dX, dY);
                else
                    return false;
            }
            else
                return false;
        }