Exemple #1
0
        public void GameStateChanged(BoardState state)
        {
            SquareView squareView = null;

            ResetBoardColor();

            switch (state)
            {
            case BoardState.Normal:
                if (_lastChangedSquareView != null)
                {
                    ResetSquareViewColor(_lastChangedSquareView);
                }
                break;

            case BoardState.WhiteCheck:
                squareView =
                    SquareViews.First(
                        x => (x.Square?.Piece?.Type == Type.King) && (x.Square?.Piece?.Color == Color.White));
                squareView.SetResourceReference(BackgroundProperty, "ValidationBrush5");
                break;

            case BoardState.BlackCheck:
                squareView =
                    SquareViews.First(
                        x => (x.Square?.Piece?.Type == Type.King) && (x.Square?.Piece?.Color == Color.Black));
                squareView.SetResourceReference(BackgroundProperty, "ValidationBrush5");
                break;

            case BoardState.BlackCheckMate:
                squareView =
                    SquareViews.First(
                        x => (x.Square?.Piece?.Type == Type.King) && (x.Square?.Piece?.Color == Color.Black));
                squareView.SetResourceReference(BackgroundProperty, "TextBrush");
                break;

            case BoardState.WhiteCheckMate:
                squareView =
                    SquareViews.First(
                        x => (x.Square?.Piece?.Type == Type.King) && (x.Square?.Piece?.Color == Color.White));
                squareView.SetResourceReference(BackgroundProperty, "TextBrush");
                break;

            case BoardState.BlackPat:
                squareView =
                    SquareViews.First(
                        x => (x.Square?.Piece?.Type == Type.King) && (x.Square?.Piece?.Color == Color.Black));
                squareView.SetResourceReference(BackgroundProperty, "WhiteColorBrush");
                break;

            case BoardState.WhitePat:
                squareView =
                    SquareViews.First(
                        x => (x.Square?.Piece?.Type == Type.King) && (x.Square?.Piece?.Color == Color.White));
                squareView.SetResourceReference(BackgroundProperty, "WhiteColorBrush");
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(state), state, null);
            }
            _lastChangedSquareView = squareView;
        }
Exemple #2
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);
                    }
                }
            };
        }
        public void GameStateChanged(BoardState state)
        {
            SquareView squareView = null;

            ResetBoardColor();

            switch (state)
            {
            case BoardState.Normal:
                if (_lastChangedSquareView != null)
                {
                    ResetSquareViewColor(_lastChangedSquareView);
                }
                break;

            case BoardState.WhiteCheck:
                squareView =
                    SquareViews.First(
                        x => x.Square?.Piece?.Type == Type.King && x.Square?.Piece?.Color == Color.White);
                squareView.SetResourceReference(BackgroundProperty, "MahApps.Brushes.Validation5");
                break;

            case BoardState.BlackCheck:
                squareView =
                    SquareViews.First(
                        x => x.Square?.Piece?.Type == Type.King && x.Square?.Piece?.Color == Color.Black);
                squareView.SetResourceReference(BackgroundProperty, "MahApps.Brushes.Validation5");
                break;

            case BoardState.BlackCheckMate:
                squareView =
                    SquareViews.First(
                        x => x.Square?.Piece?.Type == Type.King && x.Square?.Piece?.Color == Color.Black);
                squareView.SetResourceReference(BackgroundProperty, "MahApps.Brushes.Text");
                break;

            case BoardState.WhiteCheckMate:
                squareView =
                    SquareViews.First(
                        x => x.Square?.Piece?.Type == Type.King && x.Square?.Piece?.Color == Color.White);
                squareView.SetResourceReference(BackgroundProperty, "MahApps.Brushes.Text");
                break;

            case BoardState.BlackPat:
                squareView =
                    SquareViews.First(
                        x => x.Square?.Piece?.Type == Type.King && x.Square?.Piece?.Color == Color.Black);
                squareView.SetResourceReference(BackgroundProperty, "MahApps.Brushes.SystemControlForegroundChromeWhite");
                break;

            case BoardState.WhitePat:
                squareView =
                    SquareViews.First(
                        x => x.Square?.Piece?.Type == Type.King && x.Square?.Piece?.Color == Color.White);
                squareView.SetResourceReference(BackgroundProperty, "MahApps.Brushes.SystemControlForegroundChromeWhite");
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(state), state, null);
            }
            _lastChangedSquareView = squareView;
        }