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