public async Task <RoundResult> PerformNextRoundAsync() { _roundNumber++; _movementService.ExpiryExplosions(); await DelayHelper.DelayAsync(_delayTime); _movementService.PerformMissilesMove(); await DelayHelper.DelayAsync(_delayTime); //call winner if (_antWarsViewModel.MovableObjectsCollection.OfType <AntModel>().Count() <= 1) { return(new RoundResult { FinalResult = _botService.GetBotResults(), IsFinished = true, History = new List <RoundPartialHistory>() }); } var partialResults = await _movementService.PlayAntsMoveAsync(200, _roundNumber); return(new RoundResult { FinalResult = null, IsFinished = false, History = partialResults, }); }
public async Task <RoundResult> PerformNextRoundAsync() { if (Player1 == null || Player2 == null) { throw new Exception("There are no players to perform next round."); } var result = new RoundResult() { History = new List <RoundPartialHistory>(), IsFinished = false }; foreach (var competitor in _competitors) { if (IsBoardFull()) { ClearTheBoard(); } result.History.Add(await PerformNextMove(competitor)); await DelayHelper.DelayAsync(_configuration.NextMoveDelay); if (await IsPlayerWon(competitor)) { result.IsFinished = true; result.FinalResult = GetResults(); break; } } return(result); }
public async override void Execute(object parameter = null) { _viewModel.IsGameInProgress = true; if (_viewModel.IsGamePaused) { _viewModel.IsGamePaused = false; await _viewModel.ResumeGameAsync(); await DelayHelper.DelayAsync(_viewModel.ArenaConfiguration.GameConfiguration.NextMatchDelay); } while (_viewModel.Elimination.GetNextCompetitors() != null && _viewModel.IsGameInProgress) { await _viewModel.PlayNextGameAsync(); await DelayHelper.DelayAsync(_viewModel.ArenaConfiguration.GameConfiguration.NextMatchDelay); } _viewModel.IsGameInProgress = false; if (_viewModel.ShouldRestartGame) { _viewModel.RestartGame(); } }
public async Task HandleExplodablesAsync(int explosionDisplayTime) { _field.Explosions.Clear(); foreach (var missile in _field.Missiles) { HandleMissileMovement(missile); } foreach (var bomb in _field.Bombs) { bomb.RoundsUntilExplodes--; if (!bomb.IsExploded && bomb.RoundsUntilExplodes == 0) { SetExplosion(bomb); } } _field.Bombs.RemoveAll(bomb => bomb.IsExploded); _field.Missiles.RemoveAll(missile => missile.IsExploded); _field.OnArenaChanged(); await DelayHelper.DelayAsync(explosionDisplayTime); _field.Explosions.Clear(); }
private async Task SetArenaMessage(string message) { ArenaMessage = message; IsArenaMessageVisible = true; await DelayHelper.DelayAsync(ArenaConfiguration.ArenaMessageDuration); IsArenaMessageVisible = false; }
public async Task <List <RoundPartialHistory> > PlayAntsMoveAsync(int delayBetweenBots, int roundnumber) { var result = new List <RoundPartialHistory>(); _listOfMissilesToFire.Clear(); foreach (var movableObject in _antWarsViewModel.MovableObjectsCollection.OfType <AntModel>()) { result.Add(await PerformMove(movableObject, roundnumber)); await DelayHelper.DelayAsync(delayBetweenBots); } //fire missile foreach (var missile in _listOfMissilesToFire) { _antWarsViewModel.MovableObjectsCollection.Add(missile); } return(result); }
public async Task <RoundResult> PlayBotMovesAsync(int delayTime, int roundNumber) { var result = new RoundResult() { FinalResult = null, IsFinished = false, History = new List <RoundPartialHistory>() }; int turnNumber = 0; foreach (var dynaBlasterBot in _field.Bots.Where(bot => !bot.IsDead)) { BotMove move; try { turnNumber++; move = await dynaBlasterBot.NextMoveAsync(GetBotBattlefieldInfo(dynaBlasterBot, roundNumber, turnNumber)); } catch (Exception e) { move = new BotMove() { Action = BotAction.None, Direction = null }; result.OutputText += string.Format("Bot {0} threw exception:\n{1}\n", dynaBlasterBot.Name, e.Message); } if (IsMoveValid(dynaBlasterBot, move)) { result.History.Add(PerformMove(dynaBlasterBot, move, roundNumber)); _field.OnArenaChanged(); } await DelayHelper.DelayAsync(delayTime); } return(result); }
private async Task <bool> IsPlayerWon(ITicTacToeBot player) { var array = TicTacToeViewModel.Board; var diagonal1 = 0; var diagonal2 = 0; var xLine = new int[3]; var yLine = new int[3]; for (int i = 0; i < array.XSize; i++) { for (int j = 0; j < array.YSize; j++) { if (array[i, j] == player.PlayerSign) { xLine[i]++; yLine[j]++; } if ((i == j) && array[j, i] == player.PlayerSign) { diagonal1++; } if ((((i == 1) && (j == 1)) || ((i == 2) && (j == 0)) || ((i == 0) && (j == 2))) && array[i, j] == player.PlayerSign) { diagonal2++; } } if (diagonal1 == 3) { TicTacToeViewModel.ArrayOfDiagonalLines[0, 0] = Visibility.Visible; player.IsWinner = true; await DelayHelper.DelayAsync(_configuration.NextMatchDelay); return(true); } if (diagonal2 == 3) { TicTacToeViewModel.ArrayOfDiagonalLines[1, 0] = Visibility.Visible; player.IsWinner = true; await DelayHelper.DelayAsync(_configuration.NextMatchDelay); return(true); } for (int j = 0; j < 3; j++) { if (xLine[j] == 3) { TicTacToeViewModel.ArrayOfHorizontalLines[j, 0] = Visibility.Visible; player.IsWinner = true; await DelayHelper.DelayAsync(_configuration.NextMatchDelay); return(true); } if (yLine[j] == 3) { TicTacToeViewModel.ArrayOfVerticalLines[j, 0] = Visibility.Visible; player.IsWinner = true; await DelayHelper.DelayAsync(_configuration.NextMatchDelay); return(true); } } } player.IsWinner = false; return(false); }