yanchevilian

Snake Moves / C# Advanced

Aug 27th, 2021 (edited)
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SnakeMove
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             int[] dimensions = Console.ReadLine()
  12.                 .Split(" ", StringSplitOptions.RemoveEmptyEntries)
  13.                 .Select(int.Parse)
  14.                 .ToArray();
  15.  
  16.             int rows = dimensions[0];
  17.             int cols = dimensions[1];
  18.  
  19.             char[,] matrixFromString = new char[rows, cols];
  20.             string text = Console.ReadLine();
  21.  
  22.             Queue<char> inputChars = new Queue<char>(text);
  23.             for (int rowIndex = 0; rowIndex < rows; rowIndex++)
  24.             {
  25.                 for (int colIndex = 0; colIndex < cols; colIndex++)
  26.                 {
  27.                     if (rowIndex % 2 != 0)
  28.                     {
  29.                         char currentSymbol = inputChars.Dequeue();
  30.                         matrixFromString[rowIndex, cols - colIndex - 1] = currentSymbol;
  31.                         inputChars.Enqueue(currentSymbol);
  32.                     }
  33.                     else
  34.                     {
  35.                         char currentSymbol = inputChars.Dequeue();
  36.                         matrixFromString[rowIndex, colIndex] = currentSymbol;
  37.                         inputChars.Enqueue(currentSymbol);
  38.                     }
  39.                 }
  40.             }
  41.  
  42.             PrintSnake(matrixFromString);
  43.         }
  44.  
  45.         private static void PrintSnake(char[,] matrixFromString)
  46.         {
  47.             for (int row = 0; row < matrixFromString.GetLength(0); row++)
  48.             {
  49.                 for (int col = 0; col < matrixFromString.GetLength(1); col++)
  50.                 {
  51.                     Console.Write(matrixFromString[row, col] + "");
  52.                 }
  53.  
  54.                 Console.WriteLine();
  55.             }
  56.         }
  57.     }
  58. }
  59.  
Add Comment
Please, Sign In to add comment