public void MoveChecker(CheckerModel fromPlace, CheckerModel toPlace, bool addToHistory = true) { CheckerModel foundChecker = NextMovePlayer.PlayerPositions.SingleOrDefault(x => x.Column == fromPlace.Column && x.Row == fromPlace.Row); CheckerModel toPosition = EmptyCellsAsPlayer.PlayerPositions.SingleOrDefault(x => x.Column == toPlace.Column && x.Row == toPlace.Row); if (toPlace.Side == fromPlace.Side) { toPosition = NextMovePlayer.PlayerPositions.SingleOrDefault(x => x.Column == toPlace.Column && x.Row == toPlace.Row); } int currentCol = foundChecker.Column; int currentRow = foundChecker.Row; int nextCol = toPosition.Column; int nextRow = toPosition.Row; HistoryMove historyMove = NextMovePlayer.MoveCheckerToNewPlace(currentCol, currentRow, nextCol, nextRow); ReCalculateNeighborsAndPaths(); bool isGameEnded = GetIsGameEnded(); if (!isGameEnded) { ChangeTurn(); } if (addToHistory) { _actionsHistory.Push(historyMove); } }
private void RevertMove(HistoryMove historyMove) { RevertMove(historyMove.MovedFromTo.Value, historyMove.MovedFromTo.Key, historyMove.IsConvertedToQueen); foreach (KeyValuePair <CheckerModel, CheckerModel> deletedInfoPair in historyMove.DeletedList) { CheckerModel resurrectedChecker = deletedInfoPair.Key; Player playerToAddChecker = GetPlayerByCheckerSide(resurrectedChecker.Side); playerToAddChecker.AddNewChecker(resurrectedChecker, deletedInfoPair.Key.Column, deletedInfoPair.Key.Row); } ReCalculateNeighborsAndPaths(); bool isFinished = GetIsGameEnded(); if (!isFinished) { ChangeTurn(); } }
public HistoryMove MoveCheckerToNewPlace(int currentCol , int currentRow , int nextCol , int nextRow , bool convertBackToChecker = false) { CheckerModel checker = PlayerPositions.Single(x => x.Column == currentCol && x.Row == currentRow); IEnumerable <LinkedList <CheckerModel> > availablePaths = CalculateAvailablePaths(); LinkedList <CheckerModel> path = availablePaths.Where(x => x.Last.Value.Column == nextCol && x.Last.Value.Row == nextRow).OrderByDescending(x => x.Count).FirstOrDefault(); bool isConvertedToQueen = false; if (ShouldConvertToQueenByPathDuringTaking(path)) { checker.BecomeAQueen(); isConvertedToQueen = true; } CheckerModel newPosition = _dataProvider.GetElementAtPosition(nextCol, nextRow); CheckerModel oldPositionedChecker = _dataProvider.GetElementAtPosition(currentCol, currentRow); if (convertBackToChecker) { oldPositionedChecker.DowngradeToChecker(); } if (_pathCalculator.IsMoveToucheBoard(newPosition)) { oldPositionedChecker.BecomeAQueen(); isConvertedToQueen = true; } _dataProvider.StartTrackChanges(); List <CheckerModel> itemsToTake = GetToTakeCheckers(availablePaths, nextCol, nextRow, checker); var historyMove = new HistoryMove(isConvertedToQueen); foreach (CheckerModel checkerElement in itemsToTake) { var element = new CheckerModel(checkerElement.Column, checkerElement.Row, PieceType.Checker, Side.Empty); historyMove.DeletedList.Add(new KeyValuePair <CheckerModel, CheckerModel>(checkerElement.Clone(), element.Clone())); _dataProvider.MoveCheckerToNewPosition(element, checkerElement.Column, checkerElement.Row); } historyMove.MovedFromTo = new KeyValuePair <CheckerModel, CheckerModel>(oldPositionedChecker.Clone(), newPosition.Clone()); _dataProvider.MoveCheckerToNewPosition(oldPositionedChecker, nextCol, nextRow); _dataProvider.StopTrackChanges(); return(historyMove); }