Advertisement
simonradev

TargetPractice

Jun 2nd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.17 KB | None | 0 0
  1. namespace TargetPractice
  2. {
  3.     using System;
  4.     using System.Linq;
  5.     using System.Text;
  6.  
  7.     public class Startup
  8.     {
  9.         private static char[,] matrix;
  10.  
  11.         private static int rowsOfMatrix;
  12.         private static int colsOfMatrix;
  13.  
  14.         public static void Main()
  15.         {
  16.             int[] dimensionsOfMatrix = SplitStringToIntegerArray();
  17.  
  18.             rowsOfMatrix = dimensionsOfMatrix[0];
  19.             colsOfMatrix = dimensionsOfMatrix[1];
  20.  
  21.             matrix = new char[rowsOfMatrix, colsOfMatrix];
  22.  
  23.             string snakeWord = Console.ReadLine();
  24.             int indexOfSnakeWord = 0;
  25.             for (int currentRow = 0; currentRow < rowsOfMatrix; currentRow++)
  26.             {
  27.                 if (currentRow % 2 == 0)
  28.                 {
  29.                     for (int currentCol = colsOfMatrix - 1; currentCol >= 0; currentCol--)
  30.                     {
  31.                         matrix[rowsOfMatrix - 1 - currentRow, currentCol] = snakeWord[indexOfSnakeWord++ % snakeWord.Length];
  32.                     }
  33.                 }
  34.                 else
  35.                 {
  36.                     for (int currentCol = 0; currentCol < colsOfMatrix; currentCol++)
  37.                     {
  38.                         matrix[rowsOfMatrix - 1 - currentRow, currentCol] = snakeWord[indexOfSnakeWord++ % snakeWord.Length];
  39.                     }
  40.                 }
  41.             }
  42.  
  43.             int[] shotRadius = SplitStringToIntegerArray();
  44.  
  45.             int targetRow = shotRadius[0];
  46.             int targetCol = shotRadius[1];
  47.             int radius = shotRadius[2];
  48.  
  49.             for (int currentRow = 0; currentRow < rowsOfMatrix; currentRow++)
  50.             {
  51.                 for (int currentCol = 0; currentCol < colsOfMatrix; currentCol++)
  52.                 {
  53.                     if (CellIsInRangeOfTheShot(currentRow, currentCol, targetRow, targetCol, radius))
  54.                     {
  55.                         matrix[currentRow, currentCol] = ' ';
  56.                     }
  57.                 }
  58.             }
  59.  
  60.             for (int currentRow = 0; currentRow < rowsOfMatrix; currentRow++)
  61.             {
  62.                 for (int currentCol = 0; currentCol < colsOfMatrix; currentCol++)
  63.                 {
  64.                     ActivateGravityOnCell(currentRow, currentCol);
  65.                 }
  66.             }
  67.  
  68.             PrintMatrix();
  69.         }
  70.  
  71.         private static void ActivateGravityOnCell(int row, int col)
  72.         {
  73.             while (LowerCellIsEmptyOrValid(row + 1, col))
  74.             {
  75.                 for (int currentRow = row + 1; currentRow >= 1; currentRow--)
  76.                 {
  77.                     matrix[currentRow, col] = matrix[currentRow - 1, col];
  78.                 }
  79.  
  80.                 matrix[0, col] = ' ';
  81.                 row++;
  82.             }
  83.         }
  84.  
  85.         private static bool LowerCellIsEmptyOrValid(int row, int col)
  86.         {
  87.             return row >= 0 && row < rowsOfMatrix &&
  88.                    col >= 0 && col < colsOfMatrix &&
  89.                    matrix[row, col] == ' ';
  90.         }
  91.  
  92.         private static bool CellIsInRangeOfTheShot(int currentRow, int currentCol, int targetRow, int targetCol, int radius)
  93.         {
  94.             bool cellIsInsideRangeOfTheShot = Math.Pow(currentRow - targetRow, 2) +
  95.                                               Math.Pow(currentCol - targetCol, 2) <= Math.Pow(radius, 2);
  96.  
  97.             return cellIsInsideRangeOfTheShot;
  98.         }
  99.  
  100.         private static int[] SplitStringToIntegerArray()
  101.         {
  102.             return Console.ReadLine()
  103.                     .Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
  104.                     .Select(int.Parse)
  105.                     .ToArray();
  106.         }
  107.  
  108.         private static void PrintMatrix()
  109.         {
  110.             StringBuilder result = new StringBuilder();
  111.             for (int currentRow = 0; currentRow < matrix.GetLength(0); currentRow++)
  112.             {
  113.                 for (int currentCol = 0; currentCol < matrix.GetLength(1); currentCol++)
  114.                 {
  115.                     result.Append(matrix[currentRow, currentCol]);
  116.                 }
  117.  
  118.                 result.AppendLine();
  119.             }
  120.  
  121.             Console.Write(result);
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement