Advertisement
Guest User

SpaceshipFateCSharp

a guest
Apr 14th, 2012
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.50 KB | None | 0 0
  1. struct Point
  2. {
  3.     public Point(int x, int y) { X = x; Y = y; }
  4.     public int X, Y;
  5. }
  6.  
  7. class OrbitException : Exception { }
  8. class CrushException : OrbitException { }
  9.  
  10. List<Point> planets = new List<Point>();
  11. List<string> history = new List<string>();
  12. Point rocket, velocity, universeSize;
  13.  
  14. public void Main()
  15. {
  16.     var i = 0;
  17.     for (var line = Console.ReadLine(); line != ""; line = Console.ReadLine())
  18.     {
  19.         universeSize.X = line.Select((c, j) => CreateObject(c, i, j)).Count();
  20.         i++;
  21.     }
  22.  
  23.     universeSize.Y = i;
  24.  
  25.     var x = new Action<string>(Console.WriteLine);
  26.  
  27.     try
  28.     {
  29.         while (true)
  30.         {
  31.             ApplyGravity();
  32.             Move();
  33.         }
  34.     }
  35.     catch (CrushException) { x("crush"); }
  36.     catch (OrbitException) { x("orbit"); }
  37.     catch { x("escape"); }
  38. }
  39.  
  40. Point CreateObject(char c, int i, int j)
  41. {
  42.     if (c == '@')
  43.         planets.Add(new Point(j, i));
  44.     else if (c != '.')
  45.     {
  46.         rocket = new Point(j, i);
  47.         velocity = (c == 'v' ? new Point(0, 1) : c == '<' ? new Point(-1, 0) : c == '^' ? new Point(0, -1) : new Point(1, 0));
  48.     }
  49.  
  50.     return universeSize;
  51. }
  52.  
  53. private void ApplyGravity()
  54. {
  55.     planets.ForEach(planet =>
  56.     {
  57.         var dx = planet.X - rocket.X;
  58.         var dy = planet.Y - rocket.Y;
  59.  
  60.         if (dx < 2 && dx > -2 && dy < 2 && dy > -2)
  61.         {
  62.             velocity.X += dx; velocity.Y += dy;
  63.         }
  64.     });
  65. }
  66.  
  67. private void Move()
  68. {
  69.     // check if orbit; save current context
  70.     var context = string.Join(".", rocket.X, rocket.Y, velocity.X, velocity.Y);
  71.     if (history.Contains(context))
  72.         throw new OrbitException();
  73.  
  74.     history.Add(context);
  75.  
  76.     // colision check
  77.     var position = new Point(rocket.X, rocket.Y);
  78.  
  79.     var k = new[] { velocity.X, velocity.Y };
  80.     var min = k.Min();
  81.     var max = k.Max();
  82.  
  83.     for (int i = 1; i <= max; i++)
  84.     {
  85.         if ((i * min) % max == 0)
  86.         {
  87.             var p1 = new Point(
  88.                 position.X + (velocity.X == max ? i : i * min / max),
  89.                 position.Y + (velocity.Y == max ? i : i * min / max));
  90.  
  91.             if (planets.Contains(p1))
  92.                 throw new CrushException();
  93.         }
  94.     }
  95.  
  96.     // apply move
  97.     rocket.X += velocity.X;
  98.     rocket.Y += velocity.Y;
  99.  
  100.     // bounds check
  101.     if (rocket.X < 0 || rocket.X >= universeSize.X || rocket.Y < 0 || rocket.Y >= universeSize.Y)
  102.         throw new Exception(); // using plain Exception for 'escape' condition
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement