Пример #1
0
        /*
         * Updates the GUI to reflect the board passed in (this should be board1 throughout the program)
         */
        public void UpdateBoard(Board b)
        {
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    System.Drawing.Point p = new System.Drawing.Point(i, j);

                    // change this to GetPlayerAt when implemented
                    if (b.GetPieceAt(p).player.GetID() == 1)
                    {
                        Type t = b.GetPieceAt(p).GetType();
                        blocks[i][j].Text = whitePieces[t];
                    }
                    else if (b.GetPieceAt(p).player.GetID() == 2)
                    {
                        Type t = b.GetPieceAt(p).GetType();
                        blocks[i][j].Text = blackPieces[t];
                    }
                    else if (b.GetPieceAt(p).player.GetID() == 0)
                    {
                        blocks[i][j].Text = "  ";
                    }
                }
            }

            Thing.Text = (string)App.Current.Properties["ActivePlayer"];
        }
Пример #2
0
        /*
         * Returns possible points the king could move to on the specified board
         */
        public override List <Point> getPossibleEndSpaces(Board b)
        {
            List <Point> endSpaces = new List <Point>();

            Point upLeft    = new Point(position.X - 1, position.Y - 1);
            Point up        = new Point(position.X - 1, position.Y);
            Point upRight   = new Point(position.X - 1, position.Y + 1);
            Point right     = new Point(position.X, position.Y + 1);
            Point downRight = new Point(position.X + 1, position.Y + 1);
            Point down      = new Point(position.X + 1, position.Y);
            Point downLeft  = new Point(position.X + 1, position.Y - 1);
            Point left      = new Point(position.X, position.Y - 1);

            List <Point> possibles = new List <Point>();

            possibles.Add(upLeft);
            possibles.Add(up);
            possibles.Add(upRight);
            possibles.Add(right);
            possibles.Add(downRight);
            possibles.Add(down);
            possibles.Add(downLeft);
            possibles.Add(left);

            for (int i = 0; i < possibles.Count(); i++)
            {
                if (b.pointExists(possibles[i]) && b.GetPieceAt(possibles[i]).player.GetID() != player.GetID())
                {
                    endSpaces.Add(possibles[i]);
                }
            }

            return(endSpaces);
        }
Пример #3
0
        /*
         * Constructor for the second board in the symmetrical observer pattern. Either board
         * notifies the other if it is changed.
         */
        public Board(Board b)
        {
            observer   = b;
            b.observer = this;

            board = new Piece[8, 8];

            //generate flipped board (opponent's perspective)
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    Piece tempPiece = b.GetPieceAt(new Point(i, j));

                    board[7 - i, 7 - j] = tempPiece;

                    //set piece positions for their respective boards
                    if (tempPiece.player.GetID() != 2)
                    {
                        tempPiece.SetPosition(new Point(i, j));
                    }
                    else
                    {
                        tempPiece.SetPosition(new Point(7 - i, 7 - j));
                    }
                }
            }
        }
Пример #4
0
        /*
         * Returns a list of black pieces on the specified board
         */
        List <Piece> GetBlackPieces(Board b)
        {
            List <Piece> pieces = new List <Piece>();

            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    Point tempPoint = new Point(i, j);
                    if (b.GetPieceAt(tempPoint).player.GetID() == 2)
                    {
                        pieces.Add(b.GetPieceAt(tempPoint));
                    }
                }
            }

            return(pieces);
        }
Пример #5
0
        /*
         * Returns a list of points the pawn can move to
         */
        public override List <Point> getPossibleEndSpaces(Board board)
        {
            Point oneForward = new Point(position.X - 1, position.Y);
            Point twoForward = new Point(position.X - 2, position.Y);
            Point takeRight  = new Point(position.X - 1, position.Y + 1);
            Point takeLeft   = new Point(position.X - 1, position.Y - 1);

            List <Point> endSpaces = new List <Point>();

            //checks forward bounds
            if (position.X != 0)
            {
                //the space in front of it
                if (board.GetPieceAt(oneForward).player.GetID() == 0)
                {
                    endSpaces.Add(oneForward);

                    //if it hasn't moved yet
                    if (position.X == 6 && board.GetPieceAt(twoForward).player.GetID() == 0)
                    {
                        endSpaces.Add(twoForward);
                    }
                }

                //taking a piece (we will not allow 'en pesante')
                //checks right and left bounds
                if (position.Y != 7 && board.GetPieceAt(takeRight).player.GetID() != 0 && board.GetPieceAt(takeRight).player.GetID() != player.GetID())
                {
                    endSpaces.Add(takeRight);
                }

                if (position.Y != 0 && board.GetPieceAt(takeLeft).player.GetID() != 0 && board.GetPieceAt(takeLeft).player.GetID() != player.GetID())
                {
                    endSpaces.Add(takeLeft);
                }
            }

            return(endSpaces);
        }
Пример #6
0
        /*
         * Handles any click on the GUI
         * Triggers movePiece if on highlighted textfield
         */
        private void a8_MouseDown(object sender, MouseButtonEventArgs e)
        {
            TextBlock s   = (TextBlock)sender;
            Viewbox   v   = (System.Windows.Controls.Viewbox)s.Parent;
            Border    b   = (Border)v.Parent;
            int       row = Grid.GetRow(b);

            // will have to subtract 1 from column
            int col = Grid.GetColumn(b) - 1;

            // get piece from board at this position
            Piece p = board1.GetPieceAt(new System.Drawing.Point(row, col));

            if (previousPiece == null || previousPiece is EmptyPiece)
            {
                HighlightSpots(p);
                previousPiece = p;
            }
            else
            {
                MovePiece(p);
            }
        }
Пример #7
0
        /*
         * Returns a list of points that the queen could move to
         */
        public override List <Point> getPossibleEndSpaces(Board b)
        {
            List <Point> endSpaces = new List <Point>();

            //downLeft
            bool  hitSomething    = false;
            Point currentPosition = new Point(position.X, position.Y);

            while (b.pointExists(new Point(currentPosition.X + 1, currentPosition.Y - 1)) && hitSomething == false)
            {
                currentPosition.X += 1;
                currentPosition.Y -= 1;

                if (b.GetPieceAt(currentPosition).player.GetID() == 0)
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                }
                else if (b.GetPieceAt(currentPosition).player.GetID() != player.GetID())
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                    hitSomething = true;
                }
                else
                {
                    hitSomething = true;
                }
            }

            //downRight
            hitSomething    = false;
            currentPosition = new Point(position.X, position.Y);
            while (b.pointExists(new Point(currentPosition.X + 1, currentPosition.Y + 1)) && hitSomething == false)
            {
                currentPosition.X += 1;
                currentPosition.Y += 1;

                if (b.GetPieceAt(currentPosition).player.GetID() == 0)
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                }
                else if (b.GetPieceAt(currentPosition).player.GetID() != player.GetID())
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                    hitSomething = true;
                }
                else
                {
                    hitSomething = true;
                }
            }

            //upLeft
            hitSomething    = false;
            currentPosition = new Point(position.X, position.Y);
            while (b.pointExists(new Point(currentPosition.X - 1, currentPosition.Y - 1)) && hitSomething == false)
            {
                currentPosition.X -= 1;
                currentPosition.Y -= 1;

                if (b.GetPieceAt(currentPosition).player.GetID() == 0)
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                }
                else if (b.GetPieceAt(currentPosition).player.GetID() != player.GetID())
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                    hitSomething = true;
                }
                else
                {
                    hitSomething = true;
                }
            }

            //upRight
            hitSomething    = false;
            currentPosition = new Point(position.X, position.Y);
            while (b.pointExists(new Point(currentPosition.X - 1, currentPosition.Y + 1)) && hitSomething == false)
            {
                currentPosition.X -= 1;
                currentPosition.Y += 1;

                if (b.GetPieceAt(currentPosition).player.GetID() == 0)
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                }
                else if (b.GetPieceAt(currentPosition).player.GetID() != player.GetID())
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                    hitSomething = true;
                }
                else
                {
                    hitSomething = true;
                }
            }

            hitSomething    = false;
            currentPosition = new Point(position.X, position.Y);

            //up
            while (b.pointExists(new Point(currentPosition.X - 1, currentPosition.Y)) && hitSomething == false)
            {
                currentPosition.X -= 1;
                if (b.GetPieceAt(currentPosition).player.GetID() == 0)
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                }
                else if (b.GetPieceAt(currentPosition).player.GetID() != player.GetID())
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                    hitSomething = true;
                }
                else
                {
                    hitSomething = true;
                }
            }

            hitSomething    = false;
            currentPosition = new Point(position.X, position.Y);
            //right
            while (b.pointExists(new Point(currentPosition.X, currentPosition.Y + 1)) && hitSomething == false)
            {
                currentPosition.Y += 1;
                if (b.GetPieceAt(currentPosition).player.GetID() == 0)
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                }
                else if (b.GetPieceAt(currentPosition).player.GetID() != player.GetID())
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                    hitSomething = true;
                }
                else
                {
                    hitSomething = true;
                }
            }

            hitSomething    = false;
            currentPosition = new Point(position.X, position.Y);
            //down
            while (b.pointExists(new Point(currentPosition.X + 1, currentPosition.Y)) && hitSomething == false)
            {
                currentPosition.X += 1;
                if (b.GetPieceAt(currentPosition).player.GetID() == 0)
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                }
                else if (b.GetPieceAt(currentPosition).player.GetID() != player.GetID())
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                    hitSomething = true;
                }
                else
                {
                    hitSomething = true;
                }
            }

            hitSomething    = false;
            currentPosition = new Point(position.X, position.Y);
            //left
            while (b.pointExists(new Point(currentPosition.X, currentPosition.Y - 1)) && hitSomething == false)
            {
                currentPosition.Y -= 1;
                if (b.GetPieceAt(currentPosition).player.GetID() == 0)
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                }
                else if (b.GetPieceAt(currentPosition).player.GetID() != player.GetID())
                {
                    endSpaces.Add(new Point(currentPosition.X, currentPosition.Y));
                    hitSomething = true;
                }
                else
                {
                    hitSomething = true;
                }
            }

            return(endSpaces);
        }