-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTowerOfHanoi.java
More file actions
223 lines (211 loc) · 5.71 KB
/
TowerOfHanoi.java
File metadata and controls
223 lines (211 loc) · 5.71 KB
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
218
219
220
221
222
223
//Luke Bradaric
import java.util.Scanner;
public class TowerOfHanoi
{
private int a1;
private int a2;
private int a3;
private int b1;
private int b2;
private int b3;
private int c1;
private int c2;
private int c3;
private int temp;
private int tempNum;
private int Cl;
private int moveCl;
private Scanner sc;
public TowerOfHanoi()
{
sc = new Scanner(System.in);
a1 = 1;
a2 = 2;
a3 = 3;
b1 = 0;
b2 = 0;
b3 = 0;
c1 = 0;
c2 = 0;
c3 = 0;
}
public void Play()
{
Display();
}
private void Take(int num)
{
if (num == 1){
if (a1 != 0)
temp = a1;
else if (a2 != 0)
temp = a2;
else if (a3 != 0)
temp = a3;
else
System.out.println("Column empty or an error has occured!");
}
else if (num == 2){
if (b1 != 0)
temp = b1;
else if (b2 != 0)
temp = b2;
else if (b3 != 0)
temp = b3;
else
System.out.println("Column empty or an error has occured!");
}
else if (num == 3){
if (c1 != 0)
temp = c1;
else if (c2 != 0)
temp = c2;
else if (c3 != 0)
temp = c3;
else
System.out.println("Column empty or an error has occured!");
}
else{
System.out.println("Error in your code, retard");
Display();
}
}
private void oldTake(int num)
{
if (num == 1){
if (a1 != 0)
a1 = 0;
else if (a2 != 0)
a2 = 0;
else if (a3 != 0)
a3 = 0;
else
System.out.println("Column empty or an error has occured!");
}
else if (num == 2){
if (b1 != 0)
b1 = 0;
else if (b2 != 0)
b2 = 0;
else if (b3 != 0)
b3 = 0;
else
System.out.println("Column empty or an error has occured!");
}
else if (num == 3){
if (c1 != 0)
c1 = 0;
else if (c2 != 0)
c2 = 0;
else if (c3 != 0)
c3 = 0;
else
System.out.println("Column empty or an error has occured!");
}
else{
System.out.println("Error in your code, retard");
Display();
}
}
private void Place(int num)
{
if(num == 1){
if(a3 == 0){
a3 = temp;
temp = 0;
oldTake(tempNum);
Display();
}
else if(a2 == 0 && a3 > temp){
a2 = temp;
temp = 0;
oldTake(tempNum);
Display();
}
else if(a1 == 0 && a3 > temp && a2 > temp){
a1 = temp;
temp = 0;
oldTake(tempNum);
Display();
}
else{
Display2();
}
}
else if(num == 2){
if(b3 == 0){
b3 = temp;
temp = 0;
oldTake(tempNum);
Display();
}
else if(b2 == 0 && b3 > temp){
b2 = temp;
temp = 0;
oldTake(tempNum);
Display();
}
else if(b1 == 0 && b3 > temp && b2 > temp){
b1 = temp;
temp = 0;
oldTake(tempNum);
Display();
}
else{
Display2();
}
}
else if(num == 3){
if(c3 == 0){
c3 = temp;
temp = 0;
oldTake(tempNum);
Display();
}
else if(c2 == 0 && c3 > temp){
c2 = temp;
temp = 0;
oldTake(tempNum);
Display();
}
else if(c1 == 0 && c3 > temp && c2 > temp){
c1 = temp;
temp = 0;
oldTake(tempNum);
Display();
}
else{
Display2();
}
}
else{
System.out.println("Can't do that");
}
}
private void Display()
{
if(a1 == 0 && a2 == 0 && a3 == 0 && b1 == 0 && b2 == 0 && b3 == 0 && c1 == 1 && c2 == 2 && c3 == 3){
System.out.println(a1 + " " + b1 + " " + c1);
System.out.println(a2 + " " + b2 + " " + c2);
System.out.println(a3 + " " + b3 + " " + c3);
System.out.println("You win!");
}
else{
System.out.println(a1 + " " + b1 + " " + c1);
System.out.println(a2 + " " + b2 + " " + c2);
System.out.println(a3 + " " + b3 + " " + c3);
System.out.println("Input the column you would like to move : ");
Cl = sc.nextInt();
tempNum = Cl;
Take(Cl);
System.out.println("Input the column you would like to move to : ");
moveCl = sc.nextInt();
Place(moveCl);
}
}
private void Display2()
{
System.out.println("Can't make that move.");
Display();
}
}