Advertisement
amphibia89

Bunker Buster

May 1st, 2016
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         int[,] field = ReadFieldValues();
  9.  
  10.         string bombInfo = Console.ReadLine();
  11.         while (bombInfo != "cease fire!")
  12.         {
  13.             string[] bombParameters = bombInfo.Split();
  14.             int targetRow = int.Parse(bombParameters[0]);
  15.             int targetCol = int.Parse(bombParameters[1]);
  16.             int bombPower = bombParameters[2][0];
  17.  
  18.             BombardField(field, targetRow, targetCol, bombPower);
  19.             bombInfo = Console.ReadLine();
  20.         }
  21.  
  22.         int destroyedTargets = CountDestroyedTargers(field);
  23.         double totalTargets = field.GetLength(0) * field.GetLength(1);
  24.         double damageDone = 100 * destroyedTargets / totalTargets;
  25.  
  26.         Console.WriteLine($"Destroyed bunkers: {destroyedTargets}");
  27.         Console.WriteLine($"Damage done: {damageDone:F1} %");        
  28.     }
  29.  
  30.     private static int CountDestroyedTargers(int[,] field)
  31.     {
  32.         int count = 0;
  33.        
  34.         for (int row = 0; row < field.GetLength(0); row++)
  35.             for (int col = 0; col < field.GetLength(1); col++)
  36.                 if (field[row, col] <= 0)
  37.                     count++;
  38.  
  39.         return count;
  40.     }
  41.  
  42.     private static void BombardField(int[,] field, int targetRow, int targetCol, int bombPower)
  43.     {
  44.         field[targetRow, targetCol] -= bombPower;
  45.  
  46.         int secondaryPower = (int)Math.Ceiling(bombPower / 2.0);
  47.         int fieldRowLength = field.GetLength(0);
  48.         int fieldColLength = field.GetLength(1);
  49.        
  50.         // Upper Row Targets
  51.         if (targetRow - 1 >= 0)
  52.         {
  53.             field[targetRow - 1, targetCol] -= secondaryPower;
  54.  
  55.             if (targetCol - 1 >= 0)
  56.                 field[targetRow - 1, targetCol - 1] -= secondaryPower;
  57.            
  58.             if (targetRow - 1 >= 0 && targetCol + 1 < fieldColLength)
  59.                 field[targetRow - 1, targetCol + 1] -= secondaryPower;
  60.         }
  61.  
  62.         // Same Row Adjacent Targets
  63.         if (targetCol - 1 >= 0)
  64.             field[targetRow, targetCol - 1] -= secondaryPower;
  65.  
  66.         if (targetCol + 1 < fieldColLength)
  67.             field[targetRow, targetCol + 1] -= secondaryPower;
  68.        
  69.         // Down Row Targets
  70.         if (targetRow + 1 < fieldRowLength)
  71.         {
  72.             field[targetRow + 1, targetCol] -= secondaryPower;
  73.  
  74.             if (targetCol - 1 >= 0)
  75.                 field[targetRow + 1, targetCol - 1] -= secondaryPower;
  76.  
  77.             if (targetCol + 1 < fieldColLength)
  78.                 field[targetRow + 1, targetCol + 1] -= secondaryPower;
  79.         }
  80.     }
  81.  
  82.     private static int[,] ReadFieldValues()
  83.     {
  84.         int[] dimensions = Console.ReadLine()
  85.             .Split()
  86.             .Select(int.Parse)
  87.             .ToArray();
  88.         int rowLength = dimensions[0];
  89.         int colLength = dimensions[1];
  90.  
  91.         int[,] field = new int[rowLength, colLength];
  92.         for (int row = 0; row < rowLength; row++)
  93.         {
  94.             int[] colValues = Console.ReadLine()
  95.                 .Split()
  96.                 .Select(int.Parse)
  97.                 .ToArray();
  98.  
  99.             for (int col = 0; col < colLength; col++)
  100.             {
  101.                 field[row, col] = colValues[col];
  102.             }
  103.         }
  104.  
  105.         return field;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement