-
Notifications
You must be signed in to change notification settings - Fork 0
/
Cube.java
executable file
·217 lines (192 loc) · 7.92 KB
/
Cube.java
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package concurrentcube;
import concurrentcube.Monitor;
import concurrentcube.GroupType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.function.BiConsumer;
public class Cube {
private final int size;
private final ArrayList<ArrayList<ArrayList<Integer>>> sides;
private final BiConsumer<Integer, Integer> beforeRotation;
private final BiConsumer<Integer, Integer> afterRotation;
private final Runnable beforeShowing;
private final Runnable afterShowing;
private final int[] oppositeSide = {5, 3, 4, 1, 2, 0};
private final Monitor monitor;
public Cube(int size,
BiConsumer<Integer, Integer> beforeRotation,
BiConsumer<Integer, Integer> afterRotation,
Runnable beforeShowing,
Runnable afterShowing
) {
this.size = size;
this.beforeRotation = beforeRotation;
this.afterRotation = afterRotation;
this.beforeShowing = beforeShowing;
this.afterShowing = afterShowing;
this.sides = new ArrayList<>();
for (int i = 0; i < 6; i++) {
sides.add(new ArrayList<>(size));
for (int j = 0; j < size; j++) {
sides.get(i).add(new ArrayList<>(size));
}
}
for (int i = 0; i < 6; i++) {
for (int j = 0; j < size; j++) {
for (int k = 0; k < size; k++) {
sides.get(i).get(j).add(i);
}
}
}
this.monitor = new Monitor(size);
}
public int getSize() {
return size;
}
private ArrayList<Integer> getRow(int side, int number) {
ArrayList<Integer> result = new ArrayList<Integer>();
for (int c = 0; c < size; c++) {
result.add(sides.get(side).get(c).get(number));
}
return result;
}
private ArrayList<Integer> getReversedRow(int side, int number) {
ArrayList<Integer> result = new ArrayList<Integer>();
for (int c = size - 1; c >= 0; c--) {
result.add(sides.get(side).get(c).get(number));
}
return result;
}
public ArrayList<Integer> getColumn(int side, int number) {
return sides.get(side).get(number);
}
private ArrayList<Integer> getReversedColumn(int side, int number) {
ArrayList<Integer> result = sides.get(side).get(number);
Collections.reverse(result);
return result;
}
private void replaceValue(int side, int a, int b, int c, int d) {
sides.get(side).get(a).set(b, sides.get(side).get(c).get(d));
}
private void rotateSideTopLayer(int side) throws InterruptedException {
for (int i = 0; i < (size + 1) / 2; i++) {
for (int j = 0; j < size / 2; j++) {
int temp = sides.get(side).get(i).get(size - 1 - j);
replaceValue(side, i, size - 1 - j, size - j - 1, size - 1 - i);
replaceValue(side, size - j - 1, size - 1 - i, size - 1 - i, j);
replaceValue(side, size - 1 - i, j, j, i);
sides.get(side).get(j).set(i, temp);
}
}
}
private void rotateSideTopLayerAntiClockwise(int side) throws InterruptedException {
for (int i = 0; i < 3; i++) {
rotateSideTopLayer(side);
}
}
private void replaceRow(int oldRowSide, int layer, ArrayList<Integer> newRow) throws InterruptedException {
for (int i = 0; i < size; i++) {
((sides.get(oldRowSide)).get(i)).set(layer, newRow.get(i));
}
}
private void replaceColumn(int oldSide, int colNum, ArrayList<Integer> newColumn) throws InterruptedException {
sides.get(oldSide).set(colNum, newColumn);
}
private void rotateTop(int layer) throws InterruptedException {
ArrayList<Integer> tempRow = getRow(1, layer);
for (int i = 1; i < 4; i++) {
replaceRow(i, layer, getRow(i + 1, layer));
}
replaceRow(4, layer, tempRow);
}
private void rotateBottom(int layer) throws InterruptedException {
layer = size - 1 - layer;
ArrayList<Integer> tempRow = getRow(4, layer);
for (int i = 4; i > 1; i--) {
ArrayList<Integer> newRow = getRow(i - 1, layer);
replaceRow(i, layer, newRow);
}
replaceRow(1, layer, tempRow);
}
private void rotateFront(int layer) throws InterruptedException {
int complement = size - 1 - layer;
ArrayList<Integer> tempColumn = getRow(0, complement);
replaceRow(0, complement, getReversedColumn(1, complement));
replaceColumn(1, complement, getRow(5, layer));
replaceRow(5, layer, getReversedColumn(3, layer));
replaceColumn(3, layer, tempColumn);
}
private void rotateBack(int layer) throws InterruptedException {
int complement = size - 1 - layer;
ArrayList<Integer> tempColumn = getReversedRow(0, layer);
replaceRow(0, layer, getColumn(3, complement));
replaceColumn(3, complement, getReversedRow(5, complement));
replaceRow(5, complement, getColumn(1, layer));
replaceColumn(1, layer, tempColumn);
}
private void rotateLeft(int layer) throws InterruptedException {
int complement = size - 1 - layer;
ArrayList<Integer> tempColumn = getColumn(0, layer);
replaceColumn(0, layer, getReversedColumn(4, complement));
replaceColumn(4, complement, getReversedColumn(5, layer));
replaceColumn(5, layer, getColumn(2, layer));
replaceColumn(2, layer, tempColumn);
}
private void rotateRight(int layer) throws InterruptedException {
int complement = size - 1 - layer;
ArrayList<Integer> tempColumn = getReversedColumn(0, complement);
replaceColumn(0, complement, getColumn(2, complement));
replaceColumn(2, complement, getColumn(5, complement));
replaceColumn(5, complement, getReversedColumn(4, layer));
replaceColumn(4, layer, tempColumn);
}
public void rotate(int side, int layer) throws InterruptedException {
if (Thread.currentThread().isInterrupted()) throw new InterruptedException();
int standardisedLayer = monitor.getStandardisedLayer(side, layer, size);
monitor.syncGroupsBefore(monitor.getRotateGroupType(side));
monitor.syncBeforeRotate(standardisedLayer);
beforeRotation.accept(side, layer);
if (layer == 0)
rotateSideTopLayer(side);
else if (layer == size - 1)
rotateSideTopLayerAntiClockwise(oppositeSide[side]);
switch (side) {
case 0:
rotateTop(layer);
break;
case 1:
rotateLeft(layer);
break;
case 2:
rotateFront(layer);
break;
case 3:
rotateRight(layer);
break;
case 4:
rotateBack(layer);
break;
default:
rotateBottom(layer);
}
afterRotation.accept(side, layer);
monitor.syncAfterRotate(standardisedLayer);
monitor.syncGroupsAfter();
}
public String show() throws InterruptedException {
if (Thread.currentThread().isInterrupted()) throw new InterruptedException();
monitor.syncGroupsBefore(GroupType.SHOW);
beforeShowing.run();
StringBuilder cubeView = new StringBuilder();
for (int i = 0; i < 6; i++) {
for (int r = 0; r < size; r++) {
for (int c = 0; c < size; c++) {
cubeView.append(sides.get(i).get(c).get(r).toString());
}
}
}
afterShowing.run();
monitor.syncGroupsAfter();
return cubeView.toString();
}
}