// Display the current map public override string ToString() { string s = ""; for (int y = 0; y < Rows; y++) { for (int x = 0; x < Cols; x++) { Coordinates pos = new Coordinates(x, y); char c = '_'; if (MyAnts.ContainsKey(pos)) { c = 'a'; } else if (Walls.Contains(pos)) { c = '#'; } else if (Enemies.Contains(pos)) { c = 'e'; } else if (Food.Contains(pos)) { c = '.'; } else if (Hills.Contains(pos)) { c = '*'; } s += c; } s += "\n"; } return(s); }
public void ComputeMoves(GameActionsMessage msg) { // Next ants positions Dictionary <Coordinates, Ant> updated = new Dictionary <Coordinates, Ant>(); Random rand = new Random(); foreach (KeyValuePair <Coordinates, Ant> pair in MyAnts) { Ant ant = pair.Value; Coordinates pos = pair.Key; AntDirection move; Coordinates next; if (ant.HasFood) { if (ant.History.Count > 0 && !Hills.Contains(pos)) { // Walk back move = ant.History.Pop().Reverse(); next = pos.ApplyDirection(move).Normalize(this); // If the path is blocked, don't move (and put the move back on the history for next round) if (!Enemies.Contains(next) && !updated.ContainsKey(next)) { msg.AddMove(pos.Col, pos.Row, move.ToString()); updated.Add(next, ant); } else { ant.History.Push(move.Reverse()); } continue; } else { ant.HasFood = false; } } move = GetNearbyFood(pos); // If there is accessible food, go there if (move == AntDirection.U) { List <AntDirection> remaining = new List <AntDirection>() { AntDirection.N, AntDirection.S, AntDirection.E, AntDirection.W }; int index; // First move to try : continue the same way or not ? 50% chance if (ant.History.Count > 0 && rand.NextDouble() < 0.5) { move = ant.History.Peek(); remaining.Remove(move); } else { index = rand.Next(4); move = remaining[index]; remaining.RemoveAt(index); } int size = remaining.Count; do { Coordinates result = pos.ApplyDirection(move).Normalize(this); if (!Walls.Contains(result) && !MyAnts.ContainsKey(result) && !updated.ContainsKey(result)) { break; } if (size > 0) { index = rand.Next(size); move = remaining[index]; remaining.RemoveAt(index); } else { move = AntDirection.U; } size--; } while (size >= 0); } else { ant.HasFood = true; } next = pos.ApplyDirection(move).Normalize(this); if (move != AntDirection.U && !updated.ContainsKey(next)) { ant.History.Push(move); msg.AddMove(pos.Col, pos.Row, move.ToString()); updated.Add(next, ant); } } MyAnts = updated; }