Пример #1
0
        public void MovePiece(CellId source, CellId target)
        {
            if (!IsValidMove(source, target))
            {
                throw new InvalidOperationException("Invalid move!");
            }
            // Figure out if it is a castle operation.
            //
            var sourceCell = _chessboard.GetCell(source);
            var targetCell = _chessboard.GetCell(target);

            // We know it is a valid move. We just have to identify if it is a castle
            // operation.
            //
            if (sourceCell.PieceType == EChessPieceType.King &&
                targetCell.PieceType == EChessPieceType.Rook &&
                sourceCell.PieceColor == targetCell.PieceColor)
            {
                _chessboard.SwapPiece(source, target);
            }
            else
            {
                _chessboard.MovePiece(source, target);
            }
            nextTurn();
        }
        public bool IsValidMove(CellId source, CellId target, IChessBoard board)
        {
            var sourceCell = board.GetCell(source);
            var targetCell = board.GetCell(target);

            return(IsValidMove(sourceCell, targetCell, board));
        }
        public IReadOnlyList <CellId> ValidMovesForCell(CellId cellId, IChessBoard board)
        {
            var validMoveList = new List <CellId>();

            var source = board.GetCell(cellId);

            for (int row = 0; row < board.NumRows; row++)
            {
                for (int col = 0; col < board.NumCols; col++)
                {
                    // Figure out the status of the cell.
                    //
                    var target = board.GetCell(CellId.Create(row, col));
                    if (IsValidMove(source, target, board))
                    {
                        validMoveList.Add(CellId.Create(row, col));
                    }
                }
            }
            return(validMoveList);
        }
Пример #4
0
        public void MovePiece(CellId source, CellId target)
        {
            if (!IsValidMove(source, target))
            {
                throw new InvalidOperationException("Invalid move!");
            }
            // Figure out if it is a castle operation.
            //
            var sourceCell = _chessboard.GetCell(source);
            var targetCell = _chessboard.GetCell(target);

            // We know it is a valid move. We just have to identify if it is a castle
            // operation.
            //
            if (sourceCell.PieceType == EChessPieceType.King &&
                targetCell.PieceType == EChessPieceType.Rook &&
                sourceCell.PieceColor == targetCell.PieceColor)
            {
                _chessboard.SwapPiece(source, target);
            }
            else
            {
                if (targetCell.PieceColor != sourceCell.PieceColor) // The player captured a piece
                {
                    PieceCaptured(this, new PieceCapturedEventArgs(targetCell.PieceType, targetCell.PieceColor));
                    // Check if the king was captured.
                    //
                    if (targetCell.PieceType == EChessPieceType.King)
                    {
                        GameFinished(this, new GameEndedEventArgs(ActivePlayer));
                    }
                }
                _chessboard.MovePiece(source, target);
            }
            nextTurn();
        }