public void FinishTurn() { var side = Sides[(int)Chess.InverseColor(Logic.Turn)]; var opponent = side.GetPlayer(); var opponentGuid = side.PlayerGuid; if (side != null && !side.IsAi()) { SendMoveResponse(opponent, MoveResult); } side = Sides[(int)Logic.Turn]; if (side != null) { if (side.IsAi()) { AiState = ChessAiState.WaitingToStart; } else { var move = Logic.GetLastMove(); var piece = Logic.GetPiece(move.To); var data = new GameMoveData(ChessMoveType.FromTo, move.Color, move.From, move.To); SendOpponentTurn(side.GetPlayer(), opponentGuid, piece, data); } } }
public void StartAiMove() { Debug.Assert(AiState == ChessAiState.WaitingToStart); AiState = ChessAiState.WaitingForWorker; // todo: execute ai work on a separate thread var key = new ChessAiAsyncTurnKey(); AiFuture = new ChessAiMoveResult(); AsyncMoveAiSimple(key, AiFuture); }
public void AsyncMoveAiSimple(ChessAiAsyncTurnKey key, ChessAiMoveResult result) { Debug.Assert(AiState == ChessAiState.WaitingForWorker); AiState = ChessAiState.InProgress; ChessPieceCoord from = null, to = null; var moveResult = Logic.AsyncCalculateAiSimpleMove(key, ref from, ref to); result.SetResult(moveResult, from, to); AiState = ChessAiState.WaitingForFinish; }
public void AsyncMoveAiComplex(ChessAiAsyncTurnKey key, ChessAiMoveResult result) { Debug.Assert(AiState == ChessAiState.WaitingForWorker); AiState = ChessAiState.InProgress; ChessPieceCoord from = null, to = null; uint counter = 0; var moveResult = Logic.AsyncCalculateAiComplexMove(key, ref from, ref to, ref counter); result.SetResult(moveResult, from, to); result.ProfilingCounter = counter; AiState = ChessAiState.WaitingForFinish; }
public void FinishAiMove() { Debug.Assert(AiState == ChessAiState.WaitingForFinish); AiState = ChessAiState.None; var result = AiFuture; MoveResult = result.Result; if (MoveResult == ChessMoveResult.NoMoveResult) { var color = Chess.InverseColor(Logic.Turn); var side = Sides[(int)color]; var opSide = Sides[(int)Logic.Turn]; // checkmate if (Logic.InCheckmate(color, true)) { Finish((int)Logic.Turn); } // stalemate else { side.Stalemate = true; if (opSide.Stalemate) { Finish(Chess.ChessWinnerStalemate); } else { SendOpponentStalemate(opSide.GetPlayer(), side.Color, true); } } } else { FinalizeWeenieMove(result.Result); } //Console.WriteLine($"Calculated Chess AI move in {result.ProfilingTime} ms with {result.ProfilingCounter} minimax calculations."); }