Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
U
UpdatedHanoi
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Yingjian Wang
UpdatedHanoi
Commits
615b1403
Commit
615b1403
authored
3 years ago
by
Yingjian Wang
Browse files
Options
Downloads
Patches
Plain Diff
Update Initialisation.jrag
parent
25617484
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/jastadd/hanoi/Initialisation.jrag
+89
-89
89 additions, 89 deletions
src/main/jastadd/hanoi/Initialisation.jrag
with
89 additions
and
89 deletions
src/main/jastadd/hanoi/Initialisation.jrag
+
89
−
89
View file @
615b1403
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;
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment