-Annie-

TargetPractice

Jun 17th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.16 KB | None | 0 0
  1. namespace TargetPractice
  2. {
  3.     using System;
  4.     using System.Linq;
  5.  
  6.     public class TargetPractice
  7.     {
  8.         public static void Main()
  9.         {
  10.             int[] matrixDimentions = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.             int numberOfRows = matrixDimentions[0];
  12.             int numberOfColumns = matrixDimentions[1];
  13.             char[,] matrix = new char[numberOfRows, numberOfColumns];
  14.             string snake = Console.ReadLine();
  15.             int[] shotsImpactRowColumnRadius = Console.ReadLine().Split().Select(int.Parse).ToArray();
  16.             int impactRow = shotsImpactRowColumnRadius[0];
  17.             int impactColumn = shotsImpactRowColumnRadius[1];
  18.             int impactRadius = shotsImpactRowColumnRadius[2];
  19.  
  20.             FillMatrix(numberOfRows, numberOfColumns, snake, matrix);
  21.             FireAShot(matrix, impactRow, impactColumn, impactRadius);
  22.  
  23.             for (int col = 0; col < matrix.GetLength(1); col++)
  24.             {
  25.                 RunGravity(matrix, col);
  26.             }
  27.  
  28.             PrintMatrix(matrix);
  29.         }
  30.  
  31.         private static void RunGravity(char[,] matrix, int col)
  32.         {
  33.             while (true)
  34.             {
  35.                 bool hasFallen = false;
  36.  
  37.                 for (int row = 1; row < matrix.GetLength(0); row++)
  38.                 {
  39.                     char topChar = matrix[row - 1, col];
  40.                     char currentChar = matrix[row, col];
  41.  
  42.                     if (currentChar == ' ' && topChar != ' ')
  43.                     {
  44.                         matrix[row, col] = topChar;
  45.                         matrix[row - 1, col] = ' ';
  46.                         hasFallen = true;
  47.                     }
  48.                 }
  49.  
  50.                 if (!hasFallen)
  51.                 {
  52.                     break;
  53.                 }
  54.             }
  55.         }
  56.  
  57.         private static void FireAShot(char[,] matrix, int impactRow, int impactColumn, int impactRadius)
  58.         {
  59.             //(x - center_x)^2 + (y - center_y)^2 <= radius^2
  60.             //x - columns, y-rows
  61.             for (int row = 0; row < matrix.GetLength(0); row++)
  62.             {
  63.                 for (int column = 0; column < matrix.GetLength(1); column++)
  64.                 {
  65.                     if ((column - impactColumn) * (column - impactColumn)
  66.                         + (row - impactRow) * (row - impactRow)
  67.                         <= impactRadius * impactRadius)
  68.                     {
  69.                         matrix[row, column] = ' ';
  70.                     }
  71.                 }
  72.             }
  73.         }
  74.  
  75.         private static void PrintMatrix(char[,] matrix)
  76.         {
  77.             for (int row = 0; row < matrix.GetLength(0); row++)
  78.             {
  79.                 for (int column = 0; column < matrix.GetLength(1); column++)
  80.                 {
  81.                     Console.Write(matrix[row, column]);
  82.                 }
  83.  
  84.                 Console.WriteLine();
  85.             }
  86.         }
  87.  
  88.         private static void FillMatrix(int numberOfRows, int numberOfColumns, string snake, char[,] matrix)
  89.         {
  90.             bool isMovingLeft = true;
  91.             int currentIndex = 0;
  92.             for (int row = numberOfRows - 1; row >= 0; row--)
  93.             {
  94.                 if (isMovingLeft)
  95.                 {
  96.                     for (int col = numberOfColumns - 1; col >= 0; col--)
  97.                     {
  98.                         if (currentIndex >= snake.Length)
  99.                         {
  100.                             currentIndex = 0;
  101.                         }
  102.  
  103.                         matrix[row, col] = snake[currentIndex];
  104.                         currentIndex++;
  105.                     }
  106.  
  107.                 }
  108.  
  109.                 else
  110.                 {
  111.                     for (int col = 0; col < numberOfColumns; col++)
  112.                     {
  113.                         if (currentIndex >= snake.Length)
  114.                         {
  115.                             currentIndex = 0;
  116.                         }
  117.  
  118.                         matrix[row, col] = snake[currentIndex];
  119.                         currentIndex++;
  120.                     }
  121.                 }
  122.  
  123.                 isMovingLeft = !isMovingLeft;
  124.             }
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment