private List <Point> GetPoints(Compass direction, Point zeroLocation, int distance) { return(Enumerable .Range(1, distance) .Select(offset => new { sign = CompassMapping.GetDirectionSign(direction), offset = CompassMapping.GetDirectionOffset(direction, (uint)offset) }) .Select(p => zeroLocation + (p.offset * p.sign)).ToList()); }
/// <summary> /// /// </summary> /// <param name="state"></param> public void UpdateMap(TankState state) { var cardinalMapping = CompassMapping.GetCompassMap(state.CardinalDirection); var directions = new[] { Direction.Front, Direction.Back, Direction.Left, Direction.Right }; foreach (var tankSide in directions) { var isTargetInSight = tankSide == Direction.Front ? (bool?)state.TargetInSight : null; var cardinalDirection = cardinalMapping[tankSide]; _compassMap[cardinalDirection](state.CurrentPosition, isTargetInSight, state[tankSide]); } }
public static void PerformAction(TankAction action) { var apiAction = default(System.Action); switch (action) { case TankAction.FireCannon: apiAction = API.FireCannon; break; case TankAction.MoveForward: apiAction = API.MoveForward; _currentPosition = _currentPosition + CompassMapping.GetDirectionSign(_currentDirection) * CompassMapping.GetDirectionOffset(_currentDirection, (uint)1); break; case TankAction.MoveBackward: apiAction = API.MoveBackward; var oppsiteDirection = CompassMapping.GetOppositeDirection(_currentDirection); _currentPosition = _currentPosition + CompassMapping.GetDirectionSign(oppsiteDirection) * CompassMapping.GetDirectionOffset(oppsiteDirection, (uint)1); break; case TankAction.TurnLeft: apiAction = API.TurnLeft; _currentDirection = CompassMapping.GetNewDirection( _currentDirection, Compass.West); break; case TankAction.TurnRight: apiAction = API.TurnRight; _currentDirection = CompassMapping.GetNewDirection( _currentDirection, Compass.East); break; case TankAction.TryToShoot: apiAction = () => { if (CurrentState.TargetInSight) { API.FireCannon(); } }; break; default: apiAction = null; break; } if (apiAction == null) { throw new ApplicationException(string.Format("Unknown TankAction: {0}", (int)action)); } TankAPI.ConsumeTurn(apiAction); }