Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions src/components/Applications/Hanoi/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,26 +53,69 @@ Remember:
// }

export const hanoi = (from, via, to, n, moves) => {
alert("complete hanoi function first!")
if (n==1){
moves.push([from,to]);
}
else{
hanoi(from,to,via,n-1,moves);
moves.push([from,to]);
hanoi(via,from,to,n-1,moves);
}

}

export const exhanoi_1 = (A, B, C, n, moves) => {
alert("complete exhanoi_1 function first!")

if(n==1){
hanoi(C,A,B,2,moves);
moves.push([A,C]);
hanoi(B,A,C,2,moves);
}
else{
exhanoi_1(A,B,C,n-1,moves)
hanoi(C,A,B,3*n-1,moves);
moves.push([A,C]);
hanoi(B,A,C,3*n-1,moves);
}
}

export const exhanoi_2 = (A, B, C, n, moves) => {
alert("complete exhanoi_2 function first!")

if(n==1){
moves.push([A,C]);
moves.push([B,C]);
}
else{
exhanoi_2(A,B,C,n-1,moves);
hanoi(C,A,B,2*n-2,moves);
moves.push([A,C]);
hanoi(B,A,C,2*n-1,moves);
}
}

export const exhanoi_3 = (A, B, C, n, moves) => {
alert("complete exhanoi_3 function first!")

if(n==1){
moves.push([C,B]);
moves.push([A,B]);
hanoi(B,A,C,3,moves);
}
else{
exhanoi_3(A,B,C,n-1,moves)
hanoi(C,B,A,3*n-3,moves);
moves.push([C,B]);
hanoi(A,C,B,3*n-2,moves);
hanoi(B,A,C,3*n,moves);
}
}

export const exhanoi_4 = (A, B, C, n, moves) => {
alert("complete exhanoi_4 function first!")

if(n==1){
moves.push([C,B]);
hanoi(B,C,A,3,moves);
hanoi(A,B,C,6,moves);
}
else{
exhanoi_4(A,B,C,n-1,moves);
hanoi(C,A,B,6*n-5,moves);
hanoi(B,C,A,6*n-3,moves);
hanoi(A,B,C,6*n,moves);
}
}