Skip to content
Snippets Groups Projects
Commit 615b1403 authored by Yingjian Wang's avatar Yingjian Wang
Browse files

Update Initialisation.jrag

parent 25617484
No related branches found
No related tags found
No related merge requests found
import java.util.*; import java.util.*;
aspect Initialisation { aspect Initialisation {
/*initialise the Hanoi class with 3 pillars and sumDisk disks /*initialise the Hanoi class with 3 pillars and sumDisk disks
each disk is assigned with size from 1 to sumDisk each disk is assigned with size from 1 to sumDisk
all the disks are on pillar P[0] at the begining*/ all the disks are on pillar P[0] at the begining*/
public boolean Hanoi.Initialisation(int sumDisk) public boolean Hanoi.Initialisation(int sumDisk)
{ {
this.setAmountD(sumDisk); this.setAmountD(sumDisk);
int sumPillar = 3; int sumPillar = 3;
Disk[] D = new Disk[sumDisk]; Disk[] D = new Disk[sumDisk];
Pillar[] P = new Pillar[3]; Pillar[] P = new Pillar[3];
for(int i = 0; i < 3; i++) for(int i = 0; i < 3; i++)
{ {
P[i] = new Pillar(); P[i] = new Pillar();
} }
for(int i = 0; i < sumDisk; i++) for(int i = 0; i < sumDisk; i++)
{ {
D[i] = new Disk(i + 1); D[i] = new Disk(i + 1);
System.out.println("Disk: " + i + "; size: " + D[i].getSize()); System.out.println("Disk: " + i + "; size: " + D[i].getSize());
} }
for(int i = sumDisk - 1; i >= 0; i--) for(int i = sumDisk - 1; i >= 0; i--)
{ {
P[0].addDisk(D[i]); P[0].addDisk(D[i]);
} }
for(int i = 0; i < 3; i++) for(int i = 0; i < 3; i++)
{ {
this.addPillar(P[i]); this.addPillar(P[i]);
} }
this.printResult(); this.printResult();
return true; return true;
} }
//print the result //print the result
public boolean Hanoi.printResult() public boolean Hanoi.printResult()
{ {
int sumPillar = 3; int sumPillar = 3;
for(int i = 0; i < 3; i++) for(int i = 0; i < 3; i++)
{ {
if(this.getPillar(i).getNumDisk() > 0){ if(this.getPillar(i).getNumDisk() > 0){
System.out.println("Pillar_" + i + " has Disks:"); System.out.println("Pillar_" + i + " has Disks:");
for(int j = 0; j < this.getAmountD(); j++){ for(int j = 0; j < this.getAmountD(); j++){
int temp = this.getPillar(i).getDisk(j).getSize(); int temp = this.getPillar(i).getDisk(j).getSize();
System.out.println("Disk: " + (temp - 1) + "; Size: " + temp + "."); System.out.println("Disk: " + (temp - 1) + "; Size: " + temp + ".");
} }
} }
else{ else{
System.out.println("Pillar_" + i + " is empty."); System.out.println("Pillar_" + i + " is empty.");
} }
} }
return true; return true;
} }
public boolean Hanoi.play(){ public boolean Hanoi.play(){
boolean turn = true;//parity check boolean turn = true;//parity check
int count = 1;//count the turn int count = 1;//count the turn
while(true){ while(true){
//check if all the disks are moved to the last pillar //check if all the disks are moved to the last pillar
if(this.getPillar(2).getNumDisk() == this.getAmountD()){ if(this.getPillar(2).getNumDisk() == this.getAmountD()){
break; break;
} }
for(int i = 0; i < 3; i++){ for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){ for(int j = 0; j < 3; j++){
if(this.getPillar(2).getNumDisk() == this.getAmountD()){ if(this.getPillar(2).getNumDisk() == this.getAmountD()){
break; break;
} }
if(i==j){ if(i==j){
continue; continue;
}else{ }else{
Constraint constraint = new Constraint(); Constraint constraint = new Constraint();
if(turn == true){//odd turn valid move if(turn == true){//odd turn valid move
if(constraint.oddTurn(this.getPillar(i), this.getPillar(j))){ if(constraint.oddTurnMove(this.getPillar(i), this.getPillar(j))){
this.getPillar(i).moveTo(this.getPillar(j)); this.getPillar(i).moveTo(this.getPillar(j));
System.out.println("Disk_" + (this.getPillar(j).getDisk(this.getPillar(j).getNumDisk()-1).getSize()-1) + ": P" + i + "->P" + j); System.out.println("Disk_" + (this.getPillar(j).getDisk(this.getPillar(j).getNumDisk()-1).getSize()-1) + ": P" + i + "->P" + j);
System.out.println("Round: " + count++); System.out.println("Round: " + count++);
turn = !turn;//change turn turn = !turn;//change turn
break;} break;}
}else if(constraint.evenTurn(this.getPillar(i), this.getPillar(j))){//even turn and valid move }else if(constraint.evenTurn(this.getPillar(i), this.getPillar(j))){//even turn and valid move
//Origin != location since we don't move the smallest disk in even turns //Origin != location since we don't move the smallest disk in even turns
this.getPillar(i).moveTo(this.getPillar(j)); this.getPillar(i).moveTo(this.getPillar(j));
System.out.println("Disk_" + (this.getPillar(j).getDisk(this.getPillar(j).getNumDisk()-1).getSize()-1) + ": P" + i + "->P" + j); System.out.println("Disk_" + (this.getPillar(j).getDisk(this.getPillar(j).getNumDisk()-1).getSize()-1) + ": P" + i + "->P" + j);
System.out.println("Round: " + count++); System.out.println("Round: " + count++);
turn = !turn; turn = !turn;
break; break;
} }
} }
} }
} }
} }
return true; return true;
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment