Guest User

Spiral_Square_matrix in C#

a guest
Jun 26th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.78 KB | None | 0 0
  1. /* (2D) Finite Matrix - transverser
  2.  * ---incooperated a Spiral Matrix Printer
  3.  *
  4.  * (C) WhiZTiM 27th June, 2013
  5.  * FOR EDUCATIONAL PURPOSES only!
  6.  */
  7. using System;
  8.  
  9. namespace SP
  10. {
  11.     //2D Coordinate...
  12.     public struct Coord
  13.     {
  14.         public int X { get; set; }
  15.         public int Y { get; set; }
  16.     }
  17.  
  18.     //make a prepaid contract for element types
  19.     public abstract class Element
  20.     {
  21.         protected bool _taken { get; set; }
  22.         public void setTaken(bool value) { _taken = value; }
  23.         public bool isTaken() { return _taken; }
  24.     }
  25.  
  26.     //String elements
  27.     public class StringElement : Element
  28.     {
  29.         private readonly string _value;
  30.  
  31.         public StringElement(string value) : base()
  32.         {
  33.             _value = value;
  34.         }
  35.         public string   str() { return _value; }
  36.     }
  37.  
  38.     //A thin wrapper for StringElements
  39.     public class Spiral_Array
  40.     {
  41.         private readonly StringElement[,] _array;
  42.         public Spiral_Array (StringElement[,] elements)
  43.         {
  44.             _array = elements;
  45.             sizeX = _array.GetLength(0);
  46.             sizeY = _array.GetLength(1);
  47.         }
  48.         public StringElement this[int x, int y]
  49.         {   get{ return _array[x,y]; }
  50.             private set { /*      */ }
  51.         }
  52.         public int sizeX{ get; private set; }
  53.         public int sizeY{ get; private set; }
  54.     }
  55.  
  56.     //This is a helper class that creates 2D 'Element' Objects from a string array
  57.     public class Helper_2D_Elements
  58.     {
  59.         public StringElement[,] Elements {get; private set; }
  60.         public Helper_2D_Elements(string[] array)
  61.         {
  62.             int arrSize = array.Length;
  63.  
  64.             Elements = new StringElement[arrSize,arrSize];
  65.  
  66.             for(int i = 0; i < arrSize; i++)
  67.             {
  68.                 string[] str = array[i].Split(' ');
  69.                 for(int j = 0; j < arrSize; j++)
  70.                 {
  71.                     Elements[i,j] = new StringElement(str[j]);
  72.                 }
  73.             }
  74.         }
  75.     }
  76.  
  77.     //this is an independent traveller object...
  78.     /*
  79.      * It takes a finite grid of size (0,0) to (X, Y)
  80.      * transverses through it in very definite direction
  81.      * and only passes through a point once!
  82.      */
  83.     public class Element_Traveller2D
  84.     {
  85.         public enum Direction : int { Right, Down, Left, Up }
  86.         public enum SweepDirection { Clockwise, Aniclockwise }
  87.         private Coord _coord;
  88.         private readonly int LimX, LimY;
  89.         private readonly long _max;
  90.         private bool[,] _taken;
  91.         private long _taken_count;
  92.         private int _preference;
  93.         private bool _first;
  94.         private SweepDirection _sweep;
  95.         public Element_Traveller2D(int X, int Y, SweepDirection sweep, Direction startingAt)
  96.         {
  97.             LimX = X; LimY = Y;
  98.             _coord = new Coord{ X = 0, Y = 0 };     //defaults to 0,0
  99.             _taken = new bool[X,Y];     //defaults to false
  100.             _first = true;
  101.             _max = X * Y;
  102.             _taken_count = 0;
  103.             _preference = (int) startingAt;
  104.             _sweep = sweep;
  105.         }
  106.  
  107.         private void changeDir()
  108.         {
  109.             if(_sweep == SweepDirection.Clockwise)
  110.             { _preference = (++_preference) % 4; }
  111.             else
  112.             { _preference = (--_preference) % 4; }
  113.         }
  114.  
  115.         public Coord nextPoint()
  116.         {
  117.             if(_first){
  118.                 _taken[0,0] = true;
  119.                 _taken_count++;
  120.                 _first = false;
  121.                 return _coord;
  122.             }
  123.             for(int _a = 0; _a < 4; _a++) {
  124.                 switch (_preference) {
  125.                 case 0:
  126.                     if(_coord.Y + 1 < LimY && !_taken[_coord.X, _coord.Y + 1])
  127.                     {
  128.                         _coord.Y += 1; _taken_count++;
  129.                         _taken[_coord.X, _coord.Y] = true;
  130.                         return _coord;
  131.                     }
  132.                     changeDir();
  133.                     break;
  134.                 case 1:
  135.                     if(_coord.X + 1 < LimX && !_taken[_coord.X + 1, _coord.Y])
  136.                     {
  137.                         _coord.X += 1; _taken_count++;
  138.                         _taken[_coord.X, _coord.Y] = true;
  139.                         return _coord;
  140.                     }
  141.                     changeDir();
  142.                     break;
  143.                 case 2:
  144.                     if(_coord.Y - 1 >= 0 && !_taken[_coord.X, _coord.Y - 1])
  145.                     {
  146.                         _coord.Y -= 1; _taken_count++;
  147.                         _taken[_coord.X, _coord.Y] = true;
  148.                         return _coord;
  149.                     }
  150.                     changeDir();
  151.                     break;
  152.                 case 3:
  153.                     if(_coord.X - 1 >= 0 && !_taken[_coord.X - 1, _coord.Y])
  154.                     {
  155.                         _coord.X -= 1; _taken_count++;
  156.                         _taken[_coord.X, _coord.Y] = true;
  157.                         return _coord;
  158.                     }
  159.                     changeDir();
  160.                     break;
  161.                 default:
  162.                     changeDir();
  163.                     break;
  164.                 }
  165.             }
  166.             _taken_count = _max;
  167.             return new Coord();
  168.         }
  169.  
  170.         public bool canMove()
  171.         {
  172.             return (_taken_count < _max);
  173.         }
  174.     }
  175.  
  176.     /* This class is responsible for instantiating the Traveller object
  177.      * and transverse through the grid... while printing it
  178.      * */
  179.     public class Spiral_Printer
  180.     {
  181.         private readonly Spiral_Array _value;
  182.         private readonly Element_Traveller2D traveller;
  183.         public Spiral_Printer(Spiral_Array array)
  184.         {
  185.             _value = array;
  186.             traveller = new Element_Traveller2D( _value.sizeX, _value.sizeY,
  187.                             Element_Traveller2D.SweepDirection.Clockwise, Element_Traveller2D.Direction.Right );
  188.         }
  189.  
  190.         public void Print()
  191.         {
  192.             while(traveller.canMove())
  193.             {
  194.                 Coord dim = traveller.nextPoint();
  195.                 Console.Write("{0} ", _value[dim.X, dim.Y].str());
  196.             }
  197.         }
  198.     }
  199.  
  200.  
  201.     public class Program
  202.     {
  203.         public static void Main(string[] args)
  204.         {
  205.             Program prog = new Program();
  206.             //read inputs
  207.             string[] values = prog.readInput();
  208.             if(string.IsNullOrEmpty(values[0]))
  209.             {
  210.                 Console.WriteLine("ID10T!");
  211.                 return;
  212.             }
  213.  
  214.             //create an array of string elements
  215.             Helper_2D_Elements elements = new Helper_2D_Elements(values);
  216.  
  217.             //create the Spiral Array class
  218.             Spiral_Array spiral = new Spiral_Array(elements.Elements);
  219.  
  220.             //Create a printer
  221.             Spiral_Printer printer = new Spiral_Printer(spiral);
  222.             printer.Print();
  223.         }
  224.  
  225.         string[] readInput()
  226.         {
  227.             string str = Console.ReadLine();
  228.             if(string.IsNullOrEmpty(str) || string.IsNullOrWhiteSpace(str) ) {
  229.                 Console.WriteLine("Invalid input Numbnuts");
  230.                 return null;
  231.             }
  232.             string[] arrCount = str.Split(' ');
  233.             int arrSize = arrCount.Length;
  234.             string[] StrList = new string[arrSize];
  235.             StrList[0] = str;
  236.             for(int i = 1; i < arrSize; i++)
  237.             {
  238.                 str = Console.ReadLine();
  239.                 StrList[i] = str;
  240.             }
  241.             return StrList;
  242.         }
  243.     }
  244. }
Advertisement
Add Comment
Please, Sign In to add comment