private CarFoundResult FindTheCar(ICarFinder carFinder, int currentTickCount) { if (carFinder.IsCarFound) { return(null); } //call Find method agent var calculatedCarPosition = carFinder.Find(currentTickCount); var result = new CarFoundResult() { CarFoundTime = currentTickCount, FinderName = carFinder.Name, IsCarFound = false, Position = calculatedCarPosition }; //chceck if the calculated position is matching with currentposition if (calculatedCarPosition == currentTickCount) { result.IsCarFound = true; } return(result); }
private void ProcessTick(KeyValuePair <uint, int> velocity, ParallelOptions parallelOptions) { parallelOptions.CancellationToken.ThrowIfCancellationRequested(); _car.MoveCar(velocity.Key, velocity.Value); if (OnCarMoved != null) { OnCarMoved(this, new GameProgressEventArgs(velocity.Key, _maxTicks)); } ConcurrentBag <Task> tasks = new ConcurrentBag <Task>(); foreach (ICarFinder carFinder in _carFinders.Value) { ICarFinder finder = carFinder; if (!finder.SearchScore.IsSuccess) { tasks.Add(Task.Factory.StartNew(() => finder.GuessPosition(velocity.Key, _car))); } } Task.WaitAll(tasks.ToArray()); bool isSuccess = _carFinders.Value.All(x => x.SearchScore.IsSuccess); if (isSuccess) { if (OnGameComplete != null) { OnGameComplete(this, new GameCompleteEventArgs() { Success = true, CarFinders = _carFinders.Value.ToList() }); } } }
public Application(ICarFinder carFinder) { CarFinder = carFinder; }