Пример #1
0
        private bool TryLoadGame(bool showSuccess)
        {
            string oldFileName = ViewModel.FileName, oldFileTitle = ViewModel.FileTitle;

            try
            {
                if (!GetFileNameToViewModel(false, false))
                {
                    return(false);
                }

                ModelException.TryThrow <GameController.T>(Controller.TryLoadGame(ViewModel.FileName));

                if (showSuccess)
                {
                    MessageBox.Show(this, "Hra byla načtena.", "Informace", MessageBoxButton.OK, MessageBoxImage.Information);
                }

                return(true);
            }
            catch (Exception exc)
            {
                ViewModel.SetFileName(oldFileName, oldFileTitle);
                MessageBox.Show(this, ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }
        }
Пример #2
0
 public MainWindowViewModel()
 {
     if (Model == null)
     {
         Model = ModelException.TryThrow <GameModel.T>(GameModel.tryCreate());
     }
     InitModel();
 }
Пример #3
0
        public ReplayWindow(Latrunculi.Controller.ReplayController.T controller, ReplayWindowViewModel viewModel)
        {
            DataContext = viewModel;
            Controller  = controller;

            InitializeComponent();

            ViewModel.Model.PositionChanged += Model_PositionChanged;

            Dispatcher.BeginInvoke(new Action(() =>
            {
                ModelException.TryThrow <ReplayController.T>(Controller.tryGoToPosition(0));
            }), System.Windows.Threading.DispatcherPriority.Background);
        }
Пример #4
0
 private void Redo_Executed(object sender, ExecutedRoutedEventArgs e)
 {
     if (ViewModel.Position < ViewModel.NumberOfMoves)
     {
         try
         {
             ModelException.TryThrow <ReplayController.T>(Controller.tryIncPosition());
         }
         catch (Exception exc)
         {
             ViewModel.Info = string.Format("Chyba: {0}", ViewModelCommon.ConvertExceptionToShortString(exc));
         }
     }
 }
Пример #5
0
 private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs <double> e)
 {
     e.Handled = true;
     try
     {
         if (!ignoreChange)
         {
             ModelException.TryThrow <ReplayController.T>(Controller.tryGoToPosition((int)e.NewValue));
         }
     }
     catch (Exception exc)
     {
         ViewModel.Info = string.Format("Chyba: {0}", ViewModelCommon.ConvertExceptionToShortString(exc));
     }
 }
Пример #6
0
 private void Resume_Executed(object sender, ExecutedRoutedEventArgs e)
 {
     try
     {
         if (ViewModel.IsPaused || ViewModel.IsCreated)
         {
             ModelException.TryThrow <ReplayController.T>(Controller.tryResume());
             CommandManager.InvalidateRequerySuggested();
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show(this, "Nelze spustit replay kvůli chybě." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
Пример #7
0
 private void Redo_Executed(object sender, ExecutedRoutedEventArgs e)
 {
     e.Handled = true;
     try
     {
         if (!ViewModel.IsGameCreated && ViewModel.IsRedoStackNotEmpty)
         {
             ModelException.TryThrow <GameController.T>(Controller.TryRedo());
             CommandManager.InvalidateRequerySuggested();
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show(this, "Tah nelze opakovat." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
Пример #8
0
 private void CancelSuggestMove_Executed(object sender, ExecutedRoutedEventArgs e)
 {
     e.Handled = true;
     try
     {
         if (ViewModel.IsMoveSuggestionComputing)
         {
             ModelException.TryThrow <GameController.T>(Controller.TryCancelSuggestMove());
             CommandManager.InvalidateRequerySuggested();
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show(this, "Nepodařilo se zrušit nápovědu tahu." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
Пример #9
0
 private void Pause_Executed(object sender, ExecutedRoutedEventArgs e)
 {
     e.Handled = true;
     try
     {
         if (ViewModel.IsGameRunning)
         {
             ModelException.TryThrow <GameController.T>(Controller.TryPause());
             CommandManager.InvalidateRequerySuggested();
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show(this, "Nelze pozastavit hru kvůli chybě." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
Пример #10
0
 private void SuggestMove_Executed(object sender, ExecutedRoutedEventArgs e)
 {
     e.Handled = true;
     try
     {
         if (ViewModel.IsGameWaitingForHumanPlayerMove && !ViewModel.IsMoveSuggestionComputing)
         {
             ViewModel.ClearBoardIndications();
             ModelException.TryThrow <GameController.T>(Controller.TrySuggestMove());
             CommandManager.InvalidateRequerySuggested();
         }
     }
     catch (Exception exc)
     {
         MessageBox.Show(this, "Nepodařilo se napovědět tah." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
     }
 }
Пример #11
0
        private void NewGame()
        {
            PlayerSettingsViewModel vm = TryShowSettings();

            if (vm != null)
            {
                Controller.changePlayerSettings(
                    new Tuple <Model.Player.Types, Model.Player.Types>(PlayerViewModel.PlayerTypeToModel(vm.WhitePlayer), PlayerViewModel.PlayerTypeToModel(vm.BlackPlayer)),
                    new Tuple <string, string>(vm.WhitePlayer.Name, vm.BlackPlayer.Name),
                    new Tuple <Model.Player.Levels, Model.Player.Levels>(PlayerViewModel.PlayerLevelToModel(vm.WhitePlayer), PlayerViewModel.PlayerLevelToModel(vm.BlackPlayer)));

                ModelException.TryThrow <GameController.T>(Controller.TryNewGame());
                ModelException.TryThrow <GameController.T>(Controller.TryRun());
                FocusBoard();
                CommandManager.InvalidateRequerySuggested();
            }
        }
Пример #12
0
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            try
            {
                GameModel.T         model      = ModelException.TryThrow <GameModel.T>(GameModel.tryCreate());
                GameController.T    controller = GameController.create(model);
                MainWindowViewModel viewModel  = new MainWindowViewModel(model);

                MainWindow win = new GUI.MainWindow(viewModel, controller);
                MainWindow = win;
                win.Show();
            }
            catch (Exception exc)
            {
                MessageBox.Show(string.Format("Aplikaci Latrunculi se nepodařilo spustit:{0}{1}", Environment.NewLine, ViewModelCommon.ConvertExceptionToString(exc)),
                                "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
                Shutdown(-1);
            }
        }
Пример #13
0
        private void History_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            e.Handled = true;
            try
            {
                if (!ignoreChange)
                {
                    MoveHistoryItem item = e.AddedItems.OfType <MoveHistoryItem>().FirstOrDefault();

                    if (item != null)
                    {
                        ModelException.TryThrow <GameController.T>(Controller.TryGoToMove(item.ID));
                    }
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(this, "Nelze přejít na tah z historie." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Пример #14
0
        private void Replay_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            e.Handled = true;
            try
            {
                if (ViewModel.IsGameFinished)
                {
                    ReplayModel.T         replayModel = ModelException.TryThrow <ReplayModel.T>(ReplayModel.tryCreate(ViewModel.Model.Board, ViewModel.Model.PlayerSettings));
                    ReplayController.T    controller  = ModelException.TryThrow <ReplayController.T>(ReplayController.tryCreate(replayModel));
                    ReplayWindowViewModel vm          = new ReplayWindowViewModel(replayModel);

                    ReplayWindow win = new ReplayWindow(controller, vm);
                    win.Owner = this;
                    win.ShowDialog();
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show(this, "Okno přehrávače nelze otevřít." + Environment.NewLine + ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
Пример #15
0
        private bool TrySaveGame(bool showSuccess, bool isSaveAsDialog)
        {
            try
            {
                if (!GetFileNameToViewModel(true, isSaveAsDialog))
                {
                    return(false);
                }

                ModelException.TryThrow <GameController.T>(Controller.TrySaveGame(ViewModel.FileName));

                if (showSuccess)
                {
                    MessageBox.Show(this, "Hra byla uložena.", "Informace", MessageBoxButton.OK, MessageBoxImage.Information);
                }

                return(true);
            }
            catch (Exception exc)
            {
                MessageBox.Show(this, ViewModelCommon.ConvertExceptionToShortString(exc), "Chyba", MessageBoxButton.OK, MessageBoxImage.Error);
                return(false);
            }
        }
Пример #16
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);
            }
        }