Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hanoi
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Martin Kaller
hanoi
Commits
8f84db8a
Commit
8f84db8a
authored
4 years ago
by
Martin Kaller
Browse files
Options
Downloads
Patches
Plain Diff
Made hanoi tower playable
parent
f3378592
No related branches found
No related tags found
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
5.txt
+159
-0
159 additions, 0 deletions
5.txt
Hanoi.class
+0
-0
0 additions, 0 deletions
Hanoi.class
Hanoi.java
+42
-16
42 additions, 16 deletions
Hanoi.java
Main.class
+0
-0
0 additions, 0 deletions
Main.class
Main.java
+23
-3
23 additions, 3 deletions
Main.java
with
224 additions
and
19 deletions
5.txt
0 → 100644
+
159
−
0
View file @
8f84db8a
A: [5, 4, 3, 2, 1]
B: []
C: []
Move disc 1 from A to C
A: [5, 4, 3, 2]
B: []
C: [1]
Move disc 2 from A to B
A: [5, 4, 3]
B: [2]
C: [1]
Move disc 1 from C to B
A: [5, 4, 3]
B: [2, 1]
C: []
Move disc 3 from A to C
A: [5, 4]
B: [2, 1]
C: [3]
Move disc 1 from B to A
A: [5, 4, 1]
B: [2]
C: [3]
Move disc 2 from B to C
A: [5, 4, 1]
B: []
C: [3, 2]
Move disc 1 from A to C
A: [5, 4]
B: []
C: [3, 2, 1]
Move disc 4 from A to B
A: [5]
B: [4]
C: [3, 2, 1]
Move disc 1 from C to B
A: [5]
B: [4, 1]
C: [3, 2]
Move disc 2 from C to A
A: [5, 2]
B: [4, 1]
C: [3]
Move disc 1 from B to A
A: [5, 2, 1]
B: [4]
C: [3]
Move disc 3 from C to B
A: [5, 2, 1]
B: [4, 3]
C: []
Move disc 1 from A to C
A: [5, 2]
B: [4, 3]
C: [1]
Move disc 2 from A to B
A: [5]
B: [4, 3, 2]
C: [1]
Move disc 1 from C to B
A: [5]
B: [4, 3, 2, 1]
C: []
Move disc 5 from A to C
A: []
B: [4, 3, 2, 1]
C: [5]
Move disc 1 from B to A
A: [1]
B: [4, 3, 2]
C: [5]
Move disc 2 from B to C
A: [1]
B: [4, 3]
C: [5, 2]
Move disc 1 from A to C
A: []
B: [4, 3]
C: [5, 2, 1]
Move disc 3 from B to A
A: [3]
B: [4]
C: [5, 2, 1]
Move disc 1 from C to B
A: [3]
B: [4, 1]
C: [5, 2]
Move disc 2 from C to A
A: [3, 2]
B: [4, 1]
C: [5]
Move disc 1 from B to A
A: [3, 2, 1]
B: [4]
C: [5]
Move disc 4 from B to C
A: [3, 2, 1]
B: []
C: [5, 4]
Move disc 1 from A to C
A: [3, 2]
B: []
C: [5, 4, 1]
Move disc 2 from A to B
A: [3]
B: [2]
C: [5, 4, 1]
Move disc 1 from C to B
A: [3]
B: [2, 1]
C: [5, 4]
Move disc 3 from A to C
A: []
B: [2, 1]
C: [5, 4, 3]
Move disc 1 from B to A
A: [1]
B: [2]
C: [5, 4, 3]
Move disc 2 from B to C
A: [1]
B: []
C: [5, 4, 3, 2]
Move disc 1 from A to C
A: []
B: []
C: [5, 4, 3, 2, 1]
This diff is collapsed.
Click to expand it.
Hanoi.class
+
0
−
0
View file @
8f84db8a
No preview for this file type
This diff is collapsed.
Click to expand it.
Hanoi.java
+
42
−
16
View file @
8f84db8a
import
java.util.*
;
public
class
Hanoi
{
Stack
<
String
>
a
=
new
Stack
<
String
>();
Stack
<
String
>
b
=
new
Stack
<
String
>();
Stack
<
String
>
c
=
new
Stack
<
String
>();
Stack
<
Integer
>[]
stacks
=
(
Stack
<
Integer
>[])
new
Stack
[
3
];
String
[]
names
=
{
"A"
,
"B"
,
"C"
};
String
output
=
""
;
int
amount
;
public
Hanoi
()
{
public
Hanoi
(
int
amount
)
{
for
(
int
i
=
0
;
i
<
names
.
length
;
i
++)
{
stacks
[
i
]
=
new
Stack
<>();
}
for
(
int
i
=
amount
;
i
>=
1
;
i
--)
{
stacks
[
0
].
push
(
i
);
}
this
.
amount
=
amount
;
printStacks
();
}
...
...
@@ -18,23 +22,35 @@ public class Hanoi {
* @param from
* @param to
*/
p
ublic
void
move
(
int
from
,
int
to
)
{
p
rivate
void
move
(
int
from
,
int
to
)
{
int
disc
=
stacks
[
from
].
pop
();
stacks
[
to
].
push
(
disc
);
if
(
stacks
[
to
].
empty
()
||
disc
<
stacks
[
to
].
peek
()){
stacks
[
to
].
push
(
disc
);
System
.
out
.
println
(
"Move disc "
+
disc
+
" from "
+
names
[
from
]
+
" to "
+
names
[
to
]);
printStacks
();
output
+=
"Move disc "
+
disc
+
" from "
+
names
[
from
]
+
" to "
+
names
[
to
]
+
"\n"
;
printStacks
();
}
else
{
stacks
[
from
].
push
(
disc
);
System
.
out
.
println
(
"lmao no cheating"
);
}
}
public
void
move
(
String
playerFrom
,
String
playerTo
){
int
from
=
0
;
int
to
=
0
;
for
(
int
i
=
0
;
i
<
names
.
length
;
i
++)
{
if
(
names
[
i
].
equals
(
playerFrom
))
from
=
i
;
if
(
names
[
i
].
equals
(
playerTo
))
to
=
i
;
}
move
(
from
,
to
);
amount
++;
}
/**
* @param amount
*/
public
void
start
(
int
amount
)
{
for
(
int
i
=
amount
;
i
>=
1
;
i
--)
{
stacks
[
0
].
push
(
i
);
}
printStacks
();
public
void
start
()
{
hanoi
(
amount
,
0
,
1
,
2
);
}
...
...
@@ -57,10 +73,20 @@ public class Hanoi {
public
void
printStacks
()
{
for
(
int
i
=
0
;
i
<
names
.
length
;
i
++)
{
System
.
out
.
print
(
names
[
i
]+
": "
)
;
System
.
out
.
println
(
stacks
[
i
]);
output
+=
names
[
i
]+
": "
;
output
+=
stacks
[
i
].
toString
()+
"\n"
;
}
System
.
out
.
println
();
output
+=
"\n"
;
}
/**
* @return the output
*/
public
String
getOutput
()
{
String
tmp
=
output
;
output
=
""
;
return
tmp
;
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Main.class
+
0
−
0
View file @
8f84db8a
No preview for this file type
This diff is collapsed.
Click to expand it.
Main.java
+
23
−
3
View file @
8f84db8a
import
java.util.*
;
public
class
Main
{
public
static
void
main
(
String
[]
args
)
{
int
amount
=
Integer
.
parseInt
(
args
[
0
]);
String
mode
=
"game"
;
int
amount
=
4
;
Hanoi
test
=
new
Hanoi
();
test
.
start
(
amount
);
Scanner
input
=
new
Scanner
(
System
.
in
);
String
from
,
to
;
Hanoi
hanoi
=
new
Hanoi
(
amount
);
if
(
mode
.
equals
(
"game"
)){
System
.
out
.
println
(
hanoi
.
getOutput
());
while
(
true
){
System
.
out
.
print
(
"Flytta från: "
);
from
=
input
.
nextLine
();
System
.
out
.
print
(
"Flytta till: "
);
to
=
input
.
nextLine
();
System
.
out
.
println
();
hanoi
.
move
(
from
,
to
);
System
.
out
.
println
(
hanoi
.
getOutput
());
}
}
else
{
hanoi
.
start
();
System
.
out
.
println
(
hanoi
.
getOutput
());
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment