private void ResolveMoves() { IList <Latch> requests = _latches.Where(x => x.State == LatchState.RequestActivation).ToList(); while (requests.Any()) { foreach (Latch latchRequest in requests) { Fish eater = latchRequest.Eater; Food target = latchRequest.Target; Latch counterRequest = (target is Fish) ? ExistingLatch((Fish)target, eater) : null; if (counterRequest == null) { // unopposed request latchRequest.State = LatchState.Activated; } else if (eater.Val > 0 && target.Val > 0) { DoBattleAndActivateWinner(latchRequest, counterRequest); } else { latchRequest.State = LatchState.ToRemove; } } requests = _latches.Where(x => x.State == LatchState.RequestActivation).ToList(); } }
private void AddLatch(Fish eater, Food target, int val) { if (_latches.Any(x => x.Eater == eater && x.Target == target)) { throw new ApplicationException("Latch already exists in some state"); } var newLatch = new Latch(eater, target, val); _latches.Add(newLatch); newLatch.Eater.NotifyLatch(newLatch); }
private void DoBattleAndActivateWinner(Latch latchRequest, Latch counterRequest) { Random rnd = new Random(); float rollOne = rnd.Next(0, latchRequest.Eater.Val) - rnd.Next(0, latchRequest.Val); float rollTwo = rnd.Next(0, counterRequest.Eater.Val) - rnd.Next(0, counterRequest.Val); if (rollOne > rollTwo) { latchRequest.State = LatchState.Activated; counterRequest.State = LatchState.ToRemove; } else { latchRequest.State = LatchState.ToRemove; counterRequest.State = LatchState.Activated; } }
public Move GetMove() { IList <Latch> deadLatches = _latchesIOwn.Where(x => x.State == LatchState.ToRemove).ToList(); foreach (Latch latch in deadLatches) { _latchesIOwn.Remove(latch); } var move = new Move(); move.MoveTowards = default(Vector2); IList <Latch> eatable = new List <Latch>(_latchesIOwn.Where(x => x.State == LatchState.Dormant || x.State == LatchState.Activated)); if (eatable.Any()) { Latch chosen = eatable.Any(x => this.DistanceToFood(x.Target) < 50) ? // switch target if food is close eatable.OrderBy(x => this.DistanceToFoodSquared(x.Target)).First() : eatable.OrderByDescending(x => x.Target.Val).First(); if (chosen.State != LatchState.Activated) { move.Eat = chosen; } move.MoveTowards = chosen.Target.Pos; } if (!_latchesIOwn.Any(x => x.State == LatchState.Activated)) { var fishNearMe = _fishBowl.FoodNearMe(this); if (fishNearMe.Count > 0) { int which = new Random().Next(fishNearMe.Count); Food target = fishNearMe[which]; if (!_latchesIOwn.Any(x => x.Target == target) && target != this) { move.Latch = new Tuple <Food, int>(target, target.Val); } move.MoveTowards = target.Pos; } } return(move); }
public void NotifyLatch(Latch latch) { _latchesIOwn.Add(latch); }