Advertisement
Svetli0o

AngryBits

Mar 31st, 2014
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.55 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 Program
  8. {
  9.     static void Main(string[] args)
  10.     {
  11.         int[,] matrix = new int[8, 16];
  12.         int pigsCount = 0;
  13.         //Filling the matrix and counting how many pigs needs to be destroyed to win the game
  14.         for (int i = 0; i < 8; i++)
  15.         {
  16.             int number = int.Parse(Console.ReadLine());
  17.             for (int j = 0; j < 16; j++)
  18.             {
  19.                 matrix[i, j] = Convert.ToInt32((number >> j) & 1);
  20.                 if (j < 8 && (matrix[i, j] == 1))
  21.                 {
  22.                     pigsCount++;
  23.                 }
  24.             }
  25.         }
  26.         int totalCount = 0; //holds the total points made
  27.         int destroyedPigs = 0; //holds the count of the destroyed pigs
  28.         //one loop to iterate through the columns of the left side of the matrix (where the birds are)
  29.         for (int col = 8; col < 16; col++)
  30.         {
  31.             //one loop to iterate through the rows of the left side of the matrix and to check
  32.             //cell by cell if there is a bird
  33.             for (int row = 0; row < 8; row++)
  34.             {
  35.                 //if there is a bird (current cell is equal to 1)
  36.                 if (matrix[row, col] == 1)
  37.                 {
  38.                     int cellRow = row; //used to copy row so we can change the value
  39.                     int cellCol = col; //used to copy col so we can change the value
  40.                     matrix[row, col] = 0; //we make the bird 0 because we have to remove it from the game
  41.                     string direction = "up"; //specify the direction up or down
  42.                     int counter = 0; //count how many position we have moved the bird
  43.                     //the while(true) loop is used to "throw" the bird (increase it's row and
  44.                     //col indexes until 1 is found or until the bird hits the bottom or the side of the matrix
  45.                     while (true)
  46.                     {
  47.                         //if we find a pig on the right side of the matrix we go into the body of the if statement
  48.                         if ((matrix[cellRow, cellCol] == 1) && cellCol < 8)
  49.                         {
  50.                             matrix[cellRow, cellCol] = 0; //we destroy the pig by setting it to zero
  51.                             destroyedPigs++;
  52.                             int multiplyer = 1; //since the formula is calculated by destroyed pigs * the length of the flight we need a variable to see how many pigs we are destroying
  53.                             //with this loop we count all the pigs around the pig that we just hit
  54.                             //with all the breaks inside the loop we make sure that won't go of matrix and we will avoid index out of range exception
  55.                             for (int i = cellCol + 1; i >= cellCol - 1; i--)
  56.                             {
  57.                                 if (i == 8)
  58.                                 {
  59.                                     i = 7;
  60.                                 }
  61.                                 for (int j = cellRow - 1; j <= cellRow + 1; j++)
  62.                                 {
  63.                                     if (j == -1)
  64.                                     {
  65.                                         j = 0;
  66.                                     }
  67.                                     //since we counted 2 positions out of the nine we skip them with continue
  68.                                     if ((i == cellCol && j == cellRow) || (i == cellCol + 1 && j == cellRow - 1))
  69.                                     {
  70.                                         if (j + 1 == 8) break;
  71.                                         if (i - 1 == -1) break;
  72.                                         continue;
  73.                                     }
  74.                                     else
  75.                                     {
  76.                                         //if we find a pig around the pig that we destroyed we set it to 0 and we count it
  77.                                         if (matrix[j, i] == 1)
  78.                                         {
  79.                                             matrix[j, i] = 0;
  80.                                             destroyedPigs++;
  81.                                             multiplyer += 1;
  82.                                         }
  83.                                     }
  84.                                     if (j + 1 == 8) break;
  85.                                 }
  86.                                 if (i - 1 == -1) break;
  87.                             }
  88.                             //after we check how many pigs we have around our destroyed pig we put them in the variable multiplyer and we multiply it by the positions changed during the fly
  89.                             totalCount += multiplyer * counter;
  90.                             counter = 0;
  91.                             break; //after we destroy the pig and pigs around it we break the loop and after that we go back to find an other bird
  92.                         }
  93.                         if (direction == "up") //if the bird reaches the top of the matrix we change it's direction to down
  94.                         {
  95.                             if (cellRow == 0)
  96.                             {
  97.                                 direction = "down";
  98.                                 cellCol--;
  99.                                 cellRow++;
  100.                             }
  101.                             else
  102.                             {
  103.                                 cellRow--;
  104.                                 cellCol--;
  105.                             }
  106.                         }
  107.                         else if (direction == "down") //if the bird is flying down and it reaches the right or the bottom of the matrix we break the loop
  108.                         {
  109.                             if (cellCol == 0 || cellRow == 7)
  110.                             {
  111.                                 break;
  112.                             }
  113.                             cellRow++;
  114.                             cellCol--;
  115.                         }
  116.                         counter++; //after each movement we make with the bird we increase the counter
  117.                     }
  118.                 }
  119.             }
  120.         }
  121.         Console.Write(totalCount);
  122.         if (destroyedPigs == pigsCount) // if we have destroyed as many pigs as there are in the game we say they we won
  123.         {
  124.             Console.WriteLine(" Yes");
  125.         }
  126.         else
  127.         {
  128.             Console.WriteLine(" No");    
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement