Exemplo n.º 1
0
        public bool CheckForCheck(Grid grid, int kingId, int kingX, int kingY)
        {
            string color = grid.GetByPieceId(kingId).Piece.Color;

            foreach (var p in grid.Map)
            {
                if (p.Piece != null && p.Piece.Color != color)
                {
                    if (_moveChecker.ValidMove(grid, p.Piece.Id, kingY, kingX))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 2
0
        private List <MoveSet> FindPossibleMoves(GridCell cell)
        {
            List <MoveSet> moveSets = new List <MoveSet>();

            for (int y = 1; y <= 8; y++)
            {
                for (int x = 1; x <= 8; x++)
                {
                    if (_moveChecker.ValidMove(_grid, cell.Piece.Id, y, x))
                    {
                        _grid.MovePiece(cell.Piece.Id, x, y);
                        var piece = _grid.GetByCoords(x, y).Piece;
                        var king  = _grid.GetKingByColor(piece.Color);
                        if (!_checkChecker.CheckForCheck(_grid, king.Piece.Id, king.XCoord, king.YCoord))
                        {
                            MoveSet move = new MoveSet()
                            {
                                XCoord = x,
                                YCoord = y,
                                Piece  = piece,
                                Score  = 16
                            };
                            moveSets.Add(move);
                        }
                        _grid.RevertHistory();
                    }
                }
            }
            return(moveSets);
        }
Exemplo n.º 3
0
        public bool MakeMove(int pieceId, int newX, int newY)
        {
            var currentColorsTurn = WhitesTurn ? "White" : "Black";
            var opponentsColor    = WhitesTurn ? "Black" : "White";

            var cell = Grid.GetByPieceId(pieceId);

            if (cell == null)
            {
                return(false);
            }

            if (cell != null && cell.Piece.Color != currentColorsTurn)
            {
                return(false);
            }

            if (!_moveChecker.ValidMove(Grid, pieceId, newY, newX))
            {
                return(false);
            }

            Grid.MovePiece(pieceId, newX, newY);

            if (CheckForCheck(currentColorsTurn))
            {
                Grid.RevertHistory();
                return(false);
            }

            CheckForCheckMate(opponentsColor);

            WhitesTurn = !WhitesTurn;

            return(true);
        }