Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading;
- using Loops__Homework;
- namespace _14.SpiraleMatrix
- {
- class SpiraleMatrix
- {
- private static uint[,] spiraleMatrix;
- private static uint colStart;
- private static uint rowStart;
- private static uint colEnd;
- private static uint rowEnd;
- static void Main(string[] args)
- {
- uint matrixDimension = Functions.ValidateInput("matrix dimension", 20);
- spiraleMatrix = new uint[matrixDimension, matrixDimension];
- GenerateSpirale(matrixDimension);
- //PrintMatrix(spiraleMatrix);
- }
- private static void GenerateSpirale(uint matrixDimension)
- {
- colStart = 0;
- rowStart = 0;
- colEnd = matrixDimension - 1;
- rowEnd = matrixDimension - 1;
- uint counter = 1;
- uint matrixTotalLength = (uint)Math.Pow(matrixDimension, 2);
- //In case of invalid inputs, the drawing should begin after the input text
- // and in the beginning of the console.
- int consoleTopPos = ++Console.CursorTop;
- Console.CursorLeft=0;
- while (counter <= matrixTotalLength)
- {
- //Left-to-Right
- counter = LeftToRight(counter, consoleTopPos);
- //Top-to-bottom
- counter = TopToBottom(counter, consoleTopPos);
- //Right-to-left
- counter = RightToLeft(counter, consoleTopPos);
- // Bottom-to-top
- counter = BottomToTop(counter, consoleTopPos);
- }
- //This places the "Press any key..." text below the matrix, regardess of matrix size.
- Console.CursorTop += (int)matrixDimension * 3 / 2 + 1;
- }
- private static uint TopToBottom(uint counter, int consoleTopPos)
- {
- for (int i = (int)rowStart; i <= rowEnd; i++)
- {
- // FOR DRAWING PURPOSES ONLY!!!
- //-----------------------------------------------------------
- Console.SetCursorPosition(6 * (int)colEnd + 1, 3 * i + consoleTopPos);
- Console.Write(counter);
- Thread.Sleep(150);
- //-----------------------------------------------------------
- spiraleMatrix[i, colEnd] = counter;
- counter++;
- }
- colEnd--;
- return counter;
- }
- private static uint LeftToRight(uint counter, int consoleTopPos)
- {
- for (int i = (int)colStart; i <= colEnd; i++)
- {
- // FOR DRAWING PURPOSES ONLY!!!
- //-----------------------------------------------------------
- Console.SetCursorPosition(6 * i + 1, 3 * (int)rowStart + consoleTopPos);
- Console.Write(counter);
- Thread.Sleep(150);
- //-----------------------------------------------------------
- spiraleMatrix[rowStart, i] = counter;
- counter++;
- }
- rowStart++;
- return counter;
- }
- private static uint RightToLeft(uint counter, int consoleTopPos)
- {
- for (int i = (int)colEnd; i >= colStart; i--)
- {
- // FOR DRAWING PURPOSES ONLY!!!
- //-----------------------------------------------------------
- Console.SetCursorPosition(6 * i + 1, 3 * (int)rowEnd + consoleTopPos);
- Console.Write(counter);
- Thread.Sleep(150);
- //-----------------------------------------------------------
- spiraleMatrix[rowEnd, i] = counter;
- counter++;
- }
- rowEnd--;
- return counter;
- }
- private static uint BottomToTop( uint counter, int consoleTopPos)
- {
- for (int i = (int)rowEnd; i >= rowStart; i--)
- {
- // FOR DRAWING PURPOSES ONLY!!!
- //-----------------------------------------------------------
- Console.SetCursorPosition(6 * (int)colStart + 1, 3 * i + consoleTopPos);
- Console.Write(counter);
- Thread.Sleep(150);
- //-----------------------------------------------------------
- spiraleMatrix[i, colStart] = counter;
- counter++;
- }
- colStart++;
- return counter;
- }
- static void PrintMatrix<T>(T[,] matrix)
- {
- for (int row = 0; row < matrix.GetLength(0); row++)
- {
- for (int col = 0; col < matrix.GetLength(1); col++)
- {
- Console.Write("{0,3}", matrix[row,col]);
- }
- Console.WriteLine();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement