/// <summary> /// Applies a move for the current player at the given position. /// </summary> public async Task ApplyMove(BoardPosition position) { var possMoves = mBoard.GetPossibleMoves() as IEnumerable <ChessMove>; // Validate the move as possible. foreach (var move in possMoves) { if (SelectedSquare.Position.Equals(move.StartPosition) && move.EndPosition.Equals(position)) { if (move.MoveType == ChessMoveType.PawnPromote) { var window = new PawnPromotionWindow(this, move.StartPosition, move.EndPosition); window.ShowDialog(); Promote = window.promotePicked; ChessMove m = new ChessMove(move.StartPosition, move.EndPosition, ChessMove.StringToPromoType(Promote), ChessMoveType.PawnPromote); mBoard.ApplyMove(m); foreach (var s in mSquares) { s.KingInCheck = false; } SelectedState = false; break; } else { mBoard.ApplyMove(move); foreach (var s in mSquares) { s.KingInCheck = false; } SelectedState = false; break; } } } RebindState(); if (Players == NumberOfPlayers.One && !mBoard.IsFinished) { var bestMove = await Task.Run(() => mGameAi.FindBestMove(mBoard)); if (bestMove != null) { mBoard.ApplyMove(bestMove as ChessMove); } } RebindState(); if (mBoard.IsFinished) { GameFinished?.Invoke(this, new EventArgs()); } //Show Boardweight in a message box when a move is applied //MessageBox.Show("Board Weight: " + mBoard.BoardWeight); }
/// <summary> /// Applies a move for the current player at the given position. /// </summary> public void ApplyMove(BoardPosition position) { var possMoves = mBoard.GetPossibleMoves() as IEnumerable <ChessMove>; // Validate the move as possible. foreach (var move in possMoves) { if (SelectedSquare.Position.Equals(move.StartPosition) && move.EndPosition.Equals(position)) { if (move.MoveType == ChessMoveType.PawnPromote) { var window = new PawnPromotionWindow(this, move.StartPosition, move.EndPosition); window.ShowDialog(); Promote = window.promotePicked; ChessMove m = new ChessMove(move.StartPosition, move.EndPosition, ChessMove.StringToPromoType(Promote), ChessMoveType.PawnPromote); mBoard.ApplyMove(m); foreach (var s in mSquares) { s.KingInCheck = false; } SelectedState = false; break; } else { mBoard.ApplyMove(move); foreach (var s in mSquares) { s.KingInCheck = false; } SelectedState = false; break; } } } RebindState(); if (mBoard.IsFinished) { GameFinished?.Invoke(this, new EventArgs()); } }