PROBLEM ArtificialAnt CODE << public struct Point { public int x; public int y; public Point(int x, int y) { this.x = x; this.y = y; } }; class World { private static string[] LosAltosTrail = new string[] { " ### ", " # .###.. ", " # # # ", " # # # ", " ####.##### .############.. . ", " # . # ", " # # . ", " # # . ", " # # # ", " . # . ", " # . . ", " # . # ", " # # . ", " # # ...###. ", " . .#... # ", " . . . ", " # . . ", " # # .#... ", " # # # ", " # # . ", " # # . ", " # . ...#. ", " # . # ", " ..##..#####. # # ", " # # # ", " # # # ", " # # # ", " # # # ", " # # # ", " # .#####....##.. # ", " # # # ", " . # # ", " .####.. # ", " #.# ", " # ", " # ", " # ", " # ", " # ", " # ", " # ", " ###### ", " . ", " # ", " # ", " # ", " # ", " # ", " # ", " #.. ", " # ", " # ", " # ", " # ", " # ", " .###### ", " . ", " # ", " # ", " # ", " # ", " # ", " # ", " # ", " # ", " # " }; private char[][] world = LosAltosTrail.Select(l=>l.ToArray()).ToArray(); public int TotalFood = LosAltosTrail.Select(l=>l.Where(c=>c=='#').Count()).Sum(); public int Width { get { return 100; } } public int Height { get { return 100; } } public void Reset() { world = LosAltosTrail.Select(l=>l.ToArray()).ToArray(); } public bool FoodAt(Point p) { if(p.x < 0 || p.y < 0) return false; if(world.Length <= p.y) return false; if(world[p.y].Length <= p.x) return false; return world[p.y][p.x] == '#'; } public void RemoveFoodAt(Point p) { if(world.Length < p.y) return; if(world[p.y].Length < p.x) return; world[p.y][p.x] = ' '; } } enum Direction {West, East, North, South}; class AntState { public Point Position { get; set;} public int MaxSteps { get; set;} public Direction Direction { get; set; } public World World { get; set; } public int Steps { get; set; } public int FoodEaten { get; set; } public AntState() { Steps = 0; FoodEaten = 0; } public void TurnLeft() { Steps++; switch(Direction) { case Direction.West: Direction = Direction.South; break; case Direction.South: Direction = Direction.East; break; case Direction.East: Direction = Direction.North; break; case Direction.North: Direction = Direction.West; break; } } public void TurnRight() { Steps++; switch(Direction) { case Direction.West: Direction = Direction.North; break; case Direction.South: Direction = Direction.West; break; case Direction.East: Direction = Direction.South; break; case Direction.North: Direction = Direction.East; break; } } public void MoveForward() { Position = NextPosition; Steps++; if(World.FoodAt(Position)) { World.RemoveFoodAt(Position); FoodEaten++; } } public Point NextPosition { get { int x, y; switch(Direction) { case Direction.West: x = (Position.x + World.Width - 1) % World.Width; y = Position.y; return new Point(x, y); case Direction.South: x = Position.x; y = (Position.y + World.Height + 1) % World.Height; return new Point(x, y); case Direction.East: x = (Position.x + World.Width + 1) % World.Width; y = Position.y; return new Point(x, y); case Direction.North: x = Position.x; y = (Position.y + World.Height - 1) % World.Height; return new Point(x, y); } return new Point(-1,-1); } } } World CreateWorld() { var w = new World(); Console.WriteLine("Total food: "+w.TotalFood); return w; } World world; >> INIT << world = CreateWorld(); >> NONTERMINALS Ant<>. IfFoodAhead<>. Prog2<>. Prog3<>. TERMINALS Left. Right. Move. RULES Ant<> = IfFoodAhead<> | Prog2<> | Prog3<> | Left SEM << state.TurnLeft(); >> | Right SEM << state.TurnRight(); >> | Move SEM << state.MoveForward(); >> . IfFoodAhead<> = SEM << if(world.FoodAt(state.NextPosition)) { >> Ant<> SEM << } else { >> Ant<> SEM << } >> . Prog2<> = Ant<> Ant<> . Prog3<> = Ant<> Ant<> Ant<> . MAXIMIZE /* could also use the keyword MINIMIZE here */ << world.Reset(); var state = new AntState() {Position = new Point(0,0), MaxSteps=400, Direction=Direction.East, World = world}; while(state.Steps < state.MaxSteps) { Ant(world, state); } return (double)state.FoodEaten; >> END ArtificialAnt.