Advertisement
Guest User

Untitled

a guest
Mar 19th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.20 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Warheads
  4. {
  5.     class Program
  6.     {
  7.         static int[,] circuit = new int[16, 16];
  8.  
  9.         static void Main()
  10.         {
  11.             ReadInput();
  12.             ReadCommands();
  13.         }
  14.  
  15.         private static void ReadCommands()
  16.         {
  17.             string status = "intact";
  18.             while (status == "intact")
  19.             {
  20.                 string command = Console.ReadLine();
  21.                 switch (command)
  22.                 {
  23.                     case "hover":
  24.                         {
  25.                             int line = int.Parse(Console.ReadLine());
  26.                             int col = int.Parse(Console.ReadLine());
  27.                             Console.WriteLine(CellStatus(line, col));
  28.                             break;
  29.                         }
  30.                     case "operate":
  31.                         {
  32.                             int line = int.Parse(Console.ReadLine());
  33.                             int col = int.Parse(Console.ReadLine());
  34.                             if (circuit[line, col] == 1)
  35.                             {
  36.                                 Console.WriteLine("missed");
  37.                                 Console.WriteLine(GetShapes("blue") + GetShapes("red"));
  38.                                 Console.WriteLine("BOOM");
  39.                                 status = "done";
  40.                             }
  41.                             else if (IsFigure(line, col))
  42.                             {
  43.                                 RemoveFigure(line, col);
  44.                             }
  45.                             break;
  46.                         }
  47.                     case "cut":
  48.                         {
  49.                             string section = Console.ReadLine();
  50.                             int sectionFigures = GetShapes(section);
  51.                             if (sectionFigures > 0)
  52.                             {
  53.                                 Console.WriteLine(sectionFigures);
  54.                                 Console.WriteLine("BOOM");
  55.                             }
  56.                             else
  57.                             {
  58.                                 string otherSection;
  59.                                 if (section == "red")
  60.                                 {
  61.                                     otherSection = "blue";
  62.                                 }
  63.                                 else
  64.                                 {
  65.                                     otherSection = "red";
  66.                                 }
  67.                                 Console.WriteLine(GetShapes(otherSection));
  68.                                 Console.WriteLine("disarmed");
  69.                             }
  70.                             status = "done";
  71.                             break;
  72.                         }
  73.                     default:
  74.                         break;
  75.                 }
  76.             }
  77.         }
  78.  
  79.         private static int GetShapes(string sector)
  80.         {
  81.             int startCol;
  82.             int endCol;
  83.             if (sector == "red")
  84.             {
  85.                 startCol = 1;
  86.                 endCol = 7;
  87.             }
  88.             else
  89.             {
  90.                 startCol = 8;
  91.                 endCol = 14;
  92.             }
  93.  
  94.             int figureCount = 0;
  95.             for (int i = 1; i < 15; i++)
  96.             {
  97.                 for (int p = startCol; p <= endCol; p++)
  98.                 {
  99.                     if (IsFigure(i, p))
  100.                     {
  101.                         figureCount++;
  102.                     }
  103.                 }
  104.             }
  105.             return figureCount;
  106.         }
  107.  
  108.         private static void RemoveFigure(int line, int col)
  109.         {
  110.             for (int i = line - 1; i <= line + 1; i++)
  111.             {
  112.                 for (int p = col - 1; p <= col + 1; p++)
  113.                 {
  114.                     circuit[i, p] = 0;
  115.                 }
  116.             }
  117.         }
  118.  
  119.         private static string CellStatus(int line, int col)
  120.         {
  121.             if (circuit[line, col] == 1)
  122.             {
  123.                 return "*";
  124.             }
  125.             else
  126.             {
  127.                 return "-";
  128.             }
  129.         }
  130.  
  131.         private static bool IsFigure(int line, int col)
  132.         {
  133.             if (line == 0 || line == 15 || col == 0 || col == 15)
  134.             {
  135.                 return false;
  136.             }
  137.             bool isFigure = circuit[line, col] == 0 &&
  138.                 circuit[line - 1, col] == 1 &&
  139.                  circuit[line - 1, col + 1] == 1 &&
  140.                  circuit[line, col + 1] == 1 &&
  141.                  circuit[line + 1, col + 1] == 1 &&
  142.                  circuit[line + 1, col] == 1 &&
  143.                  circuit[line + 1, col - 1] == 1 &&
  144.                  circuit[line, col - 1] == 1 &&
  145.                  circuit[line - 1, col - 1] == 1;
  146.             return isFigure;
  147.         }
  148.  
  149.         private static void ReadInput()
  150.         {
  151.             for (int line = 0; line < 16; line++)
  152.             {
  153.                 char[] cells = Console.ReadLine().ToCharArray();
  154.                 for (int col = 0; col < 16; col++)
  155.                 {
  156.                     circuit[line, col] = int.Parse(cells[col].ToString());
  157.                 }
  158.             }
  159.         }
  160.     }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement