Ejemplo n.º 1
0
        private void MoveCheckerToNewPlace(CheckerElementViewModel currentPositionElementViewModel, CheckerElementViewModel emptyPosition, PlayerViewModel playerViewModel)
        {
            _game.ResetHistoryIfNeeded(CurrentHistoryPosition);
            CurrentHistoryPosition++;

            playerViewModel.MoveCheckerToNewPlace(currentPositionElementViewModel);
            _game.MoveChecker(currentPositionElementViewModel.CheckerModel, emptyPosition.CheckerModel);

            _playerOne.ReSetPossibleMovements(_emptyCellsPlayerViewModel.PlayerPositions.ToList());
            _playerTwo.ReSetPossibleMovements(_emptyCellsPlayerViewModel.PlayerPositions.ToList());
        }
Ejemplo n.º 2
0
 public MoveValidationManager(CheckerElementViewModel oldSelectedElementViewModel
                              , CheckerElementViewModel newSelectedElementViewModel
                              , Side nextMoveSide
                              , PlayerViewModel player
                              , GameViewModel gameViewModel)
 {
     _oldSelectedElementViewModel = oldSelectedElementViewModel;
     _newSelectedElementViewModel = newSelectedElementViewModel;
     _nextMoveSide  = nextMoveSide;
     _player        = player;
     _gameViewModel = gameViewModel;
 }
Ejemplo n.º 3
0
        public MoveValidationResult GetMoveValidationResult()
        {
            IEnumerable <LinkedList <CheckerModel> > availablePathsModels = _player.GetAvailablePaths();
            var availablePaths = new List <LinkedList <CheckerElementViewModel> >();

            foreach (LinkedList <CheckerModel> playerAvailablePath in availablePathsModels)
            {
                var checkersLinkedList = new LinkedList <CheckerElementViewModel>();
                foreach (var checkerModel in playerAvailablePath)
                {
                    CheckerElementViewModel foundCheckerViewModel = _gameViewModel.FindChecker(checkerModel.Column, checkerModel.Row);
                    checkersLinkedList.AddLast(foundCheckerViewModel);
                }
                availablePaths.Add(checkersLinkedList);
            }

            if (availablePaths.Any() && (_oldSelectedElementViewModel == null || _oldSelectedElementViewModel.Side == _newSelectedElementViewModel.Side) && availablePaths.All(x => x.First.Value != _newSelectedElementViewModel))
            {
                return(new MoveValidationResult(MoveValidationStatus.Error, "You have required move by other checker"));
            }

            if (_oldSelectedElementViewModel == null)
            {
                return(new MoveValidationResult(MoveValidationStatus.NewItemSelected));
            }

            if (_oldSelectedElementViewModel == _newSelectedElementViewModel && availablePaths.Any(x => x.Last.Value == _newSelectedElementViewModel))
            {
                return(new MoveValidationResult(MoveValidationStatus.Ok));
            }

            if (_oldSelectedElementViewModel == _newSelectedElementViewModel)
            {
                return(new MoveValidationResult(MoveValidationStatus.DeselectChecker));
            }

            if (_oldSelectedElementViewModel.Side == _newSelectedElementViewModel.Side)
            {
                return(new MoveValidationResult(MoveValidationStatus.NewItemSelected));
            }

            if (_oldSelectedElementViewModel != null && !_oldSelectedElementViewModel.CanMoveToPosition(_newSelectedElementViewModel))
            {
                return(new MoveValidationResult(MoveValidationStatus.Error, "Can't move checker to this place"));
            }

            return(new MoveValidationResult(MoveValidationStatus.Ok));
        }
Ejemplo n.º 4
0
        private void MakeMoveBySecondUser(CancellationToken token)
        {
            KeyValuePair <CheckerModel, CheckerModel> move = _playerTwo.GetOptimalMove(this, token);

            IsCalculatingMove = true;
            if (move.Value != null)
            {
                Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (ThreadStart)(
                                                               () =>
                {
                    CheckerElementViewModel fromChecker = FindChecker(move.Key.Column, move.Key.Row);
                    CheckerElementViewModel toPlace = FindChecker(move.Value.Column, move.Value.Row);
                    _selectedChecker = fromChecker;
                    MoveChecker(toPlace);
                }));
            }
        }
Ejemplo n.º 5
0
        private void MoveChecker(CheckerElementViewModel newSelectedChecker)
        {
            if (_game.IsGameFinished)
            {
                ShowNotificationMessage("Game is over!");
                return;
            }
            var validationManager = new MoveValidationManager(_selectedChecker
                                                              , newSelectedChecker
                                                              , NextMoveSide
                                                              , NextMovePlayer
                                                              , this);

            MoveValidationResult preValidationMoveValidationResult = validationManager.GetPreValidationResult();

            if (preValidationMoveValidationResult.Status == MoveValidationStatus.Error)
            {
                ShowNotificationMessage(preValidationMoveValidationResult.ErrorMessage);
                return;
            }

            if (preValidationMoveValidationResult.Status == MoveValidationStatus.NothingSelected)
            {
                return;
            }

            bool makeMoveStatus = IsCheckerMoved(newSelectedChecker, NextMovePlayer);

            if (makeMoveStatus == false)
            {
                return;
            }

            if (!_game.IsGameFinished)
            {
                WaitMove();
                return;
            }

            WinnerSide = _game.GetWinnerSide();
            RaisePropertyChangedEvent(nameof(CurrentGameStatus));
            RaisePropertyChangedEvent(nameof(IsCheckersMovable));
            ShowNotificationMessage(CurrentGameStatus);
        }
Ejemplo n.º 6
0
        private bool IsCheckerMoved(CheckerElementViewModel newSelectedChecker, PlayerViewModel player)
        {
            var moveValidationManager = new MoveValidationManager(_selectedChecker
                                                                  , newSelectedChecker
                                                                  , NextMoveSide
                                                                  , player
                                                                  , this);
            MoveValidationResult validationResult = moveValidationManager.GetMoveValidationResult();

            if (validationResult.Status == MoveValidationStatus.NewItemSelected)
            {
                if (_selectedChecker != null)
                {
                    _selectedChecker.IsSelected = false;
                    _selectedChecker.DeSelectPossibleMovement();
                }
                _selectedChecker = newSelectedChecker;

                _selectedChecker.IsSelected = true;
                _selectedChecker.SelectPossibleMovement();
                return(false);
            }

            if (validationResult.Status == MoveValidationStatus.DeselectChecker)
            {
                _selectedChecker.IsSelected = false;
                _selectedChecker.DeSelectPossibleMovement();
                _selectedChecker = null;
                return(false);
            }

            if (validationResult.Status == MoveValidationStatus.Error)
            {
                ShowNotificationMessage(validationResult.ErrorMessage);
                return(false);
            }

            MoveCheckerToNewPlace(_selectedChecker, newSelectedChecker, player);
            _selectedChecker.IsSelected = false;
            _selectedChecker            = null;
            return(true);
        }
Ejemplo n.º 7
0
        private void OnDataChanged(List <CheckerModel> added, List <CheckerModel> deleted, List <CheckerModel> modified)
        {
            foreach (CheckerModel checkerModel in added)
            {
                CheckerElementViewModel elementViewModel = new CheckerElementViewModel(checkerModel, new List <CheckerElementViewModel>());
                PlayerPositions.Add(elementViewModel);
            }

            foreach (var checkerModel in deleted)
            {
                CheckerElementViewModel toDelete = PlayerPositions.Single(x => x.Column == checkerModel.Column && x.Row == checkerModel.Row);
                PlayerPositions.Remove(toDelete);
            }

            foreach (CheckerModel modifiedElement in modified)
            {
                CheckerElementViewModel viewModel = PlayerPositions.Single(x => x.Column == modifiedElement.Column && x.Row == modifiedElement.Row);
                if (viewModel.Type != modifiedElement.Type)
                {
                    viewModel.Type = modifiedElement.Type;
                }
            }
        }
Ejemplo n.º 8
0
        public void MoveCheckerToNewPlace(CheckerElementViewModel checker)
        {
            CheckerElementViewModel existingPlayerChecker = PlayerPositions.Single(x => x == checker);

            existingPlayerChecker.DeSelectPossibleMovement();
        }
 public bool CanMoveToPosition(CheckerElementViewModel elementViewModel)
 {
     return(PossibleMovementElements.Contains(elementViewModel));
 }