-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTower.java
177 lines (163 loc) · 5.12 KB
/
Tower.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
package ch.hslu.prg1.allg.hanoi;
import java.util.ArrayList;
import java.util.Collections;
/**
* In this class the Tower will be generated for a "Tower if Hanoi"-Simulation.
* at first the 3 poles will be drawn on a Canvas. Then the Tower will be drawn,
* possible with 1 - 8 blocks. This tower will then be transferred from one pole
* to another pole via a third pole.
*
* @Rules - During this transfer only one block at once can be moved<br>
* - Each moved block should then be placed in a pole again.<br>
* - A bigger block can't be put on a smaller block.
*
*/
public class Tower {
private int amount; // Number of blocks
private int top; // where the Poles start from above
private int fullHeight; // height of the poles
private String[] shades; // colors of the blocks, they will get from the
// Canvas-class
private ArrayList<Square> allParts; // this list saves all blocks which are
// generated
private ArrayList<Square> towerA; // here all blocks will be added which are
// located at the first pole.
private ArrayList<Square> towerB; // same here for the second block.
private ArrayList<Square> towerC; // all blocks which are placed on the
// third block.
private int height;
private int yPos;
private int w;
public Tower(int num) {
amount = num;
top = 80;
fullHeight = 160;
allParts = new ArrayList<Square>();
shades = new String[8];
shades[0] = "white";
shades[1] = "fairGrey";
shades[2] = "blue";
shades[3] = "green";
shades[4] = "yellow";
shades[5] = "orange";
shades[6] = "red";
shades[7] = "lila";
towerA = new ArrayList<Square>();
towerB = new ArrayList<Square>();
towerC = new ArrayList<Square>();
height = 20;
yPos = top + fullHeight;
w = 20;
}
/**
* Creating the poles..
*/
public void createPoles() {
Square poleA = new Square(6, fullHeight, 150, top, "black");
Square poleB = new Square(6, fullHeight, 350, top, "black");
Square poleC = new Square(6, fullHeight, 550, top, "black");
poleA.makeVisible();
poleB.makeVisible();
poleC.makeVisible();
}
/**
* Creating the tower..
*/
public void createTower() {
int size = 20;
int xPos = 143;
int colNum = 0;
for (int i = 0; i < amount; i++) {
allParts.add(new Square(size, height, xPos, yPos, shades[colNum]));
allParts.get(i).makeVisible();
size += 20;
xPos -= 10;
colNum++;
for (int k = 0; k <= i; k++) {
allParts.get(i - k).moveVertical(-height);
}
}
towerA.addAll(allParts);
Collections.reverse(towerA);
timeout(w * 10);
}
/**
* action!
*/
public void runMove() {
moveDisks("A", "B", "C", amount);
}
/**
* This is a recursive algorithm who will do all the work
*
* @param from
* start pole
* @param via
* pole between
* @param to
* destination pole
* @param n
* number of blocks
*/
private void moveDisks(String from, String via, String to, int n) {
if (n == 1) {
/**
* ;
*
*/
if (from.equals("A") && to.equals("C")) {
int mV = yPos - height - towerA.get(towerA.size() - 1).getYPos() - towerC.size() * height;
towerA.get(towerA.size() - 1).moveDiag(400, mV);
towerC.add(towerA.get(towerA.size() - 1));
towerA.remove(towerA.size() - 1);
timeout(w);
} else if (from.equals("A") && to.equals("B")) {
int mV = yPos - height - towerA.get(towerA.size() - 1).getYPos() - towerB.size() * height;
towerA.get(towerA.size() - 1).moveDiag(200, mV);
towerB.add(towerA.get(towerA.size() - 1));
towerA.remove(towerA.size() - 1);
timeout(w);
} else if (from.equals("B") && to.equals("C")) {
int mV = yPos - height - towerB.get(towerB.size() - 1).getYPos() - towerC.size() * height;
towerB.get(towerB.size() - 1).moveDiag(200, mV);
towerC.add(towerB.get(towerB.size() - 1));
towerB.remove(towerB.size() - 1);
timeout(w);
} else if (from.equals("B") && to.equals("A")) {
int mV = yPos - height - towerB.get(towerB.size() - 1).getYPos() - towerA.size() * height;
towerB.get(towerB.size() - 1).moveDiag(-200, mV);
towerA.add(towerB.get(towerB.size() - 1));
towerB.remove(towerB.size() - 1);
timeout(w);
} else if (from.equals("C") && to.equals("A")) {
int mV = yPos - height - towerC.get(towerC.size() - 1).getYPos() - towerA.size() * height;
towerC.get(towerC.size() - 1).moveDiag(-400, mV);
towerA.add(towerC.get(towerC.size() - 1));
towerC.remove(towerC.size() - 1);
timeout(w);
} else if (from.equals("C") && to.equals("B")) {
int mV = yPos - height - towerC.get(towerC.size() - 1).getYPos() - towerB.size() * height;
towerC.get(towerC.size() - 1).moveDiag(-200, mV);
towerB.add(towerC.get(towerC.size() - 1));
towerC.remove(towerC.size() - 1);
timeout(w);
}
} else {
moveDisks(from, to, via, n - 1); // A -> C -> B
moveDisks(from, "", to, 1); // A -> C
moveDisks(via, from, to, n - 1); // B -> A -> C
}
}
/**
* method for short delays thus the animation is not too fast
*
* @param wait
*/
private void timeout(int wait) {
try {
Thread.sleep(wait);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}