private bool DoHumanMove() { var nextHumanMove = PickHumanMove(); if (nextHumanMove == null) { return(false); } if (!GameAnalysis.IsMoveLegal(currentBoardState, nextHumanMove)) { WinnerAvailable?.Invoke(computerPlayer, WinningReason.InvalidMove, nextHumanMove); return(false); } currentBoardState = currentBoardState.ApplyMove(nextHumanMove); NewBoardStateAvailable?.Invoke(currentBoardState); if (nextHumanMove is Capitulation) { WinnerAvailable?.Invoke(computerPlayer, WinningReason.Capitulation, null); } var winner = GameAnalysis.CheckWinningCondition(currentBoardState); if (winner != null) { WinnerAvailable?.Invoke(winner, WinningReason.RegularQuoridorWin, null); return(false); } return(true); }
public void Run() { IsRunning = true; NewBoardStateAvailable?.Invoke(currentBoardState); var moveCounter = 0; while (!stopRunning) { if (moveCounter >= gameConstraints.MaximalMovesPerPlayer) { WinnerAvailable?.Invoke(currentBoardState.TopPlayer.Player, WinningReason.ExceedanceOfMaxMoves); } var nextBottomPlayerBotMove = GetBottomPlayerBotMove(); if (nextBottomPlayerBotMove == null) { break; } if (!GameAnalysis.IsMoveLegal(currentBoardState, nextBottomPlayerBotMove)) { WinnerAvailable?.Invoke(currentBoardState.TopPlayer.Player, WinningReason.InvalidMove); break; } currentBoardState = currentBoardState.ApplyMove(nextBottomPlayerBotMove); NewBoardStateAvailable?.Invoke(currentBoardState); if (nextBottomPlayerBotMove is Capitulation) { WinnerAvailable?.Invoke(currentBoardState.TopPlayer.Player, WinningReason.Capitulation); } var winner = GameAnalysis.CheckWinningCondition(currentBoardState); if (winner != null) { WinnerAvailable?.Invoke(winner, WinningReason.RegularQuoridorWin); break; } var nextTopPlayerBotMove = GetTopPlayerBotMove(); if (nextTopPlayerBotMove == null) { break; } if (!GameAnalysis.IsMoveLegal(currentBoardState, nextTopPlayerBotMove)) { WinnerAvailable?.Invoke(currentBoardState.BottomPlayer.Player, WinningReason.InvalidMove); break; } currentBoardState = currentBoardState.ApplyMove(nextTopPlayerBotMove); NewBoardStateAvailable?.Invoke(currentBoardState); if (nextTopPlayerBotMove is Capitulation) { WinnerAvailable?.Invoke(currentBoardState.BottomPlayer.Player, WinningReason.Capitulation); } var winner2 = GameAnalysis.CheckWinningCondition(currentBoardState); if (winner2 != null) { WinnerAvailable?.Invoke(winner2, WinningReason.RegularQuoridorWin); break; } moveCounter++; } IsRunning = false; bottomPlayerBot.NextMoveAvailable -= OnNextBottomPlayerBotMoveAvailable; topPlayerBot.NextMoveAvailable -= OnNextTopPlayerBotMoveAvailable; }