From 615b14038b7042c877183744a059460c781b5924 Mon Sep 17 00:00:00 2001 From: Yingjian Wang <yingjian.wang@mailbox.tu-dresden.de> Date: Wed, 19 Jan 2022 00:12:21 +0100 Subject: [PATCH] Update Initialisation.jrag --- src/main/jastadd/hanoi/Initialisation.jrag | 178 ++++++++++----------- 1 file changed, 89 insertions(+), 89 deletions(-) diff --git a/src/main/jastadd/hanoi/Initialisation.jrag b/src/main/jastadd/hanoi/Initialisation.jrag index f0a1806..6c59137 100644 --- a/src/main/jastadd/hanoi/Initialisation.jrag +++ b/src/main/jastadd/hanoi/Initialisation.jrag @@ -1,89 +1,89 @@ -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; + } +} -- GitLab