mirror of
https://github.com/MartinThoma/LaTeX-examples.git
synced 2025-04-26 06:48:04 +02:00
Added source files (Programmieren Tutorium)
This commit is contained in:
parent
1be8f17cc0
commit
ad81fa802b
244 changed files with 8252 additions and 0 deletions
|
@ -0,0 +1,42 @@
|
|||
/**
|
||||
This is an aotomatic Hanoi algorithm. It solves a given game.
|
||||
*/
|
||||
public class Hanoi {
|
||||
|
||||
public static void main (String[] args) {
|
||||
Pole a = new Pole(3);
|
||||
Pole b = new Pole(3);
|
||||
Pole c = new Pole(3);
|
||||
Disc d1 = new Disc(1);
|
||||
Disc d2 = new Disc(2);
|
||||
Disc d3 = new Disc(3);
|
||||
|
||||
a.push(d3);
|
||||
a.push(d2);
|
||||
a.push(d1);
|
||||
|
||||
System.out.println("from: \n" + a);
|
||||
System.out.println("help: \n" + b);
|
||||
System.out.println("to: \n" + c);
|
||||
move(a, b, c);
|
||||
System.out.println("from: \n" + a);
|
||||
System.out.println("help: \n" + b);
|
||||
System.out.println("to: \n" + c);
|
||||
}
|
||||
|
||||
public static void move(Pole from, Pole help, Pole to) {
|
||||
if(from == null | to == null || help == null) {
|
||||
throw new IllegalArgumentException("Pole is null");
|
||||
}
|
||||
|
||||
move(from.getSize(), from, help, to);
|
||||
}
|
||||
|
||||
private static void move(int n, Pole from, Pole help, Pole to) {
|
||||
if(n > 0) {
|
||||
move(n - 1, from, to, help);
|
||||
to.push(from.pop());
|
||||
move(n - 1, help, from, to);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue