Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class AngryBits
- {
- static int height = 8;
- static int width = 16;
- static int middle = 7;
- //initialize the playfield matrix
- static char[,] playField = new char[height, width];
- static void Main()
- {
- //fill the playfield with the given values
- for (int row = 0; row < height; row++)
- {
- int currentInput = int.Parse(Console.ReadLine());
- string numberInBinaryString = Convert.ToString(currentInput, 2).PadLeft(16, '0');
- for (int col = 0; col < width; col++)
- {
- playField[row, col] = numberInBinaryString[col];
- }
- }
- //print the initial playfield
- //PrintPlayefield(playField);
- //Console.WriteLine();
- //Console.WriteLine();
- //now start playing the game
- int totalScore = 0;
- string wonOrNot = "";
- for (int col = middle; col >= 0; col--)
- {
- for (int row = 0; row < height; row++)
- {
- int currentScore = 0;
- //the bird will start flying
- if (playField[row, col] == '1')
- {
- currentScore = FlyBird(row, col);
- totalScore += currentScore;
- }
- }
- }
- //PrintPlayefield(playField);
- wonOrNot = CheckIfWon(playField);
- Console.WriteLine(totalScore + " " + wonOrNot);
- }
- //define a method to check if won or not at the end of the game
- static string CheckIfWon(char[,] playField)
- {
- bool won = true;
- for (int row = 0; row < height; row++)
- {
- for (int col = middle + 1; col < width; col++)
- {
- if (playField[row, col] == '1')
- {
- won = false;
- }
- }
- }
- if (won)
- {
- return "Yes";
- }
- else
- {
- return "No";
- }
- }
- //this method will fly our bird, hit the pigs and reset the playfield squares
- static int FlyBird(int row, int col)
- {
- playField[row, col] = '0';
- int flightSteps = 0;
- int hitPigs = 0;
- string flyingDirection;
- //determine our initial flying direction
- if (row != 0)
- {
- flyingDirection = "top";
- }
- else
- {
- flyingDirection = "down";
- }
- //start the flight
- bool moving = true;
- while (col < width && moving)
- {
- switch (flyingDirection)
- {
- case "top":
- col++;
- row--;
- flightSteps++;
- if (row == 0)
- {
- flyingDirection = "down";
- }
- if (col == width - 1)
- {
- moving = false;
- }
- //check if a pig is hit
- if (playField[row, col] == '1')
- {
- hitPigs = HitPig(row, col);
- //Console.WriteLine(hitPigs);
- moving = false;
- }
- break;
- case "down":
- col++;
- row++;
- flightSteps++;
- if (row == height - 1)
- {
- moving = false;
- }
- if (col == width - 1)
- {
- moving = false;
- }
- //check if a pig is hit
- if (playField[row, col] == '1')
- {
- hitPigs = HitPig(row, col);
- //Console.WriteLine(hitPigs);
- moving = false;
- }
- break;
- }
- }
- //Console.WriteLine("flight steps: {0}", flightSteps);
- int score = flightSteps * hitPigs;
- return score;
- }
- //define this method when a pig is hit
- static int HitPig(int row, int col)
- {
- int pigsHit = 0;
- for (int newRow = row - 1; newRow < row - 1 + 3; newRow++)
- {
- for (int newCol = col - 1; newCol < col - 1 + 3; newCol++)
- {
- if (newCol >= 0 && newCol < width && newRow >= 0 && newRow < height)
- {
- if (playField[newRow, newCol] == '1')
- {
- playField[newRow, newCol] = '0';
- pigsHit++;
- }
- }
- }
- }
- return pigsHit;
- }
- //define a method that can print the matrix any time needed
- static void PrintPlayefield(char[,] playField)
- {
- for (int row = 0; row < height; row++)
- {
- for (int col = 0; col < width; col++)
- {
- Console.Write(playField[row, col].ToString().PadLeft(3, ' '));
- }
- Console.WriteLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment