示例#1
0
 private void Model_MoveSuggestionComputed(object sender, MoveEventArgs e)
 {
     if (e.Move.IsSuccess)
     {
         Move.T move = ((Common.Result <Move.T, ErrorDefinitions.Error> .Success)e.Move).Item;
         Board.SetIsSuggestedMove(move.Source);
         Board.SetIsSuggestedMove(move.Target);
     }
     else
     {
         Board.ClearIsSuggestedMove();
     }
 }
示例#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);
            }
        }