DonJabone

TowerOfDoom

Dec 7th, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.77 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.  
  8. class FifthTask
  9. {
  10.     public static int firstPossition;
  11.     public static int secondPossition;
  12.     public static int survivedWarriors = 0;
  13.     public static bool firstFloor = false;
  14.     public static int[,] matrix = new int[8, 8];
  15.     static void Main()
  16.     {
  17.         int totalWarriors = 0;
  18.         int finalResult = 0;
  19.         for (int i = 0; i < 8; i++)
  20.         {
  21.             int tempNumber = int.Parse(Console.ReadLine());
  22.             for (int k = 0; k < 8; k++)
  23.             {
  24.                 int mask = 1 << k;
  25.                 int nAndMask = tempNumber & mask;
  26.                 int bit = nAndMask >> k;
  27.                 if (bit == 1)
  28.                 {
  29.                     totalWarriors++;
  30.                 }
  31.                 matrix[i, 7-k] = bit;
  32.                
  33.             }      
  34.  
  35.         }
  36.         while (true)
  37.         {
  38.             string command = Console.ReadLine();
  39.             if (command == "end")
  40.             {
  41.                 break;
  42.             }
  43.             firstPossition = int.Parse(Console.ReadLine());
  44.             secondPossition = int.Parse(Console.ReadLine());
  45.  
  46.             switch (command)
  47.             {
  48.                 case "select": Select(firstPossition, secondPossition); break;
  49.                 case "kill": kill(firstPossition, secondPossition); break;
  50.                 case "move": move(firstPossition, secondPossition); break;
  51.                 default: break;
  52.             }
  53.         }
  54.         for (int i = 0; i < matrix.GetLength(0); i++)
  55.         {
  56.             int tempNumber = 0;
  57.             for (int k = 0; k < matrix.GetLength(1); k++)
  58.             {
  59.                 if (matrix[i, k] == 1)
  60.                 {
  61.                     int p = 7-k;
  62.                     int mask = 1 << p;        
  63.                     tempNumber = tempNumber | mask;    
  64.  
  65.                     survivedWarriors++;
  66.                 }
  67.             }
  68.             finalResult += tempNumber;
  69.         }
  70.         Console.WriteLine(totalWarriors);
  71.         Console.WriteLine(survivedWarriors);
  72.         Console.WriteLine(finalResult);
  73.     }
  74.  
  75.     private static void move(int firstPossition, int secondPossition)
  76.     {
  77.         bool warriorOnLeft = false;
  78.         bool warriorOnRight = false;
  79.         if (firstPossition < 0 || firstPossition > 7 || secondPossition < 0 || secondPossition > 7)
  80.         {
  81.             return;
  82.         }
  83.         if (secondPossition != 0)
  84.         {
  85.             if (matrix[firstPossition, secondPossition - 1] == 1)
  86.             {
  87.                 warriorOnLeft = true;
  88.             }
  89.         }
  90.         if (secondPossition != 7)
  91.         {
  92.             if (matrix[firstPossition, secondPossition + 1] == 1)
  93.             {
  94.                 warriorOnRight = true;
  95.             }
  96.         }
  97.         if (warriorOnLeft || warriorOnRight || matrix[firstPossition, secondPossition] == 1)
  98.         {
  99.             firstFloor = false;
  100.             return;
  101.         }
  102.         else
  103.         {
  104.             matrix[firstPossition, secondPossition] = 1;
  105.         }
  106.        
  107.         firstFloor = false;
  108.  
  109.     }
  110.  
  111.     private static void kill(int firstPossition, int secondPossition)
  112.     {
  113.         bool warriorOnLeft = false;
  114.         bool warriorOnRight = false;
  115.         if (firstPossition < 0 || firstPossition > 7 || secondPossition < 0 || secondPossition > 7)
  116.         {
  117.             if (firstFloor)
  118.             {
  119.                 survivedWarriors++;
  120.             }
  121.             return;
  122.         }
  123.         if (secondPossition != 0)
  124.         {
  125.             if (matrix[firstPossition, secondPossition - 1] == 1)
  126.             {
  127.                 warriorOnLeft = true;
  128.             }
  129.         }
  130.         if (secondPossition != 7)
  131.         {
  132.             if (matrix[firstPossition, secondPossition + 1] == 1)
  133.             {
  134.                 warriorOnRight = true;
  135.             }
  136.         }
  137.         if (warriorOnLeft && warriorOnRight)
  138.         {
  139.             matrix[firstPossition, secondPossition] = 0;
  140.         }
  141.         else if (warriorOnLeft && !warriorOnRight)
  142.         {
  143.             matrix[firstPossition, secondPossition] = 0;
  144.             matrix[firstPossition, secondPossition - 1] = 0;
  145.         }
  146.         else if (!warriorOnLeft && warriorOnRight)
  147.         {
  148.             matrix[firstPossition, secondPossition] = 0;
  149.             matrix[firstPossition, secondPossition + 1] = 0;
  150.         }
  151.         else if (!warriorOnLeft && !warriorOnRight)
  152.         {
  153.             matrix[firstPossition, secondPossition] = 1;
  154.         }
  155.         firstFloor = false;
  156.     }
  157.  
  158.     private static void Select(int first ,int second)
  159.     {
  160.         matrix[first, second] = 0;
  161.         if (first == 0 || first == 1)
  162.         {
  163.             firstFloor = true;
  164.  
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment