Created
January 30, 2014 16:31
-
-
Save ro31337/8712633 to your computer and use it in GitHub Desktop.
Spiral box on C# (see http://habrahabr.ru/post/210796/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Box | |
{ | |
public interface IAsset | |
{ | |
void Accept(IVisitor visitor); | |
} | |
public class MyBox : IAsset | |
{ | |
public int[,] Arr; | |
public int Size; | |
public MyBox(int size) | |
{ | |
this.Size = size; | |
Arr = new int[size, size]; | |
} | |
public void Accept(IVisitor visitor) | |
{ | |
visitor.Visit(this); | |
} | |
public override string ToString() | |
{ | |
string s = ""; | |
for (int i = 0; i < Size; i++) | |
{ | |
for(int j = 0; j < Size; j++) | |
{ | |
s += (" " + String.Format("{0:D2}", Arr[j, i]) + " ").Replace(" 00 ", " __ "); | |
} | |
s += "\r\n"; | |
} | |
return s; | |
} | |
} | |
public interface IVisitor | |
{ | |
void Visit(MyBox box); | |
} | |
public class MyVisitor : IVisitor | |
{ | |
public enum Directions { Left, Right, Up, Down } | |
public class Rules | |
{ | |
private int index = 0; | |
public Directions CurrentDirection | |
{ | |
get | |
{ | |
return rules[index]; | |
} | |
} | |
private List<Directions> rules = new List<Directions> | |
{ | |
Directions.Right, | |
Directions.Down, | |
Directions.Left, | |
Directions.Up | |
}; | |
public void SwitchToNext() | |
{ | |
index++; | |
if (index > 3) | |
index = 0; | |
} | |
} | |
private Rules rules = new Rules(); | |
private bool isObstacle(MyBox box, Directions direction, int x, int y) | |
{ | |
switch(direction) | |
{ | |
case Directions.Right: | |
if (x + 1 >= box.Size) | |
return true; | |
return box.Arr[x + 1, y] != 0; | |
case Directions.Down: | |
if (y + 1 >= box.Size) | |
return true; | |
return box.Arr[x, y + 1] != 0; | |
case Directions.Left: | |
if (x - 1 < 0) | |
return true; | |
return box.Arr[x - 1, y] != 0; | |
case Directions.Up: | |
if (y - 1 < 0) | |
return true; | |
return box.Arr[x, y - 1] != 0; | |
default: | |
throw new NotSupportedException(); | |
} | |
} | |
public void Visit(MyBox box) | |
{ | |
int x = -1; | |
int y = 0; | |
int value = 1; | |
while(value < box.Size * box.Size + 1) | |
{ | |
if(isObstacle(box, rules.CurrentDirection, x, y)) | |
rules.SwitchToNext(); | |
switch(rules.CurrentDirection) | |
{ | |
case Directions.Right: | |
x++; | |
break; | |
case Directions.Down: | |
y++; | |
break; | |
case Directions.Left: | |
x--; | |
break; | |
case Directions.Up: | |
y--; | |
break; | |
} | |
box.Arr[x, y] = value; | |
value++; | |
} | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Console.Write("Type the size (enter for 8): "); | |
string str = Console.ReadLine(); | |
if (String.IsNullOrWhiteSpace(str)) | |
str = "8"; | |
int size = Int32.Parse(str); | |
IVisitor visitor = new MyVisitor(); | |
MyBox box = new MyBox(size); | |
box.Accept(visitor); | |
Console.WriteLine(box); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment