Esempio n. 1
0
 public Controller()
 {
     _board = new ChessBoard();
     _startLoc = new Location();
     _endLoc = new Location();
     _turn = 1;
     _selectedSquare = null;
     _promotion = 0;
 }
Esempio n. 2
0
        /// <summary>
        /// Checks if the movements of the piece is a valid movement.
        /// 
        /// If it is Valid, it moves the piece to the desired location.
        /// It checks for castleing, and En Passant.
        /// Updates a label where everything describing the movement in readable english.
        /// Changes the image from the old location to the new location.
        /// Removes the highlight for the selected piece.
        /// Removes the highlighted squares that show where the piece can move.
        /// Deselects the selected piece.
        /// Checks for pawn promotion.
        /// Checks if the for checkmate if the selected piece not a king and changes turn.
        /// </summary>
        /// <param name="endSquare">A square on the board that the piece will move too.</param>
        public void MovePiece(UniformGrid board, string endSquare)
        {
            _endLoc.X = GrabRow(endSquare[1]);
            _endLoc.Y = GrabColumn(endSquare[0]);

            RemovePieceMovement(board, _startLoc.X, _startLoc.Y);

            if (Board.Squares[_startLoc.X, _startLoc.Y].Piece.CheckMovement(Board.Squares, _startLoc.X, _startLoc.Y, _endLoc.X, _endLoc.Y) == true)
            {
                if (inCheck == false)//*****************************************************
                {
                    if (Board.Squares[_startLoc.X, _startLoc.Y].Piece.GetType() != typeof(King))
                    {
                        bool isValid = WillKingCheck(_startLoc.X, _startLoc.Y, _endLoc.X, _endLoc.Y);
                        if (isValid == false)
                        {
                            Board.Squares[_startLoc.X, _startLoc.Y].Piece.MovePiece(Board.Squares, _startLoc.X, _startLoc.Y, _endLoc.X, _endLoc.Y);
                            CheckKingSideCastle();
                            l.Content = ("The piece at " + _startSquare + " moved to " + endSquare + ".");
                            Image img = (Image)((Square)board.Children[(_startLoc.X * 8) + _startLoc.Y]).Pic;
                            ((Square)board.Children[(_startLoc.X * 8) + _startLoc.Y]).Panel.Children.Clear();
                            ((Square)board.Children[(_startLoc.X * 8) + _startLoc.Y]).Pic = null;
                            ((Square)board.Children[(_endLoc.X * 8) + _endLoc.Y]).Panel.Children.Clear();
                            ((Square)board.Children[(_endLoc.X * 8) + _endLoc.Y]).Panel.Children.Add(img);
                            ((Square)board.Children[(_endLoc.X * 8) + _endLoc.Y]).Pic = img;
                            SetDefaultBoardSquareColors(_selectedSquare, _selectedSquare.loc.X, _selectedSquare.loc.Y);
                            _selectedSquare = null;
                            CheckEnPassant(Board.Squares, _endLoc.X, _endLoc.Y);
                            CheckPawnPromotion();
                            if (Board.Squares[_startLoc.X, _startLoc.Y].Piece.GetType() != typeof(King))
                            {
                                ChangeTurn();
                                IsInCheckMate(Turn);
                            }
                            else
                            {
                                ChangeTurn();
                            }
                            MovablePieces();
                        }
                        else
                        {
                            PrintPieceMovement(board, _startLoc.X, _startLoc.Y);
                            MessageBox.Show("Don't put the king check!!!");
                        }
                    }
                    else
                    {
                        bool isValid = KingInCheck(_endLoc.X, _endLoc.Y);
                        if(isValid == true)
                        {
                            Board.Squares[_startLoc.X, _startLoc.Y].Piece.MovePiece(Board.Squares, _startLoc.X, _startLoc.Y, _endLoc.X, _endLoc.Y);
                            CheckKingSideCastle();
                            l.Content = ("The piece at " + _startSquare + " moved to " + endSquare + ".");
                            Image img = (Image)((Square)board.Children[(_startLoc.X * 8) + _startLoc.Y]).Pic;
                            ((Square)board.Children[(_startLoc.X * 8) + _startLoc.Y]).Panel.Children.Clear();
                            ((Square)board.Children[(_startLoc.X * 8) + _startLoc.Y]).Pic = null;
                            ((Square)board.Children[(_endLoc.X * 8) + _endLoc.Y]).Panel.Children.Clear();
                            ((Square)board.Children[(_endLoc.X * 8) + _endLoc.Y]).Panel.Children.Add(img);
                            ((Square)board.Children[(_endLoc.X * 8) + _endLoc.Y]).Pic = img;
                            SetDefaultBoardSquareColors(_selectedSquare, _selectedSquare.loc.X, _selectedSquare.loc.Y);
                            _selectedSquare = null;
                            if (Board.Squares[_startLoc.X, _startLoc.Y].Piece.GetType() != typeof(King))
                            {
                                ChangeTurn();
                                IsInCheckMate(Turn);
                            }
                            else
                            {
                                ChangeTurn();
                            }
                            MovablePieces();
                        }
                        else
                        {
                            PrintPieceMovement(board, _startLoc.X, _startLoc.Y);
                            MessageBox.Show("Don't go back in check!!!");
                        }
                    }
                }
                else//*****************************************************
                {
                    if (WillSaveTheKing(_startLoc.X, _startLoc.Y, _endLoc.X, _endLoc.Y) == true)
                    {
                        if (Board.Squares[_startLoc.X, _startLoc.Y].Piece.GetType() != typeof(King))
                        {
                            Board.Squares[_startLoc.X, _startLoc.Y].Piece.MovePiece(Board.Squares, _startLoc.X, _startLoc.Y, _endLoc.X, _endLoc.Y);
                            l.Content = ("The piece at " + _startSquare + " moved to " + endSquare + ".");
                            Image img = (Image)((Square)board.Children[(_startLoc.X * 8) + _startLoc.Y]).Pic;
                            ((Square)board.Children[(_startLoc.X * 8) + _startLoc.Y]).Panel.Children.Clear();
                            ((Square)board.Children[(_startLoc.X * 8) + _startLoc.Y]).Pic = null;
                            ((Square)board.Children[(_endLoc.X * 8) + _endLoc.Y]).Panel.Children.Clear();
                            ((Square)board.Children[(_endLoc.X * 8) + _endLoc.Y]).Panel.Children.Add(img);
                            ((Square)board.Children[(_endLoc.X * 8) + _endLoc.Y]).Pic = img;
                            SetDefaultBoardSquareColors(_selectedSquare, _selectedSquare.loc.X, _selectedSquare.loc.Y);
                            _selectedSquare = null;
                            CheckEnPassant(Board.Squares, _endLoc.X, _endLoc.Y);
                            CheckPawnPromotion();
                            if (Board.Squares[_startLoc.X, _startLoc.Y].Piece.GetType() != typeof(King))
                            {
                                ChangeTurn();
                                IsInCheckMate(Turn);
                            }
                            else
                            {
                                ChangeTurn();
                            }
                            MovablePieces();
                        }
                        else
                        {
                            bool isValid = KingInCheck(_endLoc.X, _endLoc.Y);
                            if (isValid == true)
                            {
                                Board.Squares[_startLoc.X, _startLoc.Y].Piece.MovePiece(Board.Squares, _startLoc.X, _startLoc.Y, _endLoc.X, _endLoc.Y);
                                CheckKingSideCastle();
                                l.Content = ("The piece at " + _startSquare + " moved to " + endSquare + ".");
                                Image img = (Image)((Square)board.Children[(_startLoc.X * 8) + _startLoc.Y]).Pic;
                                ((Square)board.Children[(_startLoc.X * 8) + _startLoc.Y]).Panel.Children.Clear();
                                ((Square)board.Children[(_startLoc.X * 8) + _startLoc.Y]).Pic = null;
                                ((Square)board.Children[(_endLoc.X * 8) + _endLoc.Y]).Panel.Children.Clear();
                                ((Square)board.Children[(_endLoc.X * 8) + _endLoc.Y]).Panel.Children.Add(img);
                                ((Square)board.Children[(_endLoc.X * 8) + _endLoc.Y]).Pic = img;
                                SetDefaultBoardSquareColors(_selectedSquare, _selectedSquare.loc.X, _selectedSquare.loc.Y);
                                _selectedSquare = null;
                                if (Board.Squares[_startLoc.X, _startLoc.Y].Piece.GetType() != typeof(King))
                                {
                                    ChangeTurn();
                                    IsInCheckMate(Turn);
                                }
                                else
                                {
                                    ChangeTurn();
                                }
                                MovablePieces();
                            }
                            else
                            {
                                PrintPieceMovement(board, _startLoc.X, _startLoc.Y);
                                MessageBox.Show("Don't go back in check!!!");
                            }
                        }
                    }
                    else
                    {
                        CanSaveKing(_startLoc.X, _startLoc.Y);
                        MessageBox.Show("Save your king!!!");
                        MovablePieces();
                    }
                }
            }
            else
            {
                PrintPieceMovement(board, _startLoc.X, _startLoc.Y);
                MessageBox.Show("Invalid piece movement, please try again...");
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Adds handlers to the buttons, while also adding the buttons to the board.
 /// </summary>
 /// <param name="board">A grid representing the board.</param>
 /// <param name="movement">A label that will display the movemnt for every moved piece.</param>
 /// <param name="playerTurns">A label that will display who's turn it is.</param>
 public void CreateBoard(UniformGrid board, Label movement, Label playerTurns)
 {
     g = board;
     l = movement;
     l2 = playerTurns;
     for (int x = 0; x < 8; ++x)
     {
         for (int y = 0; y < 8; ++y)
         {
             Square s = new Square(Board.Squares[x, y].Piece.ToString());
             SetDefaultBoardSquareColors(s, x, y);
             s.loc.X = x;
             s.loc.Y = y;
             s.Click += Button_PieceHandler;
             s.MouseRightButtonDown += Button_RightClickHandler;
             g.Children.Add(s);
         }
     }
 }
Esempio n. 4
0
 private void Button_RightClickHandler(object sender, RoutedEventArgs e)
 {
     Square s = (Square)sender;
     if (_selectedSquare != null)
     {
         SetDefaultBoardSquareColors(_selectedSquare, _selectedSquare.loc.X, _selectedSquare.loc.Y);
         RemovePieceMovement((UniformGrid)_selectedSquare.Parent, _selectedSquare.loc.X, _selectedSquare.loc.Y);
         MovablePieces();
         _selectedSquare = null;
     }
 }
Esempio n. 5
0
        private void Button_PieceHandler(object sender, RoutedEventArgs e)
        {
            Square s = (Square)sender;

            if (_selectedSquare == null)
            {
                if (Board.Squares[s.loc.X, s.loc.Y].Piece.GetType() != typeof(Space))
                {
                     if ((int)Board.Squares[s.loc.X, s.loc.Y].Piece.Color == Turn)
                     {
                         string[] input = GrabPiece(s.loc.X, s.loc.Y);
                         s.Background = Brushes.CornflowerBlue;
                         ProcessPiece((UniformGrid)s.Parent, (input[0] + input[1]), @"([a-h])([1-8])");
                         _selectedSquare = s;
                     }
                     else
                     {
                         MessageBox.Show("Please select a piece with the correct color!!!");
                     }
                }
            }
            else
            {
                string[] input = GrabPiece(s.loc.X, s.loc.Y);
                ProcessMove((UniformGrid)s.Parent, (input[0] + input[1]), @"([a-h])([1-8])");
                ResetEnPassant();
            }
        }
Esempio n. 6
0
 public void SetDefaultBoardSquareColors(Square s, int x, int y)
 {
     if(inCheck == false)
     {
         if (Board.Squares[x, y].Color == ChessColor.DARK)
         {
             s.Background = Brushes.Gray;
         }
         else if (Board.Squares[x, y].Color == ChessColor.LIGHT)
         {
             s.Background = Brushes.LightGray;
         }
     }
     else
     {
         for(int z = 0; z < a.Count; ++z)
         {
             if (a[z][0] == x && a[z][1] == y)
             {
                 s.Background = Brushes.Plum;
             }
             else if (Board.Squares[x, y].Color == ChessColor.DARK)
             {
                 s.Background = Brushes.Gray;
             }
             else if (Board.Squares[x, y].Color == ChessColor.LIGHT)
             {
                 s.Background = Brushes.LightGray;
             }
         }
     }
 }
Esempio n. 7
0
 public void ResetGame()
 {
     _board = new ChessBoard();
     g.Children.Clear();
     inCheck = false;
     CreateBoard(g, l, l2);
     _startLoc = new Location();
     _endLoc = new Location();
     ChangeTurn();
     _startSquare = null;
     _selectedSquare = null;
 }