Exemplo n.º 1
0
        /** This method clears the valid squares of both players and resets the squares for each
         * piece of both players after a round. It also gets the squares the king is unable to move
         * because the king is not allowed to move into check.
         * @author Thomas Hooper
         * @date March 2019
         */
        public void CalculateValidSquares()
        {
            WhitePlayer.ValidSquares.Clear();
            WhitePlayer.AttackSquares.Clear();
            BlackPlayer.ValidSquares.Clear();
            BlackPlayer.AttackSquares.Clear();

            foreach (Piece p in WhitePlayer.Pieces)
            {
                p.SetValidSquares(ChessBoard);
                WhitePlayer.ValidSquares.AddRange(p.ValidSquares);
                WhitePlayer.AttackSquares.AddRange(p.AttackingSquares);
            }
            foreach (Piece p in BlackPlayer.Pieces)
            {
                p.SetValidSquares(ChessBoard);
                BlackPlayer.ValidSquares.AddRange(p.ValidSquares);
                BlackPlayer.AttackSquares.AddRange(p.AttackingSquares);
            }
            King wk = (King)WhitePlayer.Pieces.Find(x => x is King);
            King bk = (King)BlackPlayer.Pieces.Find(x => x is King);

            wk.SetInvalidSquares(BlackPlayer);
            bk.SetInvalidSquares(WhitePlayer);

            if (BlackPlayer.AttackSquares.Exists(x => x.Row == wk.Row && x.Column == wk.Column))
            {
                WhitePlayer.Check = true;
            }
            else
            {
                WhitePlayer.Check = false;
            }
            if (WhitePlayer.AttackSquares.Exists(x => x.Row == bk.Row && x.Column == bk.Column))
            {
                BlackPlayer.Check = true;
            }
            else
            {
                BlackPlayer.Check = false;
            }
            if (ComputerPlayer != null)
            {
                if (ComputerPlayer.Color == Turn)
                {
                    ComputerPlayer.GenerateMoves(HumanPlayer, ChessBoard, PreviousMove);
                }
            }
        }