zdravko7

[C# Advanced Exam] Bunker Buster

Jul 19th, 2015
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.47 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. class BunkerBuster
  8. {
  9.     static void Main(string[] args)
  10.     {
  11.         int[] size = Console.ReadLine().Split().Select(p => int.Parse(p)).ToArray();
  12.  
  13.         int rows = size[0];
  14.         int cols = size[1];
  15.  
  16.         int[,] matrix = new int[rows, cols];
  17.  
  18.         for (int row = 0; row < rows; row++)
  19.         {
  20.             int[] currentRow = Console.ReadLine().Split(new char[]{' '}, StringSplitOptions.RemoveEmptyEntries).Select(p => int.Parse(p)).ToArray();
  21.            
  22.             for (int col = 0; col < cols; col++)
  23.             {
  24.                 matrix[row, col] = currentRow[col];
  25.             }
  26.         }
  27.         //input checked
  28.  
  29.         while (true)
  30.         {
  31.             string[] input = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  32.  
  33.             if (input[0] + " " + input[1] == "cease fire!")
  34.                 break;
  35.  
  36.             int bombRow = int.Parse(input[0]);
  37.             int bombCol = int.Parse(input[1]);
  38.             char symbol = char.Parse(input[2]);
  39.             int damage = (int)symbol;
  40.             int halfDamage = (int)Math.Ceiling(((decimal)damage)/2);
  41.  
  42.             //bombardment
  43.             try
  44.             {
  45.                 matrix[bombRow, bombCol] -= damage;
  46.             }
  47.             catch (Exception) { }
  48.  
  49.             try
  50.             {
  51.                 matrix[bombRow-1, bombCol-1] -= halfDamage;
  52.             }
  53.             catch (Exception) { }
  54.  
  55.             try
  56.             {
  57.                 matrix[bombRow - 1, bombCol] -= halfDamage;
  58.             }
  59.             catch (Exception) { }
  60.  
  61.             try
  62.             {
  63.                 matrix[bombRow - 1, bombCol + 1] -= halfDamage;
  64.             }
  65.             catch (Exception) { }
  66.  
  67.             try
  68.             {
  69.                 matrix[bombRow, bombCol-1] -= halfDamage;
  70.             }
  71.             catch (Exception) { }
  72.  
  73.             try
  74.             {
  75.                 matrix[bombRow, bombCol + 1] -= halfDamage;
  76.             }
  77.             catch (Exception) { }
  78.  
  79.             try
  80.             {
  81.                 matrix[bombRow + 1, bombCol - 1] -= halfDamage;
  82.             }
  83.             catch (Exception) { }
  84.  
  85.             try
  86.             {
  87.                 matrix[bombRow + 1, bombCol] -= halfDamage;
  88.             }
  89.             catch (Exception) { }
  90.  
  91.             try
  92.             {
  93.                 matrix[bombRow + 1, bombCol + 1] -= halfDamage;
  94.  
  95.             }
  96.             catch (Exception) { }
  97.  
  98.  
  99.         }
  100.  
  101.         int destroyedCells = 0;
  102.         //checks for destroyed cells
  103.         for (int row = 0; row < matrix.GetLength(0); row++)
  104.         {
  105.             for (int col = 0; col < matrix.GetLength(1); col++)
  106.             {
  107.                 if (matrix[row,col] <= 0)
  108.                     destroyedCells++;
  109.             }
  110.         }
  111.  
  112.  
  113.         decimal damageDone = ((decimal)destroyedCells / (rows * cols))*100;
  114.  
  115.  
  116.         //PrintMatrix(matrix);
  117.         Console.WriteLine("Destroyed bunkers: {0}", destroyedCells);
  118.         Console.WriteLine("Damage done: {0:F1} %", damageDone);
  119.     }
  120.  
  121.     public static void PrintMatrix(int[,] matrix)
  122.     {
  123.         for (int row = 0; row < matrix.GetLength(0); row++)
  124.         {
  125.             for (int col = 0; col < matrix.GetLength(1); col++)
  126.             {
  127.                 Console.Write(matrix[row, col] + " ");
  128.             }
  129.  
  130.             Console.WriteLine();
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment