Пример #1
0
        private async void Run()
        {
            Running = true;

            _turns = 1;                                  // keeping track of the game's turns
            var actingTank = _random.Next(_tanks.Count); // the index of the tank that is acting this turn

            var chainedEvents = new List <Event>();      // events triggered from last turn

            while (Running)
            {
                Logger.Log($"--- Turn {_turns}: ---");
                _map.CurrentWeather = await _weatherApiClient.GetWeatherAsync();

                Logger.Log($"It's {_map.CurrentWeather} outside.");

                // Processing chained events from last turn before acting this turn
                var eventsToAdd = new List <Event>();
                for (int i = chainedEvents.Count - 1; i >= 0; i--)
                {
                    var chainResult = chainedEvents[i].Process(Logger);
                    if (chainResult != EventResult.Succeeded)
                    {
                        eventsToAdd.Add(chainResult.ChainEvent);
                    }
                    chainedEvents.RemoveAt(i);
                }
                chainedEvents.AddRange(eventsToAdd);
                eventsToAdd.Clear();

                // This turn's action, as well as its consequences (chained events)
                var resultEvent = _tanks[actingTank].Act();
                var result      = resultEvent.Process(Logger);
                if (result != EventResult.Succeeded)
                {
                    chainedEvents.Add(result.ChainEvent);
                }

                // Win condition
                if (_tanks.Any(t => t.IsDestroyed))
                {
                    Running = false;
                    _winner = _tanks.SingleOrDefault(t => !t.IsDestroyed);
                    break;
                }

                // Changing the turn
                actingTank = (actingTank + 1) % _tanks.Count;
                _turns++;
            }
            OnGameFinished();
        }