private PromoteCommand(PromoteCommand promoteCommand, Board board)
 {
     Move         = promoteCommand.Move;
     _board       = board;
     _moveCommand = promoteCommand._moveCommand.Copy(board);
     _oldPawn     = board.PieceAt(Move.StartCoordinate);
 }
        private CastlingCommand(CastlingCommand command, Board board)
        {
            Move = command.Move;

            _rookCommand = command._rookCommand.Copy(board);
            _kingCommand = command._kingCommand.Copy(board);
        }
        public HistoryView(GameView gameView)
        {
            InitializeComponent();
            _game = gameView.Game;

            _gameView      = gameView;
            _realBoardView = gameView.UcBoardView.Content as BoardView;
            _conversation  = new HistoryViewConversation();

            foreach (ICompensableCommand command in _game.Container.Moves)
            {
                ICompensableCommand momand = command.Copy(_board);
                _conversation.Execute(momand);
                _moves.Add(momand);
            }

            _game.Container.Moves.CollectionChanged += (sender, args) =>
            {
                if (args.Action == NotifyCollectionChangedAction.Add)
                {
                    ICompensableCommand command = args.NewItems[args.NewItems.Count - 1] as ICompensableCommand;
                    command = command.Copy(_board);
                    _conversation.Execute(command);
                    _moves.Add(command);
                }
                if (args.Action == NotifyCollectionChangedAction.Remove)
                {
                    _moves.RemoveAt(_moves.Count - 1);
                    _conversation.Undo();
                }
            };

            _boardView = new BoardView(new Container(_board, _moves));
            ListViewHistory.ItemsSource = _moves;
        }
示例#4
0
        /// <summary>
        ///     Undo the last command that has been done
        /// </summary>
        /// <returns>True if anything has been done</returns>
        public Move Undo()
        {
            ICompensableCommand command = _conversation.Undo();

            if (command == null)
            {
                return(null);
            }

            if (_container.HalfMoveSinceLastCapture != 0)
            {
                _container.HalfMoveSinceLastCapture--;
            }
            else
            {
                int count = 0;
                for (int i = _moves.Count - 1; i > 0; i--)
                {
                    if (!_moves[i].TakePiece)
                    {
                        count++;
                    }
                    else
                    {
                        break;
                    }
                }
                _container.HalfMoveSinceLastCapture = count;
            }

            _moves.Remove(command);
            return(command.Move);
        }
        public PromoteCommand(Move move, Board board)
        {
            if (move?.PromotePieceType == null)
            {
                throw new NullReferenceException("Can't build a promote command with null Move.PromotedPieceType");
            }
            _board = board;
            Move   = move;

            _moveCommand = new MoveCommand(move, board);
            _oldPawn     = board.PieceAt(move.StartCoordinate);
        }
        /// <summary>
        ///     Redo the last command that has been undone
        /// </summary>
        /// <returns>The last command, null if there is none</returns>
        public ICompensableCommand Redo()
        {
            if (_redoCommands.Count == 0)
            {
                return(null);
            }

            ICompensableCommand command = _redoCommands.Pop();

            command.Execute();
            _undoCommands.Push(command);

            return(command);
        }
        public CastlingCommand(Move move, Board board)
        {
            Move = move;

            var isLeftCastling = move.TargetCoordinate.X < move.StartCoordinate.X;

            _kingCommand =
                new MoveCommand(
                    new Move(board.PieceAt(move.StartCoordinate),
                             board.Squares[isLeftCastling ? 2 : 6, move.StartCoordinate.Y]), board);
            _rookCommand =
                new MoveCommand(
                    new Move(board.PieceAt(new Coordinate(isLeftCastling ? 0 : 7, move.StartCoordinate.Y)),
                             board.Squares[isLeftCastling ? 3 : 5, move.TargetCoordinate.Y]), board);
        }
示例#8
0
        public EnPassantCommand(Move move, Board board)
        {
            Move = move;

            var isWhite = move.PieceColor == Color.White;
            var isLeft  = move.StartCoordinate.X > move.TargetCoordinate.X;

            var x = move.StartCoordinate.X + (isLeft ? -1 : 1);
            var y = move.StartCoordinate.Y;

            var startSquare  = board.SquareAt(move.StartCoordinate);
            var secondSquare = board.Squares[x, y];
            var thirdSquare  = board.Squares[x, y + (isWhite ? -1 : 1)];

            _firstMove  = new MoveCommand(new Move(startSquare, secondSquare, Move.PieceType, Move.PieceColor), board);
            _secondMove = new MoveCommand(new Move(secondSquare, thirdSquare, Move.PieceType, Move.PieceColor), board);
        }
示例#9
0
        /// <summary>
        ///     Redo the last command that has been undone
        /// </summary>
        /// <returns>True if anything has been done</returns>
        public Move Redo()
        {
            ICompensableCommand command = _conversation.Redo();

            if (command == null)
            {
                return(null);
            }

            //Number of moves since last capture
            if (!command.TakePiece)
            {
                _container.HalfMoveSinceLastCapture++;
            }
            else
            {
                _container.HalfMoveSinceLastCapture = 0;
            }

            _moves.Add(command);
            return(command.Move);
        }
示例#10
0
        public bool IsMoveValid(Move move, Board board)
        {
            IState checkState = new CheckState();
            Board  tempBoard  = new Board(board);

            bool castling = new CastlingRule().IsMoveValid(move, board) && (move.PieceType == Type.King) &&
                            (tempBoard.PieceAt(move.TargetCoordinate)?.Type == Type.Rook) &&
                            (move.PieceColor == tempBoard.PieceAt(move.TargetCoordinate)?.Color);

            if (!castling)
            {
                if (move.PieceColor == tempBoard.PieceAt(move.TargetCoordinate)?.Color)
                {
                    return(true);
                }
            }
            ICompensableCommand command = castling
                ? new CastlingCommand(move, tempBoard)
                : new MoveCommand(move, tempBoard) as ICompensableCommand;

            command.Execute();

            return(!checkState.IsInState(tempBoard, move.PieceColor));
        }
示例#11
0
 private EnPassantCommand(EnPassantCommand command, Board board)
 {
     Move        = command.Move;
     _firstMove  = command._firstMove.Copy(board);
     _secondMove = command._secondMove.Copy(board);
 }
示例#12
0
        public BoardView(Container container)
        {
            InitializeComponent();
            Board      = container.Board;
            _container = container;

            for (int i = 0; i < Board.Size; i++)
            {
                Grid.RowDefinitions.Add(new RowDefinition());
                Grid.ColumnDefinitions.Add(new ColumnDefinition());
            }

            Grid.RowDefinitions.Add(new RowDefinition {
                Height = GridLength.Auto
            });
            Grid.ColumnDefinitions.Add(new ColumnDefinition {
                Width = GridLength.Auto
            });
            /* Ajout des cases */
            foreach (var square in Board.Squares)
            {
                var squareView = new SquareView(square)
                {
                    UcPieceView     = { LayoutTransform = LayoutTransform },
                    LayoutTransform = LayoutTransform
                };
                SquareViews.Add(squareView);
                Grid.Children.Add(squareView); //Position is set in the squareview constructor
            }
            /* Ajout des cases */
            /*  Numéro de case */
            for (int i = 0; i < Board.Size; i++)
            {
                Label label = new Label
                {
                    Content             = (char)('A' + i),
                    HorizontalAlignment = HorizontalAlignment.Center
                };
                Grid.SetColumn(label, i);
                Grid.SetRow(label, 8);
                Grid.Children.Add(label);
            }

            for (int i = Board.Size; i > 0; i--)
            {
                Label label = new Label
                {
                    Content           = Board.Size - i + 1,
                    VerticalAlignment = VerticalAlignment.Center
                };
                Grid.SetColumn(label, 8);
                Grid.SetRow(label, i - 1);
                Grid.Children.Add(label);
            }
            /* Numéro de case */
            /* Affichage du dernier coup */
            _container.Moves.CollectionChanged += (sender, args) =>
            {
                if (args.Action == NotifyCollectionChangedAction.Add ||
                    args.Action == NotifyCollectionChangedAction.Remove)
                {
                    if (_container.Moves.Count != 0)
                    {
                        Console.WriteLine("Changed color");
                        _lastMove.ForEach(ResetSquareViewColor);
                        _lastMove.Clear();

                        ICompensableCommand command      = _container.Moves.Last();
                        SquareView          startSquare  = SquareAt(command.Move.StartCoordinate);
                        SquareView          targetSquare = SquareAt(command.Move.TargetCoordinate);

                        _lastMove.Add(targetSquare);

                        _lastMove.Add(startSquare);
                    }
                }
            };
        }
示例#13
0
 /// <summary>
 ///     Executes a command
 /// </summary>
 /// <param name="command">The command to execute</param>
 public void Execute(ICompensableCommand command)
 {
     command.Execute();
     _undoCommands.Push(command);
     _redoCommands.Clear();
 }