/// <summary> /// /// </summary> /// <param name="startModel"></param> /// <param name="endModel"></param> /// <returns></returns> public static bool MoveModel(CellViewModel startModel, CellViewModel endModel) { // No Chesspiece to move or one of the ViewModel's is empty if (startModel == null || endModel == null || startModel.CurrentChessPiece == null) { return(false); } // Same color if (endModel.CurrentChessPiece != null && (startModel.CurrentChessPiece.IsWhite() == endModel.CurrentChessPiece.IsWhite())) { return(false); } // No Path to the destination / endModel if (!endModel.CanMoveHere.Any(o => Equals(o.StartCell, startModel)) && !endModel.CanEatHere.Any(o => Equals(o.StartCell, startModel))) { return(false); } // Startmodel has a ChessPece // Startmodel and Endmodel aren't the same color // Startmodel was able to find a path to the Endmodel // Start and Endmodel are not the same endModel.MoveToGraveyard(); endModel.CurrentChessPiece = startModel.CurrentChessPiece; endModel.CurrentChessPiece.DidMove = true; startModel.CurrentChessPiece = null; return(true); }
public object Clone() { var newCellViewModel = new CellViewModel(CurrentChessPiece, Board); newCellViewModel.CanEatHere.AddRange(CanEatHere.Select(path => path.ClonePath())); newCellViewModel.CanMoveHere.AddRange(CanEatHere.Select(path => path.ClonePath())); newCellViewModel.Bgc = Bgc; newCellViewModel.Name = Name; newCellViewModel.CurrentChessPiece = CurrentChessPiece?.CloneChessPiece(); return(newCellViewModel); }
/// <summary> /// /// </summary> /// <param name="startModel"></param> /// <param name="endModel"></param> /// <returns></returns> public static bool MoveModel(CellViewModel startModel, CellViewModel endModel) { // No Chesspiece to move or one of the ViewModel's is empty if (startModel == null || endModel == null || startModel.CurrentChessPiece == null) { return false; } // Same color if (endModel.CurrentChessPiece != null && (startModel.CurrentChessPiece.IsWhite() == endModel.CurrentChessPiece.IsWhite())) { return false; } // No Path to the destination / endModel if (!endModel.CanMoveHere.Any(o => Equals(o.StartCell, startModel)) && !endModel.CanEatHere.Any(o => Equals(o.StartCell, startModel))) { return false; } // Startmodel has a ChessPece // Startmodel and Endmodel aren't the same color // Startmodel was able to find a path to the Endmodel // Start and Endmodel are not the same endModel.MoveToGraveyard(); endModel.CurrentChessPiece = startModel.CurrentChessPiece; endModel.CurrentChessPiece.DidMove = true; startModel.CurrentChessPiece = null; return true; }
private async Task CreateEmptyChessBoard() { for (var number = 1; number <= 8; number++) { for (int character = 'A'; character <= 'H'; character++) { var propertyName = (char) character + number.ToString(); var property = new CellViewModel(null, this) { Name = propertyName }; AllCells.Add(property); } } await CreateLink(); }
public object Clone() { var newCellViewModel = new CellViewModel(CurrentChessPiece, Board); newCellViewModel.CanEatHere.AddRange(CanEatHere.Select(path => path.ClonePath())); newCellViewModel.CanMoveHere.AddRange(CanEatHere.Select(path => path.ClonePath())); newCellViewModel.Bgc = Bgc; newCellViewModel.Name = Name; newCellViewModel.CurrentChessPiece = CurrentChessPiece?.CloneChessPiece(); return newCellViewModel; }
private async Task SelectCellViewModel(CellViewModel cellThatGotClicked) { // NextTurn, when the ViewModel moved to the newly selected ViewModel if (CellViewModel.MoveModel(SelectedCellViewModel, cellThatGotClicked)) { AddToHistory(SelectedCellViewModel, cellThatGotClicked); // Reset CheckedKingColor ResetColors(true); await NextTurn(); SelectedCellViewModel = null; } // Select ViewModel, if it's the players turn else if (cellThatGotClicked?.CurrentChessPiece != null && WhiteTurn == cellThatGotClicked.CurrentChessPiece.IsWhite()) { SelectedCellViewModel = cellThatGotClicked; SelectedCellViewModel.StartColorize(); MarkCheck(); } else { SelectedCellViewModel = null; } }
/// <summary> /// Add a new Element to the History /// </summary> /// <param name="startModel">The StartCell, which was selected first</param> /// <param name="endModel">The EndCell, which was moved / eaten to</param> private void AddToHistory(CellViewModel startModel, CellViewModel endModel) { if (startModel == null || endModel == null) { return; } var historyViewModel = new HistoryViewModel { FromCell = startModel, ToCell = endModel }; History.Add(historyViewModel); }
/// <summary> /// Add a new Element to the GraveYard /// </summary> /// <param name="cellViewModel">The Cell that will be added to the GraveYard</param> public void AddToGraveYard(CellViewModel cellViewModel) { if (cellViewModel?.CurrentChessPiece == null) { return; } GraveYard.Add(cellViewModel.CurrentChessPiece); }
/// <summary> /// Validates a step /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <returns>True, when the step doesn't put the own king into checkmate</returns> public async Task<bool> ValidateMovement(CellViewModel from, CellViewModel to) { var tmpBoard = await CloneBoard(); var tmpFrom = tmpBoard.AllCells.FirstOrDefault(cell => cell.Equals(from)); var tmpTo = tmpBoard.AllCells.FirstOrDefault(cell => cell.Equals(to)); await tmpBoard.CalculatePossibleSteps(true); CellViewModel.MoveModel(tmpFrom, tmpTo); foreach (var cell in tmpBoard.AllCells) { cell.CanEatHere.Clear(); cell.CanMoveHere.Clear(); } await tmpBoard.CalculatePossibleSteps(true); return !tmpBoard.CalculateKingUnderAttack(); }
/// <summary> /// Method that gets called when a player clicks on a CellViewModel. /// </summary> /// <param name="mouseButtonState">Only checks wheter the Left or Right mousebutton is the ChangedButton</param> /// <param name="cellThatGotClicked">CellViewModel, that got clicked. Either selects or moves a Cell</param> public async Task CellViewModelOnMouseDown(MouseButtonEventArgs mouseButtonState, CellViewModel cellThatGotClicked) { ResetColors(); switch (mouseButtonState.ChangedButton) { case MouseButton.Left: await SelectCellViewModel(cellThatGotClicked); break; case MouseButton.Right: // Mark CellViewModel's which can move here, just colorisation foreach (var canMoveHere in cellThatGotClicked.CanMoveHere) { canMoveHere.StartCell.Bgc = CellViewModel.CanMoveHereColor; } foreach (var canEathere in cellThatGotClicked.CanEatHere) { canEathere.StartCell.Bgc = CellViewModel.CanEatHereColor; } break; case MouseButton.Middle: break; case MouseButton.XButton1: break; case MouseButton.XButton2: break; default: throw new ArgumentOutOfRangeException(); } }