Advertisement
gparlakov

6.loops 14.spiral matrix wih SetCursorPosition

Nov 15th, 2012
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _14.SpiralMatrix
  8. {
  9.     class SpiralMatrix
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int N,lenght,cursorX=15,cursorY=1,step=1;       /* cursorX,Y hold the position of the next number  */
  14.             sbyte direction = 1;                            /*  and "step" holds the nex number to be written  */
  15.             Console.SetWindowSize(150, 35);                 /* in the spiral matrix, The window is set so large*/
  16.             do                                              /* so that it can contain larger matrixes          */
  17.             {
  18.                 Console.WriteLine("Number N: ");
  19.             } while (!int.TryParse(Console.ReadLine(), out N));
  20.             lenght = N;
  21.             do                                             //endless loop which will be break-ed when at end of spiral        
  22.             {
  23.                 for (int i = 0; Math.Abs(i) < lenght; )
  24.                 {
  25.                    
  26.                     cursorX += direction * 4;
  27.                     Console.SetCursorPosition(cursorX, cursorY);
  28.                     Console.Write("|{0,2}|", step);
  29.                     i+=direction;
  30.                     if (step == (N * N))        //checks if we've arrived at the end of the spiral
  31.                     {                           //and breaks the small loop
  32.                         break;
  33.                     }
  34.                     step++;                  
  35.                 }
  36.                 lenght--;
  37.  
  38.                 if (step == (N * N))        /*checks if we've arrived at the end */
  39.                                             /*  of the spiral and breaks the big loop*/
  40.                 {
  41.                     break;
  42.                 }
  43.                
  44.                 for (int y = 0; Math.Abs(y) < lenght; )
  45.                 {
  46.                    
  47.                     cursorY += direction;
  48.                     Console.SetCursorPosition(cursorX, cursorY);
  49.                     Console.Write("|{0,2}|", step);
  50.                     y += direction;
  51.                     if (step == (N * N))        //checks if we've arrived at the end of the spiral
  52.                     {
  53.                         break;
  54.                     }
  55.                     step++;                    
  56.                 }
  57.  
  58.                 /*on every two iterations the direction of writing is set opposite
  59.                  so that it first moves right/down then left/up*/
  60.                 if (direction == 1)        
  61.                 {
  62.                     direction = -1;
  63.                 }
  64.                 else
  65.                 {
  66.                     direction = 1;
  67.                 }
  68.                
  69.             } while (true);
  70.             Console.SetCursorPosition(0, 34);
  71.             //Console.WriteLine("\n");
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement