Skip to content
Snippets Groups Projects
Commit 8f84db8a authored by Martin Kaller's avatar Martin Kaller
Browse files

Made hanoi tower playable

parent f3378592
No related branches found
No related tags found
No related merge requests found
5.txt 0 → 100644
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]
No preview for this file type
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
*/
public void move(int from, int to) {
private 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
No preview for this file type
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());
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment