-Annie-

TextGravity

Jul 11th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.79 KB | None | 0 0
  1. namespace TextGravity
  2. {
  3.     using System;
  4.     using System.Text;
  5.     using System.Security;
  6.  
  7.     public class TextGravity
  8.     {
  9.         public static void Main()
  10.         {
  11.             int numberOfColumns = int.Parse(Console.ReadLine());
  12.             string inputLine = Console.ReadLine();
  13.             int numberOfRows = inputLine.Length / numberOfColumns;
  14.  
  15.             if (inputLine.Length % numberOfColumns != 0)
  16.             {
  17.                 numberOfRows++;
  18.             }
  19.  
  20.             char[,] matrix = new char[numberOfRows, numberOfColumns];
  21.  
  22.             //Fill the matrix
  23.             int currentCharIndex = 0;
  24.  
  25.             for (int row = 0; row < matrix.GetLength(0); row++)
  26.             {
  27.                 for (int col = 0; col < matrix.GetLength(1); col++)
  28.                 {
  29.                     if (currentCharIndex < inputLine.Length)
  30.                     {
  31.                         matrix[row, col] = inputLine[currentCharIndex];
  32.                         currentCharIndex++;
  33.                     }
  34.  
  35.                     else
  36.                     {
  37.                         matrix[row, col] = ' ';
  38.                     }
  39.                 }
  40.             }
  41.  
  42.             //Text fall
  43.             for (int col = 0; col < matrix.GetLength(1); col++)
  44.             {
  45.                 RunGravity(matrix, col);
  46.             }
  47.  
  48.             //Print
  49.             StringBuilder outputBuilder = new StringBuilder();
  50.             outputBuilder.Append("<table>");
  51.  
  52.             for (int row = 0; row < matrix.GetLength(0); row++)
  53.             {
  54.                 outputBuilder.Append("<tr>");
  55.  
  56.                 for (int col = 0; col < matrix.GetLength(1); col++)
  57.                 {
  58.                     outputBuilder.AppendFormat("<td>{0}</td>",
  59.                         SecurityElement.Escape(matrix[row, col].ToString()));
  60.                 }
  61.  
  62.                 outputBuilder.Append("</tr>");
  63.             }
  64.  
  65.             outputBuilder.Append("</table>");
  66.  
  67.             Console.WriteLine(outputBuilder.ToString());
  68.         }
  69.  
  70.         //Method for falling
  71.         private static void RunGravity(char[,] matrix, int col)
  72.         {
  73.             while (true)
  74.             {
  75.                 bool hasFalen = false;
  76.  
  77.                 for (int row = 1; row < matrix.GetLength(0); row++)
  78.                 {
  79.                     char topChar = matrix[row - 1, col];
  80.                     char currentChar = matrix[row, col];
  81.  
  82.                     if (currentChar == ' ' && topChar != ' ')
  83.                     {
  84.                         matrix[row, col] = topChar;
  85.                         matrix[row - 1, col] = ' ';
  86.                         hasFalen = true;
  87.                     }
  88.                 }
  89.  
  90.                 if (! hasFalen)
  91.                 {
  92.                     break;
  93.                 }
  94.             }
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment