-
Notifications
You must be signed in to change notification settings - Fork 1
/
NominoBuilder.cs
116 lines (107 loc) · 4.26 KB
/
NominoBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BASeTris.Blocks;
namespace BASeTris
{
public class NominoBuilder
{
static T[][] CopyArrayBuiltIn<T>(T[][] source)
{
var len = source.Length;
var dest = new T[len][];
for (var x = 0; x < len; x++)
{
var inner = source[x];
var ilen = inner.Length;
var newer = new T[ilen];
Array.Copy(inner, newer, ilen);
dest[x] = newer;
}
return dest;
}
bool[][] NominoMatrix = null;
public static String NominoToString(bool[][] input)
{
StringBuilder buildresult = new StringBuilder();
foreach(var row in input)
{
foreach(var col in row)
{
buildresult.Append(col ? "#" : ".");
}
buildresult.AppendLine();
}
return buildresult.ToString();
}
public static IEnumerable<bool[][]> BuildNominoes(int BlockCount,bool[][] BuildingNomino = null, int XPos = 0, int YPos = 0)
{
//start case:
//if our building nomino is null, create a new one.
if (BuildingNomino == null)
{
BuildingNomino = new bool[BlockCount][];
for (int bn= 0; bn < BlockCount; bn++)
{
BuildingNomino[bn] = new bool[BlockCount];
}
//set the top left index...
BuildingNomino[0][0] = true;
var Duplicated = CopyArrayBuiltIn(BuildingNomino);
//recursively call with the two possible directions.
foreach(var iterate in BuildNominoes(BlockCount-1, Duplicated, 1,0))
{
yield return iterate;
}
foreach(var iterate in BuildNominoes(BlockCount-1, Duplicated, 0,1))
{
yield return iterate;
}
//that actually covers all possibilities...
}
else
{
//otherwise:
//if BblockCount is 0, we are done. set the specified index, and yield the completed nomino matrix.
if (BlockCount == 0)
{
BuildingNomino[XPos][YPos] = true;
yield return BuildingNomino;
yield break;
}
else
{
foreach (int xoffset in new int[] { -1, 0,1 })
{
int useX = XPos + xoffset;
if (useX < 0) continue;
foreach (int yoffset in new int[] { -1,0,1})
{
if ((xoffset == 0) ^ (yoffset == 0)) //only proceed if one or the other is zero- but not both or neither. (xor)
{
int useY = YPos + yoffset;
//since we make the matrix the "length" of the 'snake', we only need to check that we are < 0 since we cannot be long enough to hit either bottom or right side.
if (useY < 0) continue;
//if the given position is not occupied already
if (!BuildingNomino[useX][useY])
{
//set the particular position...
BuildingNomino[XPos][YPos] = true;
var copied = CopyArrayBuiltIn(BuildingNomino);
//now, recursively call for this particular arrangement...
foreach (var iterate in BuildNominoes(BlockCount - 1, copied, useX, useY))
{
yield return iterate;
}
}
}
}
}
}
}
}
}
}