Пример #1
0
 private void BoardSquareClick_Executed(object sender, ExecutedRoutedEventArgs e)
 {
     e.Handled = true;
     if (IsActive && (e.OriginalSource is Controls.Square) && (e.Parameter is ViewModel.BoardSquareViewModel))
     {
         Controls.Square sq = (Controls.Square)e.OriginalSource;
         ViewModel.BoardSquareViewModel vm = ((ViewModel.BoardSquareViewModel)e.Parameter);
         if (BoardSquareClicked != null)
         {
             BoardSquareClickedEventArgs arg = new BoardSquareClickedEventArgs(vm);
             BoardSquareClicked(this, arg);
             if (arg.BlinkRed)
             {
                 sq.BlinkRed();
             }
         }
     }
 }
Пример #2
0
        private void board_BoardSquareClicked(object sender, Controls.BoardSquareClickedEventArgs e)
        {
            try
            {
                if (ViewModel.IsGameWaitingForHumanPlayerMove)
                {
                    if (ViewModel.Source != null)
                    {
                        if (e.Square.Coord.Equals(ViewModel.Source) && e.Square.IsSelected)
                        {
                            // clear selection if same source was clicked
                            ViewModel.ClearSelection();
                            return;
                        }
                        // source was selected, try create move from target
                        Result <Move.T, ErrorDefinitions.Error> result =
                            Controller.TryGetValidMove(ViewModel.Source, e.Square.Coord);

                        if (result.IsSuccess)
                        {
                            // user selected valid move
                            if (MainWindowCommands.CancelSuggestMove.CanExecute(null, this))
                            {
                                MainWindowCommands.CancelSuggestMove.Execute(null, this);
                            }

                            Move.T move = ((Result <Move.T, ErrorDefinitions.Error> .Success)result).Item;
                            ModelException.TryThrow <Move.T>(Controller.TrySetSelectedMove(move));
                        }
                        else
                        {
                            // unable to create move with specified target
                            ErrorDefinitions.Error err = ((Result <Move.T, ErrorDefinitions.Error> .Error)result).Item;
                            if (!err.IsNoValidMoveExists)
                            {
                                throw new ModelException(err);
                            }
                            else
                            {
                                // try to use it as new source
                                bool isValidMoveSource = ModelException.TryThrow <bool>(Controller.TryMoveExistsForCoord(e.Square.Coord));
                                if (!isValidMoveSource)
                                {
                                    e.BlinkRed = true;
                                    return;
                                }
                                else
                                {
                                    ViewModel.ClearSelection();
                                }
                            }
                        }
                    }

                    if (ViewModel.Source == null)
                    {
                        // no source exists, select this coord as new source
                        bool isValidMoveSource = ModelException.TryThrow <bool>(Controller.TryMoveExistsForCoord(e.Square.Coord));
                        e.BlinkRed = !isValidMoveSource;

                        if (isValidMoveSource)
                        {
                            ViewModel.SetSource(e.Square.Coord);
                            Controller.GetPossibleTargetCoords(ViewModel.Source).ForEach(ViewModel.SetIsSelected);
                        }
                    }

                    CommandManager.InvalidateRequerySuggested();
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(this, "Nebyl vybrán platný tah." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }