public MapTile GetTileTo(AntWrapper ant, AntAction action) { var x = ant.CurrentTile.X; var y = ant.CurrentTile.Y; switch (action) { case AntAction.ShootRight: case AntAction.EchoRight: return(GetTileTo(() => ++ x, () => y)); case AntAction.ShootDown: case AntAction.EchoDown: return(GetTileTo(() => x, () => ++ y)); case AntAction.ShootLeft: case AntAction.EchoLeft: return(GetTileTo(() => -- x, () => y)); case AntAction.ShootUp: case AntAction.EchoUp: return(GetTileTo(() => x, () => -- y)); } return(null); }
/// <summary> /// Sets the action for the current turn and sets a local _lastAction because the game will reset Action each turn to Wait and we lose what was there. /// </summary> /// <param name="a">The Action to set for the current turn.</param> private void SetAction(AntAction a) { //Set the action for the current turn. Action = a; //Set the last action so we know the direction on the next turn for successful moving and echo response. _lastAction = a; }
/// <summary> /// Take in an action and return its opposite direction. /// </summary> /// <param name="a">Direction to get the opposite.</param> /// <returns>Opposite direction of action, Wait if no opposite exists.</returns> private static AntAction OppositeDirection(AntAction a) { switch (a) { case AntAction.MoveRight: return(AntAction.MoveLeft); case AntAction.MoveDown: return(AntAction.MoveUp); case AntAction.MoveLeft: return(AntAction.MoveRight); case AntAction.MoveUp: return(AntAction.MoveDown); case AntAction.EchoRight: return(AntAction.EchoLeft); case AntAction.EchoDown: return(AntAction.EchoUp); case AntAction.EchoLeft: return(AntAction.EchoRight); case AntAction.EchoUp: return(AntAction.EchoDown); case AntAction.ShootRight: return(AntAction.ShootLeft); case AntAction.ShootDown: return(AntAction.ShootUp); case AntAction.ShootLeft: return(AntAction.ShootRight); case AntAction.ShootUp: return(AntAction.ShootDown); default: return(AntAction.Wait); } }
/// <summary> /// Check if we can move to this space without running into anything. /// </summary> /// <param name="a">Action which brings us to the space.</param> /// <returns>True if safe.</returns> private bool CanMove(AntAction a) { MapPosition nextSpace; switch (a) { case AntAction.MoveRight: if (_currentX + 1 == _mapWidth) { return(false); } nextSpace = _map[_currentX + 1, _currentY]; break; case AntAction.MoveDown: if (_currentY + 1 == _mapHeight) { return(false); } nextSpace = _map[_currentX, _currentY + 1]; break; case AntAction.MoveLeft: if (_currentX - 1 == -1) { return(false); } nextSpace = _map[_currentX - 1, _currentY]; break; case AntAction.MoveUp: if (_currentY - 1 == -1) { return(false); } nextSpace = _map[_currentX, _currentY - 1]; break; default: return(true); } return(nextSpace.Known && !Blocked(nextSpace.Item)); }
public static GameEvent ShotDirectionToEvent(AntAction a) { switch (a) { case AntAction.ShootRight: return(GameEvent.ImpactDamageLeft); case AntAction.ShootDown: return(GameEvent.ImpactDamageUp); case AntAction.ShootLeft: return(GameEvent.ShotDamageRight); case AntAction.ShootUp: return(GameEvent.ShotDamageDown); default: return(GameEvent.Nothing); } }
/// <summary> /// Initialize the ant with current map settings, runs once before each game starts. /// </summary> /// <param name="mapWidth">How many spaces wide is the current map.</param> /// <param name="mapHeight">How many spaces high is the current map.</param> /// <param name="color">Our ant's current color.</param> /// <param name="startX">X position in map where the ant starts.</param> /// <param name="startY">Y position in map where the ant starts.</param> public override void Initialize(int mapWidth, int mapHeight, ItemColor color, int startX, int startY) { _mapWidth = mapWidth; _mapHeight = mapHeight; _myColor = color; _currentX = startX; _currentY = startY; _currentMode = 0; _lastAction = AntAction.Wait; //Initialize internal map _map = new MapPosition[_mapWidth, _mapHeight]; for (var y = 0; y < _mapHeight; ++y) { for (var x = 0; x < _mapWidth; ++x) { _map[x, y] = new MapPosition(); } } _map[_currentX, _currentY].Known = true; SetSearchDirection(); }
/// <summary> /// Try to guess a general direction to search in. /// </summary> private void SetSearchDirection() { if (_currentX == 0 || (decimal)_mapWidth / _currentX < 0.9m) { //search right _searchPrimary = AntAction.MoveRight; } else { //search left _searchPrimary = AntAction.MoveLeft; } if (_currentY == 0 || (decimal)_mapHeight / _currentY < 0.9m) { //search down _searchSecondary = AntAction.MoveDown; } else { //search up _searchSecondary = AntAction.MoveUp; } }
/// <summary> /// Mode to randomly wonder around the map looking for the flag. /// </summary> private void MapMode() { //Check left if (_currentX > 0 && !_map[_currentX - 1, _currentY].Known) { SetAction(AntAction.EchoLeft); return; } //Check Right if (_currentX < _mapWidth - 1 && !_map[_currentX + 1, _currentY].Known) { SetAction(AntAction.EchoRight); return; } //Check Up if (_currentY > 0 && !_map[_currentX, _currentY - 1].Known) { SetAction(AntAction.EchoUp); return; } //Check Down if (_currentY < _mapHeight - 1 && !_map[_currentX, _currentY + 1].Known) { SetAction(AntAction.EchoDown); return; } //All tiles next to us are known, move a direction. if (CanMove(_searchPrimary)) { SetAction(_searchPrimary); return; } if (CanMove(_searchSecondary)) { SetAction(_searchSecondary); return; } //Can't move in that direction any more, try a different direction. if (_lastAction != _searchPrimary && CanMove(OppositeDirection(_searchPrimary))) { _searchPrimary = OppositeDirection(_searchPrimary); SetAction(_searchPrimary); return; } if (CanMove(OppositeDirection(_searchSecondary))) { _searchSecondary = OppositeDirection(_searchSecondary); SetAction(_searchSecondary); return; } //Can't find a direction, reset current search directions SetSearchDirection(); }