Skip to content

Add function documentation, enhance comments in TowerOfHanoi.java #5533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 24 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
887ee0f
Add function documentation, enhance comments in TowerOfHanoi.java
Hardvan Oct 3, 2024
f87e70d
Merge branch 'master' into tower_of_hanoi_improvement
Hardvan Oct 3, 2024
3bedefd
Update directory
Hardvan Oct 3, 2024
a04c2c3
Add wiki link
Hardvan Oct 3, 2024
4b9bea5
Merge branch 'tower_of_hanoi_improvement' of https://github.com/Hardv…
Hardvan Oct 3, 2024
a941a1d
Merge branch 'master' into tower_of_hanoi_improvement
Hardvan Oct 4, 2024
c2f6872
Update directory
Hardvan Oct 4, 2024
10102c7
Fix comments
Hardvan Oct 4, 2024
ca10371
Merge branch 'tower_of_hanoi_improvement' of https://github.com/Hardv…
Hardvan Oct 4, 2024
c0dc63b
Add unit tests for ToH
Hardvan Oct 5, 2024
65b9306
Update directory
Hardvan Oct 5, 2024
69e69b5
Fix clang errors
Hardvan Oct 5, 2024
95f4a90
Merge branch 'tower_of_hanoi_improvement' of https://github.com/Hardv…
Hardvan Oct 5, 2024
a4ec1ae
Merge branch 'master' into tower_of_hanoi_improvement
Hardvan Oct 5, 2024
5d17a20
Update directory
Hardvan Oct 5, 2024
146f3e9
Fix comments
Hardvan Oct 5, 2024
27f7760
Merge branch 'tower_of_hanoi_improvement' of https://github.com/Hardv…
Hardvan Oct 5, 2024
49e42ac
Add suggested changes
Hardvan Oct 6, 2024
ecbc8d1
Merge branch 'master' into tower_of_hanoi_improvement
alxkm Oct 6, 2024
903c69a
Merge branch 'master' into tower_of_hanoi_improvement
Hardvan Oct 7, 2024
496d9e4
Merge branch 'master' into tower_of_hanoi_improvement
alxkm Oct 12, 2024
ba1b548
Update directory
alxkm Oct 12, 2024
cf99ba3
Merge branch 'master' into tower_of_hanoi_improvement
alxkm Oct 13, 2024
e557086
Update directory
alxkm Oct 13, 2024
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
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@
* [KnapsackMemoization](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/KnapsackMemoization.java)
* [LevenshteinDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LevenshteinDistance.java)
* [LongestAlternatingSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequence.java)
* [LongestArithmeticSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequence.java)
* [LongestCommonSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestCommonSubsequence.java)
* [LongestIncreasingSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequence.java)
* [LongestPalindromicSubsequence](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubsequence.java)
Expand Down Expand Up @@ -733,6 +734,7 @@
* [KnapsackTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/KnapsackTest.java)
* [LevenshteinDistanceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LevenshteinDistanceTests.java)
* [LongestAlternatingSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestAlternatingSubsequenceTest.java)
* [LongestArithmeticSubsequenceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestArithmeticSubsequenceTest.java)
* [LongestIncreasingSubsequenceTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestIncreasingSubsequenceTests.java)
* [LongestPalindromicSubstringTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestPalindromicSubstringTest.java)
* [LongestValidParenthesesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/dynamicprogramming/LongestValidParenthesesTest.java)
Expand Down
46 changes: 40 additions & 6 deletions src/main/java/com/thealgorithms/others/TowerOfHanoi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,57 @@

import java.util.Scanner;

/**
* The {@code TowerOfHanoi} class provides functionality to solve the Tower of
* Hanoi puzzle.
* It uses recursion to move discs between poles and prints the steps to solve
* the puzzle.
* The main function interacts with the user to get the number of discs and
* calls the recursive {@code shift} function to perform the moves.
* Wikipedia: https://en.wikipedia.org/wiki/Tower_of_Hanoi
*/
final class TowerOfHanoi {

private TowerOfHanoi() {
}

/**
* Recursively solve the Tower of Hanoi puzzle by moving discs between poles.
*
* @param n The number of discs to move.
* @param startPole The name of the start pole.
* @param intermediatePole The name of the intermediate pole.
* @param endPole The name of the end pole.
*
* <p>
* This method is called recursively to move n-1 discs
* to the intermediate pole,
* then moves the nth disc to the end pole, and finally
* moves the n-1 discs from the
* intermediate pole to the end pole.
* </p>
*/
public static void shift(int n, String startPole, String intermediatePole, String endPole) {
// if n becomes zero the program returns thus ending the loop.
// If n becomes zero, the program returns, ending the recursion.
if (n != 0) {
// Shift function is called in recursion for swapping the n-1 disc from the startPole to
// the intermediatePole
// Recursively move n-1 discs from startPole to intermediatePole
shift(n - 1, startPole, endPole, intermediatePole);
System.out.format("Move %d from %s to %s%n", n, startPole, endPole); // Result Printing
// Shift function is called in recursion for swapping the n-1 disc from the
// intermediatePole to the endPole

// Print the move of the nth disc from startPole to endPole
System.out.format("Move %d from %s to %s%n", n, startPole, endPole);

// Recursively move the n-1 discs from intermediatePole to endPole
shift(n - 1, intermediatePole, startPole, endPole);
}
}

/**
* The main method that starts the Tower of Hanoi puzzle solution.
* It prompts the user for the number of discs and invokes the {@code shift}
* function to begin solving the puzzle.
*
* @param args Command-line arguments (not used in this case).
*/
public static void main(String[] args) {
System.out.print("Enter number of discs on Pole 1: ");
Scanner scanner = new Scanner(System.in);
Expand Down