Advertisement
sylviapsh

Fighter Attack

Dec 27th, 2012
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.58 KB | None | 0 0
  1. using System;
  2. class FighterAttack
  3. {
  4.   static void Main()
  5.   {
  6.     //Telerik Academy
  7.     //A rectangular plant P located in east-west direction is under attack by a fighter aircraft flying over it on the west. When the fighter launches a missile we have its coordinates F. It is assumed that the missile's direction is always straight on the west and the missile always hits the target after a fixed distance D in front of the fighter.
  8. //To simplify our model we assume the land is built of square cells of size 1 x 1 located in east-west direction and each cell has integer Cartesian coordinates {x, y}. In this model the plant can be represented by a rectangular area of cell and the missile always hits some of the square cells (inside or outside of the plant).
  9. //When the missile hits a certain cell, the damage over it is 100%, on the cells staying on the left and on the right of it the damage is 50% and in front of it the damage is 75%. The total damage is sum of the separate damages and can exceed 100%.
  10. //You are given the location of the plant P, the location of the fighter F and the distance D. Write a program that calculates the damage over the plant after the attack. Not that the missile could hits the plant partially of fully or can hit some area outside of the plant and cause no damage.
  11. //At the figure below a plant P, a fighter F, a distance D and the missile hit point are shown along with the damage caused over the cells by the hit. Note that some of the damaged cells are outside of the plant and thus the total damage is 225%
  12.     //Your task is to write a program that calculates the total damage caused after the attack over the plant.
  13.  
  14.     int plantX1 = int.Parse(Console.ReadLine()),
  15.         plantY1 = int.Parse(Console.ReadLine()),
  16.         plantX2 = int.Parse(Console.ReadLine()),
  17.         plantY2 = int.Parse(Console.ReadLine()),
  18.         fighterX = int.Parse(Console.ReadLine()),
  19.         fighterY = int.Parse(Console.ReadLine()),
  20.         distance = int.Parse(Console.ReadLine());
  21.  
  22.     int totalDamage = 0;
  23.  
  24.     int plantTop = Math.Max(plantY1, plantY2),
  25.         plantBottom = Math.Min(plantY1, plantY2),
  26.         plantRight = Math.Max(plantX1, plantX2),
  27.         plantLeft = Math.Min(plantX1, plantX2);
  28.  
  29.     //Direct hit
  30.     int currentHitX = fighterX + distance,
  31.         currentHitY = fighterY;
  32.  
  33.     if (currentHitX >= plantLeft && currentHitX <= plantRight && currentHitY >= plantBottom && currentHitY <= plantTop)
  34.     {
  35.       totalDamage += 100;
  36.     }
  37.  
  38.     //Hit in front of the direct hit
  39.     currentHitX = fighterX + distance + 1; // Here only the X coordinate moves with 1. The Y coordinate is the same;
  40.     currentHitY = fighterY;
  41.  
  42.     if (currentHitX >= plantLeft && currentHitX <= plantRight && currentHitY >= plantBottom && currentHitY <= plantTop)
  43.     {
  44.       totalDamage += 75;
  45.     }
  46.  
  47.     //Hit on the left(up) side of the direct hit
  48.     currentHitX = fighterX + distance;
  49.     currentHitY = fighterY + 1; // Here only the Y coordinate moves with 1 upwards. The X coordinate is the same;
  50.  
  51.     if (currentHitX >= plantLeft && currentHitX <= plantRight && currentHitY >= plantBottom && currentHitY <= plantTop)
  52.     {
  53.       totalDamage += 50;
  54.     }
  55.  
  56.     //Hit on the right(down) side of the direct hit
  57.     currentHitX = fighterX + distance;
  58.     currentHitY = fighterY - 1; // Here only the Y coordinate moves with 1 downwards. The X coordinate is the same;
  59.  
  60.     if (currentHitX >= plantLeft && currentHitX <= plantRight && currentHitY >= plantBottom && currentHitY <= plantTop)
  61.     {
  62.       totalDamage += 50;
  63.     }
  64.  
  65.     Console.WriteLine(totalDamage + "%");
  66.   }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement