vonko1988

Angry Birds

Jul 16th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.25 KB | None | 0 0
  1. using System;
  2.  
  3. class AngryBits
  4. {
  5.     static int height = 8;
  6.     static int width = 16;
  7.     static int middle = 7;
  8.  
  9.     //initialize the playfield matrix
  10.     static char[,] playField = new char[height, width];
  11.  
  12.     static void Main()
  13.     {
  14.         //fill the playfield with the given values
  15.         for (int row = 0; row < height; row++)
  16.         {
  17.             int currentInput = int.Parse(Console.ReadLine());
  18.             string numberInBinaryString = Convert.ToString(currentInput, 2).PadLeft(16, '0');
  19.  
  20.             for (int col = 0; col < width; col++)
  21.             {
  22.                 playField[row, col] = numberInBinaryString[col];
  23.             }
  24.         }
  25.  
  26.         //print the initial playfield
  27.         //PrintPlayefield(playField);
  28.         //Console.WriteLine();
  29.         //Console.WriteLine();
  30.  
  31.         //now start playing the game
  32.         int totalScore = 0;
  33.         string wonOrNot = "";
  34.  
  35.         for (int col = middle; col >= 0; col--)
  36.         {
  37.             for (int row = 0; row < height; row++)
  38.             {
  39.                 int currentScore = 0;
  40.                 //the bird will start flying
  41.                 if (playField[row, col] == '1')
  42.                 {
  43.                     currentScore = FlyBird(row, col);
  44.                     totalScore += currentScore;
  45.                 }
  46.             }
  47.         }
  48.  
  49.         //PrintPlayefield(playField);
  50.  
  51.         wonOrNot = CheckIfWon(playField);
  52.  
  53.         Console.WriteLine(totalScore + " " + wonOrNot);
  54.     }
  55.  
  56.     //define a method to check if won or not at the end of the game
  57.     static string CheckIfWon(char[,] playField)
  58.     {
  59.         bool won = true;
  60.  
  61.         for (int row = 0; row < height; row++)
  62.         {
  63.             for (int col = middle + 1; col < width; col++)
  64.             {
  65.                 if (playField[row, col] == '1')
  66.                 {
  67.                     won = false;
  68.                 }
  69.             }
  70.         }
  71.  
  72.         if (won)
  73.         {
  74.             return "Yes";
  75.         }
  76.         else
  77.         {
  78.             return "No";
  79.         }
  80.     }
  81.  
  82.     //this method will fly our bird, hit the pigs and reset the playfield squares
  83.     static int FlyBird(int row, int col)
  84.     {
  85.         playField[row, col] = '0';
  86.         int flightSteps = 0;
  87.         int hitPigs = 0;
  88.  
  89.         string flyingDirection;
  90.  
  91.         //determine our initial flying direction
  92.         if (row != 0)
  93.         {
  94.             flyingDirection = "top";
  95.         }
  96.         else
  97.         {
  98.             flyingDirection = "down";
  99.         }
  100.  
  101.         //start the flight
  102.         bool moving = true;
  103.  
  104.         while (col < width && moving)
  105.         {
  106.             switch (flyingDirection)
  107.             {
  108.                 case "top":
  109.                     col++;
  110.                     row--;
  111.                     flightSteps++;
  112.  
  113.                     if (row == 0)
  114.                     {
  115.                         flyingDirection = "down";
  116.                     }
  117.                     if (col == width - 1)
  118.                     {
  119.                         moving = false;
  120.                     }
  121.  
  122.                     //check if a pig is hit
  123.                     if (playField[row, col] == '1')
  124.                     {
  125.                        hitPigs = HitPig(row, col);
  126.                        //Console.WriteLine(hitPigs);
  127.                        moving = false;
  128.                     }
  129.  
  130.                     break;
  131.  
  132.                 case "down":
  133.                     col++;
  134.                     row++;
  135.                     flightSteps++;
  136.  
  137.                     if (row == height - 1)
  138.                     {
  139.                         moving = false;
  140.                     }
  141.                     if (col == width - 1)
  142.                     {
  143.                         moving = false;
  144.                     }
  145.  
  146.                     //check if a pig is hit
  147.                     if (playField[row, col] == '1')
  148.                     {
  149.                        hitPigs = HitPig(row, col);
  150.                        //Console.WriteLine(hitPigs);
  151.                        moving = false;
  152.                     }
  153.  
  154.                     break;
  155.             }
  156.         }
  157.  
  158.         //Console.WriteLine("flight steps: {0}", flightSteps);
  159.         int score = flightSteps * hitPigs;
  160.        
  161.         return score;
  162.     }
  163.  
  164.     //define this method when a pig is hit
  165.     static int HitPig(int row, int col)
  166.     {
  167.         int pigsHit = 0;
  168.         for (int newRow = row - 1; newRow < row - 1 + 3; newRow++)
  169.         {
  170.             for (int newCol = col - 1; newCol < col - 1 + 3; newCol++)
  171.             {
  172.                 if (newCol >= 0 && newCol < width && newRow >= 0 && newRow < height)
  173.                 {
  174.                     if (playField[newRow, newCol] == '1')
  175.                     {
  176.                         playField[newRow, newCol] = '0';
  177.                         pigsHit++;
  178.                     }
  179.                 }
  180.             }
  181.         }
  182.  
  183.         return pigsHit;
  184.     }
  185.  
  186.     //define a method that can print the matrix any time needed
  187.     static void PrintPlayefield(char[,] playField)
  188.     {
  189.         for (int row = 0; row < height; row++)
  190.         {
  191.             for (int col = 0; col < width; col++)
  192.             {
  193.                 Console.Write(playField[row, col].ToString().PadLeft(3, ' '));
  194.             }
  195.             Console.WriteLine();
  196.         }
  197.     }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment