psotirov

Ship Damage

Dec 10th, 2012
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. using System;
  2.  
  3. class Ship
  4. {
  5.     int xLeft;
  6.     int yTop;
  7.     int xRight;
  8.     int yBottom;
  9.  
  10.     public Ship(int x1, int y1, int x2, int y2)
  11.     {
  12.         xLeft = Math.Min(x1, x2);
  13.         xRight = Math.Max(x1, x2);
  14.         yTop = Math.Max(y1, y2);
  15.         yBottom = Math.Min(y1, y2);
  16.     }
  17.  
  18.     public int Damage(int x, int y)
  19.     {
  20.         if (x < xLeft || x > xRight || y < yBottom || y > yTop) return 0; // no damage
  21.         if (x == xLeft || x == xRight)
  22.         {
  23.             if (y == yBottom || y == yTop) return 25; // 25% damage - corner was hit
  24.             else return 50; // 50% damage - side was hit
  25.         }
  26.  
  27.         if (y == yBottom || y == yTop)
  28.         {
  29.             if (x == xLeft || x == xRight) return 25; // 25% damage - corner was hit
  30.             else return 50; // 50% damage - side was hit
  31.         }
  32.  
  33.         return 100; // otherwise the hit is inside the body - 100%
  34.     }
  35. }
  36.  
  37. class ShipDamage
  38. {
  39.     static void Main()
  40.     {
  41.         Ship theShip = new Ship(    int.Parse(Console.ReadLine()), // reads the ship's coordinates: Sx1,
  42.                                     int.Parse(Console.ReadLine()), // Sy1,
  43.                                     int.Parse(Console.ReadLine()), // Sx2,
  44.                                     int.Parse(Console.ReadLine())  // Sy2 (and sort them!)
  45.                                );
  46.  
  47.         int Hy = int.Parse(Console.ReadLine()); // reads the horizon line
  48.         int Damage = 0;
  49.  
  50.         for (int i = 0; i < 3; i++)
  51.         {
  52.             int Cx = int.Parse(Console.ReadLine()); // reads the x coordinate of i-th cathapult
  53.             int Cy = int.Parse(Console.ReadLine()); // reads the y coordinate of i-th cathapult
  54.             Damage += theShip.Damage(Cx, Hy + (Hy - Cy)); // transfers the Cy coordinate at the opposite side of horizon
  55.         }
  56.  
  57.         Console.WriteLine("{0}%", Damage);
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment