Advertisement
vlad0

05.Angry Bits bgcoder29dec2012

Dec 31st, 2012
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace AngryBits
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int[,] grid = new int[8, 16];
  13.             bool noHit = true;
  14.             bool movingUp = true;
  15.             int rowThrow, colThrow;
  16.             int endOfFlight = 0;
  17.             int pigs = 0;
  18.             int score = 0;
  19.             string win = "Yes";
  20.             for (int rows = 0; rows < 8; rows++)
  21.             {
  22.                 int N = int.Parse(Console.ReadLine());
  23.  
  24.                 char[] charArray = Convert.ToString(N, 2).ToCharArray();
  25.                 int columns = charArray.Length - 1;
  26.                 foreach (char bits in charArray)
  27.                 {
  28.                     grid[rows, columns] = bits - '0';
  29.                     columns--;
  30.                 }
  31.             }
  32.            
  33.             //SEARCH FOR A BIRD FROM RIGHT TO LEFT
  34.  
  35.             for (int cols = 8; cols <= 15; cols++)
  36.             {
  37.                 for (int rows = 0; rows < 8; rows++)
  38.                 {
  39.                     rowThrow = rows;
  40.                     colThrow = cols;
  41.                     endOfFlight = 0;
  42.                     pigs = 0;
  43.                     noHit = true;
  44.                     movingUp = true;
  45.  
  46.  
  47.                     //Check for a bird every single cell
  48.                     if (grid[rowThrow, colThrow] == 1)
  49.                     {
  50.                         //we are throwing the bird and making it a zero
  51.                         grid[rowThrow, colThrow] = 0;
  52.  
  53.                         while (noHit)
  54.                         {
  55.  
  56.                             if (rowThrow == 0)
  57.                             {
  58.                                 movingUp = false;
  59.                             }
  60.                             else if (rowThrow == 7)
  61.                             {
  62.                                 movingUp = true;
  63.                             }
  64.  
  65.                             if (movingUp == true)
  66.                             {
  67.                                 rowThrow--;
  68.                             }
  69.                             else
  70.                             {
  71.                 //solve for OutOfRange
  72.                                 if (rowThrow <= 6)
  73.                                 {
  74.                                     rowThrow++;
  75.                                 }
  76.                             }
  77.                             if (colThrow > 0)
  78.                             {
  79.                                 colThrow--;
  80.                             }
  81.                             else
  82.                             {
  83.                                 noHit = false;
  84.                             }
  85.  
  86.                             endOfFlight++;
  87.  
  88.                             if (grid[rowThrow, colThrow] == 1)
  89.                             {
  90.                                 pigs++;
  91.                                 grid[rowThrow, colThrow] = 2;
  92.                                 int pigsRow = 0;
  93.                                 if (rowThrow - 1 >= 0)
  94.                                 {
  95.                                     pigsRow = rowThrow - 1;
  96.                                 }
  97.                                 else
  98.                                 {
  99.                                     pigsRow = rowThrow;
  100.                                 }
  101.                                 int pigsCol;
  102.                                 pigsCol = colThrow + 1;
  103.  
  104.  
  105.                                 for (int i = 0; i < (pigsRow == rowThrow ? 2 : 3) && pigsRow + i <= 7; i++)
  106.                                 {
  107.                                     for (int j = 0; j < 3 && pigsCol - j >= 0; j++)
  108.                                     {
  109.                                         if (grid[pigsRow + i, pigsCol - j] == 1)
  110.                                         {
  111.                                             pigs++;
  112.                                             grid[pigsRow + i, pigsCol - j] = 0;
  113.                                         }
  114.  
  115.                                     }
  116.                                 }
  117.  
  118.                                 noHit = false;
  119.                             }
  120.  
  121.                             grid[rowThrow, colThrow] = 2;
  122.  
  123.                             //stop when hit the end ot the bottom of the grid
  124.                             if (colThrow == 0 || rowThrow == 7)
  125.                             {
  126.                                 noHit = false;
  127.                             }
  128.  
  129.                         }
  130.                         score += (endOfFlight * pigs);
  131.                     }
  132.                 }
  133.             }
  134.             for (int i = 0; i < 8; i++)
  135.             {
  136.                 for (int j = 8; j >= 0; j--)
  137.                 {
  138.                     if (grid[i, j] == 1)
  139.                     {
  140.                         win = "No";
  141.                         break;
  142.                     }
  143.  
  144.                 }
  145.             }
  146.  
  147.             Console.Write("{0} {1}", score, win);
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement