Skip to content

Instantly share code, notes, and snippets.

@ariyike
Last active June 5, 2018 09:15
Show Gist options
  • Save ariyike/4b65da4c8f70cc115a4e4fa3f9d17bf1 to your computer and use it in GitHub Desktop.
Save ariyike/4b65da4c8f70cc115a4e4fa3f9d17bf1 to your computer and use it in GitHub Desktop.
This code models the Tower of Hanoi games by playing it in the optimal number of moves for any given number of discs and output the number of moves.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkgclass.projects;
/*package whatever //do not write package name here */
import java.util.Stack;
import java.util.ArrayList;
import java.util.Scanner;
/**
*
* @author Morema and Ariyike
*/
public class TowerOfHanoi{
int size;
Stack<Integer> tower1;//We should initialize with user input.
Stack<Integer> tower2;
Stack<Integer> tower3;
ArrayList<Stack<Integer>> stacks;
int smallest = 0; //tracks the index of tower containing smallest disk.
int direction;
public static void main (String[] args) {
Scanner in = new Scanner(System.in);
int input = in.nextInt();
TowerOfHanoi game = new TowerOfHanoi(input);
int dir = game.direction;
int moves = 0;
while (moves<Math.pow(2,input)-1){
if (moves%2==0){
game.moveSmallest(dir);
}
else{
game.moveOther();
}
moves++;
}
while (game.tower3.isEmpty()!=true){
System.out.println(game.tower3.pop());
}
System.out.println("The number of moves: "+moves);
}
public TowerOfHanoi(int size){
this.size = size;
this.tower1 = new Stack<>();
for (int disk=size;disk!=0;disk--){
this.tower1.push(disk);
}
this.tower2 = new Stack<>();
this.tower3 = new Stack<>();
this.stacks = new ArrayList<>();
this.stacks.add(this.tower1);
this.stacks.add(this.tower2);
this.stacks.add(this.tower3);
this.direction = this.getDirection(size);
// System.out.println(this.stacks.toString());
// System.out.println(this.tower1.toString());
// System.out.println(this.tower2.toString());
// System.out.println(this.tower3.toString());
}
private void moveSmallest(int direction){
Stack<Integer> towerWithSmall = this.stacks.get(this.smallest);
this.smallest = (this.smallest+direction+3)%3 ;
// System.out.println(smallest);
Stack<Integer> nextTower = this.stacks.get(this.smallest);
nextTower.push(towerWithSmall.pop());
}
private void moveOther(){
int notSmallest1 = (this.smallest+1)%3;
int notSmallest2=(this.smallest+2)%3;
Stack<Integer> otherTower1 = this.stacks.get(notSmallest1);
Stack<Integer> otherTower2 = this.stacks.get(notSmallest2);
if (otherTower1.isEmpty()){
otherTower1.push(otherTower2.pop());
}
else if(otherTower2.isEmpty()){
otherTower2.push(otherTower1.pop());
}
else {
int other1 = otherTower1.peek();
int other2 = otherTower2.peek();
if (other1<other2){
otherTower2.push(otherTower1.pop());
}
else{
otherTower1.push(otherTower2.pop());
}
}
}
private int getDirection(int size){
if (size%2==1){
return -1;
}
return 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment