Infinite Grid
Infinite Grid
namespace Test_MyGrid_v2 { public class SubGrid { private byte type; private ArrayList arrayList; private int countX; private int countY; public int CountX { get { return countX; } } public int CountY { get { return countY; } } public byte Type { get { return type; } } public string TypeExplained { get { switch (type) { case 0: return "RD(0) = Right Down"; case 1: return "RU(1) = Right Up"; case 2: return "LU(2) = Left Up"; case 3: return "LD(3) = Left Down"; } return "Here should be the type of the subgrid explained..."; } } /// <summary> /// Initializes a subgrid /// </summary> /// <param name="type"> /// RD (0) RU (1) LU (2) LD (3) /// </param> public SubGrid(int sizeX, int sizeY, byte type) { this.countX = sizeX; this.countY = sizeY; this.type = type; arrayList = new ArrayList(); for (int i = 0; i < countX; i++) { arrayList.Add(new ArrayList()); for (int j = 0; j < countY; j++) { Coord coord = transform(i, j); ((ArrayList)arrayList[i]).Add(new MyButton(coord)); } } } public MyButton this[int x, int y] { get {
Coord coord = transform(x, y); return (MyButton)(((ArrayList)arrayList[coord.X])[coord.Y]); } set { Coord coord = transform(x, y); MyButton button = (MyButton)(((ArrayList)arrayList[coord.X])[coo rd.Y]); button = value; } } private Coord transform(int x, int y) { switch (type) { // RU = RightUp case 1: return new Coord(x, -y - 1); // LU = LeftUp case 2: return new Coord(-x - 1, -y - 1); // LD = LeftDown case 3: return new Coord(-x - 1, y); //case 0: // RD = RightUp default: return new Coord(x, y); } } } public class Grid { private int sizeX; private int sizeY; public int SizeX { get { return sizeX; } } public int SizeY { get { return sizeY; } } private private private private SubGrid SubGrid SubGrid SubGrid sgRD; sgRU; sgLU; sgLD; // // // // RD RU LU LD 0 1 2 3
public MyButton this[int x, int y] { get { if (x >= 0) { if (y >= 0) return sgRD[x, y]; else return sgRU[x, y]; } else { if (y >= 0) return sgLD[x, y];
else return sgLU[x, y]; } } set { if (x >= 0) { if (y >= 0) sgRD[x, else sgRU[x, } else { if (y >= 0) sgLD[x, else sgLU[x, } } } public Grid(int sizeX, int sizeY) { this.sizeX = sizeX; this.sizeY = sizeY; int rightX = sizeX / 2; int leftX = sizeX / 2; int upY = sizeY / 2; int downY = sizeY / 2; if (sizeX % 2 == 1) { rightX = (sizeX + 1) / 2; leftX = (sizeX - 1) / 2; } if (sizeY % 2 == 1) { upY = (sizeY - 1) / 2; downY = (sizeY + 1) / 2; } sgRD sgRU sgLU sgLD } } } = = = = new new new new SubGrid(rightX, downY, 0); SubGrid(rightX, upY, 1); SubGrid(leftX, upY, 2); SubGrid(leftX, downY, 3);
y] = value; y] = value;
y] = value; y] = value;