-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPolyexpand.js
142 lines (130 loc) · 4.53 KB
/
Polyexpand.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
A Polyexpand object is container dedicated to boolean logic involving groups of Polyomino objects.
Polyexpand is capable of expanding a Polyomino into all possible locations on the Field.
@param polyomino is a single Polyomino object that the Polyexpand houses to begin with.
@param field is also a single Polyomino that represents all the spaces Polyominoes can be put into.
@param rules is an object with several boolean variables specifying rules for expansion.
*/
if (ps.flags.SHOW_LOGS) console.log("Creating the Polyexpand.");
ps.Polyexpand = function (polyomino, field, rules) {
let that = this;
/*
Initalize the Polyexpand
*/
that.init = function (polyomino, field, rules) {
// save given objects
that.polyominoes = [polyomino];
that.field = field;
that.rules = rules;
// if this Polyexpand should have reflections of this Polyomino, add all the reflections
if (that.rules.allowReflections) {
that.expandReflections();
}
// if this Polyexpand should have rotations of this Polyomino, add all the rotations
if (that.rules.allowRotations) {
that.expandRotations();
}
// now that all unique variation of Polyominoes exist, we can account for the field.
// this function will take a while. by the end, this Polyexpand will have all possible fitting positions on the Field.
that.expandPositions();
}
/*
Creates all unique reflection over the Y axis of all polyominoes currently in this Polyexpand
*/
that.expandReflections = function () {
// we'll create reflections of any currently existing Polyominoes
that.polyominoes.forEach(function (polyomino) {
let newPoly = polyomino.clone();
newPoly.flipY();
newPoly.reset();
newPoly.sort();
// if the new Polyomino already exists in this Polyexpand
if (that.alreadyExists(newPoly))
// delete it
delete newPoly;
else
// otherwise, add it
that.polyominoes.push(newPoly);
});
}
/*
Creates all unique rotations of all polyominoes currently in this Polyexpand
*/
that.expandRotations = function () {
// we'll create reflections of any currently existing Polyominoes
that.polyominoes.forEach(function (polyomino) {
for (let i = 1; i <= 3; i++) {
let newPoly = polyomino.clone();
newPoly.rotate(i * 90);
newPoly.reset();
newPoly.sort();
// if the new Polyomino already exists in this Polyexpand
if (that.alreadyExists(newPoly))
// delete it
delete newPoly;
else
// otherwise, add it
that.polyominoes.push(newPoly);
}
});
}
/*
Creates all unique positions of all polyominoes
*/
that.expandPositions = function () {
let newPolyominoes = [];
let max = field.getMax();
// for every Polyomino...
that.polyominoes.forEach(function(polyomino) {
let polyMax = polyomino.getMax();
// for every row in the Field...
for( let i = 0; i <= max.x - polyMax.x; i++ ) {
// for every column in every row...
for( let j = 0; j <= max.y - polyMax.y; j++ ) {
let newPoly = polyomino.clone();
newPoly.shift(i, j);
if( that.fits(newPoly) ) {
newPolyominoes.push(newPoly);
}
else {
delete newPoly;
}
}
}
});
// replace this Polyexpand's polyominoes entirely. all positions have been found.
delete that.polyominoes;
that.polyominoes = newPolyominoes;
}
/*
Returns true if the given Polyomino, in its current orientation, will fit in the Field
Returns false otherwise.
*/
that.fits = function (polyomino) {
return that.field.contains(polyomino);
}
/*
Return true if the given Polyomino already exists within this Polyexpand
Return false otherwise.
*/
that.alreadyExists = function (given) {
let result = false;
// run through all polyominoes in this Polyexpand
that.polyominoes.forEach(function (polyomino) {
// if there's an equality at any point
if (given.equals(polyomino))
result = true;
});
return result;
}
that.toString = function() {
let temp = "";
let customMax = field.getMax();
that.polyominoes.forEach(function(polyomino) {
temp += polyomino.toString(customMax) + "\n";
});
return temp;
}
// initialize
that.init(polyomino, field, rules);
}