Пример #1
0
        public Piece(Player pieceOwner, Board board, PiecePosition startingPosition, string visualId = "X", string pieceName = "Piece", int pieceValue = 10)
        {
            PieceOwner = pieceOwner;

            _board = board;

            PieceValue = pieceValue;

            PieceName = pieceName;

            CurrentPosition = startingPosition;

            VisualID = visualId;

            GenBoardValueTable();
        }
Пример #2
0
        public override IMove Move(PiecePosition movePos)
        {
            if (Math.Abs(movePos.row - CurrentPosition.row) != 1)
            {
                EnPassant = true;
            }
            else
            {
                EnPassant = false;
            }

            var move = PossibleMoves.FirstOrDefault(m => m.GetMovePos().Position == movePos);

            if (move != null)
            {
                move.MakeMove();
            }

            return(move);
        }
Пример #3
0
        public override bool CalculateMoves()
        {
            var possibleMoves = new List <IMove>();

            var pos = this.CurrentPosition;

            pos.row += _direction;

            if (pos.row < _board.RowColLen && _board[pos].OccupyingPiece == null)
            {
                if (pos.row == 0 || pos.row == _board.RowColLen - 1)
                {
                    possibleMoves.Add(new Promotion(_board[pos], this, new Queen(this.PieceOwner, this._board, pos)));
                }
                else
                {
                    possibleMoves.Add(new Move(_board[pos], this));
                }

                if (TimesMoved == 0)
                {
                    pos.row += _direction;

                    if (pos.row < _board.RowColLen && _board[pos].OccupyingPiece == null)
                    {
                        if (pos.row == 0 || pos.row == _board.RowColLen - 1)
                        {
                            possibleMoves.Add(new Promotion(_board[pos], this, new Queen(this.PieceOwner, this._board, pos)));
                        }
                        else
                        {
                            possibleMoves.Add(new Move(_board[pos], this));
                        }
                    }
                }
            }

            //reset
            pos = this.CurrentPosition;

            pos.row += _direction;

            if (pos.row < _board.RowColLen)
            {
                if (pos.col + 1 < _board.RowColLen)
                {
                    var tempPos = new PiecePosition(pos.row, pos.col + 1);

                    var tile = _board[tempPos];

                    tile.ThreateningPieces.Add(this);

                    if (tile.OccupyingPiece != null && tile.OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id)
                    {
                        if (tempPos.row == 0 || tempPos.row == _board.RowColLen - 1)
                        {
                            possibleMoves.Add(new Promotion(tile, this, new Queen(this.PieceOwner, this._board, tempPos)));
                        }
                        else
                        {
                            possibleMoves.Add(new Move(tile, this));
                        }
                    }
                }

                if (pos.col - 1 >= 0)
                {
                    var tempPos = new PiecePosition(pos.row, pos.col - 1);

                    var tile = _board[tempPos];

                    tile.ThreateningPieces.Add(this);

                    if (tile.OccupyingPiece != null && tile.OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id)
                    {
                        if (tempPos.row == 0 || tempPos.row == _board.RowColLen - 1)
                        {
                            possibleMoves.Add(new Promotion(tile, this, new Queen(this.PieceOwner, this._board, tempPos)));
                        }
                        else
                        {
                            possibleMoves.Add(new Move(tile, this));
                        }
                    }
                }
            }

            //reset
            pos = this.CurrentPosition;
            pos.col++;

            if (pos.col < _board.RowColLen && _board[pos].OccupyingPiece != null && _board[pos].OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id && _board[pos].OccupyingPiece.PieceName == "Pawn" && ((Pawn)_board[pos].OccupyingPiece).EnPassant)
            {
                var tile = _board[new PiecePosition(pos.row + _direction, pos.col)];
                var move = new EnPassant(_board[tile.Position], this);
                possibleMoves.Add(move);
            }

            pos.col -= 2;

            if (pos.col >= 0 && _board[pos].OccupyingPiece != null && _board[pos].OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id && _board[pos].OccupyingPiece.PieceName == "Pawn" && ((Pawn)_board[pos].OccupyingPiece).EnPassant)
            {
                var tile = _board[new PiecePosition(pos.row + _direction, pos.col)];
                var move = new EnPassant(tile, this);
                possibleMoves.Add(move);
            }

            PossibleMoves = possibleMoves;

            return(true);
        }
Пример #4
0
 public Pawn(Player pieceOwner, Board board, PiecePosition startingPosition) : base(pieceOwner, board, startingPosition, "♟", "Pawn", 10)
 {
     _direction = pieceOwner.Side == "bottom" ? -1 : 1;
 }
Пример #5
0
 public Rook(Player pieceOwner, Board board, PiecePosition startingPosition) : base(pieceOwner, board, startingPosition, "♜", "Rook", 50)
 {
 }
Пример #6
0
 public Queen(Player pieceOwner, Board board, PiecePosition startingPosition) : base(pieceOwner, board, startingPosition, "♛", "Queen", 90)
 {
 }
Пример #7
0
        public override bool CalculateMoves()
        {
            var possibleMoves = new List <IMove>();

            var castleMoves = new Dictionary <IMove, List <BoardTile> >();

            #region surrounding 8 blocks
            var pos = new PiecePosition(this.CurrentPosition.row + 1, this.CurrentPosition.col);

            if (pos.row < _board.RowColLen && (_board[pos].OccupyingPiece == null || _board[pos].OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id))
            {
                possibleMoves.Add(new Move(_board[pos], this));
            }

            pos = new PiecePosition(this.CurrentPosition.row, this.CurrentPosition.col + 1);

            if (pos.col < _board.RowColLen && (_board[pos].OccupyingPiece == null || _board[pos].OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id))
            {
                possibleMoves.Add(new Move(_board[pos], this));
            }

            pos = new PiecePosition(this.CurrentPosition.row, this.CurrentPosition.col - 1);

            if (pos.col >= 0 && (_board[pos].OccupyingPiece == null || _board[pos].OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id))
            {
                possibleMoves.Add(new Move(_board[pos], this));
            }

            pos = new PiecePosition(this.CurrentPosition.row - 1, this.CurrentPosition.col);

            if (pos.row >= 0 && (_board[pos].OccupyingPiece == null || _board[pos].OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id))
            {
                possibleMoves.Add(new Move(_board[pos], this));
            }

            pos = new PiecePosition(this.CurrentPosition.row + 1, this.CurrentPosition.col + 1);

            if (pos.row < _board.RowColLen && pos.col < _board.RowColLen && (_board[pos].OccupyingPiece == null || _board[pos].OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id))
            {
                possibleMoves.Add(new Move(_board[pos], this));
            }

            pos = new PiecePosition(this.CurrentPosition.row + 1, this.CurrentPosition.col - 1);

            if (pos.row < _board.RowColLen && pos.col >= 0 && (_board[pos].OccupyingPiece == null || _board[pos].OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id))
            {
                possibleMoves.Add(new Move(_board[pos], this));
            }

            pos = new PiecePosition(this.CurrentPosition.row - 1, this.CurrentPosition.col + 1);

            if (pos.row >= 0 && pos.col < _board.RowColLen && (_board[pos].OccupyingPiece == null || _board[pos].OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id))
            {
                possibleMoves.Add(new Move(_board[pos], this));
            }

            pos = new PiecePosition(this.CurrentPosition.row - 1, this.CurrentPosition.col - 1);

            if (pos.row >= 0 && pos.col >= 0 && (_board[pos].OccupyingPiece == null || _board[pos].OccupyingPiece.PieceOwner.Id != this.PieceOwner.Id))
            {
                possibleMoves.Add(new Move(_board[pos], this));
            }
            #endregion

            // hasn't yet moved, so check for castles
            if (this.TimesMoved == 0)
            {
                var rooks = _board.GetPieces(p => p.PieceName == "Rook", p => p.PieceOwner == this.PieceOwner);

                foreach (Rook rook in rooks)
                {
                    if (rook.TimesMoved != 0)
                    {
                        continue;
                    }

                    var isRookOnRight = this.CurrentPosition.col < rook.CurrentPosition.col;

                    var path = GetPath(rook);

                    if (path.Any(t => t.OccupyingPiece != null && t.OccupyingPiece != rook && t.OccupyingPiece != this))
                    {
                        continue;
                    }

                    var to = _board[new PiecePosition(this.CurrentPosition.row, this.CurrentPosition.col + (isRookOnRight ? 2 : -2))];

                    // we only want the path up to where the king will be, we dont want or need to check past that.
                    var trimmedPath = new List <BoardTile>();

                    var i = path.Count - 1;

                    while (i >= 0)
                    {
                        if (path[i] != to)
                        {
                            trimmedPath.Add(path[i]);
                        }
                        else
                        {
                            trimmedPath.Add(path[i]);
                            break;
                        }

                        i--;
                    }

                    var move = new Castle(to, this, rook);

                    castleMoves.Add(move, trimmedPath);
                }
            }

            PossibleMoves = possibleMoves;
            CastleMoves   = castleMoves;

            return(base.CalculateMoves());
        }
Пример #8
0
 public King(Player pieceOwner, Board board, PiecePosition startingPosition) : base(pieceOwner, board, startingPosition, "♚", "King", 900)
 {
 }
Пример #9
0
 public Bishop(Player pieceOwner, Board board, PiecePosition startingPosition) : base(pieceOwner, board, startingPosition, "♝", "Bishop", 30)
 {
 }
Пример #10
0
 public Knight(Player pieceOwner, Board board, PiecePosition startingPosition) : base(pieceOwner, board, startingPosition, "♞", "Knight", 30)
 {
 }