# Tower of Hanoi Game in Java

Problem: Write a program to solve the Tower of Hanoi puzzle, which involves moving disks from one peg to another while obeying certain rules.

The Tower of Hanoi puzzle is a timeless challenge where players move disks between pegs following strict rules. It’s a captivating game that highlights key concepts like recursion and problem-solving strategies. In this article, we’ll explore the puzzle’s rules, and its importance in computational theory, and present a Java program that elegantly solves it using recursive techniques.

## Tower of Hanoi Game Rules

The Tower of Hanoi puzzle consists of three pegs and several disks of different sizes that can slide onto any peg. The puzzle starts with all the disks stacked in ascending order of size on one peg, with the smallest disk at the top and the largest at the bottom. The objective of the puzzle is to move the entire stack to another peg, adhering to the following rules:

1. Only one disk can be moved at a time.
2. Each move consists of taking the top disk from one of the stacks and placing it on top of another.
3. No disk may be placed on top of a smaller disk.

Below is a Java program to solve the Tower of Hanoi puzzle along with explanations:

```package com.javacodepoint.game;

import java.util.Scanner;

public class TowerOfHanoi {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of disks: ");
int numberOfDisks = scanner.nextInt();
towerOfHanoi(numberOfDisks, 'A', 'C', 'B');
scanner.close();
}

public static void towerOfHanoi(int disks, char source, char destination, char auxiliary) {
if (disks == 1) {
System.out.println("Move disk 1 from peg " + source + " to peg " + destination);
return;
}
towerOfHanoi(disks - 1, source, auxiliary, destination);
System.out.println("Move disk " + disks + " from peg " + source + " to peg " + destination);
towerOfHanoi(disks - 1, auxiliary, destination, source);
}
}

```

OUTPUT:

Explanation:

• The `towerOfHanoi` method is a recursive function that takes four parameters:
• `disks`: The number of disks to be moved.
• `source`: The peg from which we move the disks.
• `destination`: The peg to which we move the disks.
• `auxiliary`: The auxiliary peg used for moving disks.
• The base case of the recursion is when there is only one disk to be moved. In this case, we simply move the disk from the source peg to the destination peg.
• In the recursive case, we move `disks - 1` disks from the source peg to the auxiliary peg using the destination peg as an auxiliary. Then, we move the last disk (the largest one) from the source peg to the destination peg. Finally, we move `disks - 1` disks from the auxiliary peg to the destination peg using the source peg as an auxiliary.
• The `main` method prompts the user to input the number of disks, calls the `towerOfHanoi` method with the appropriate parameters, and prints the sequence of moves required to solve the puzzle.

## Conclusion

The Tower of Hanoi puzzle teaches us about problem-solving using recursion. The game’s rules are straightforward: move disks from one peg to another, one at a time, without placing a larger disk on top of a smaller one. Our Java program demonstrates how to solve this puzzle step by step. By breaking the problem into smaller parts, we can solve it effectively. This program shows the beauty of logical thinking and helps us understand how computers solve complex problems. The Tower of Hanoi puzzle is a fun and educational challenge highlighting the power of systematic problem-solving.

See also:Â Tic-Tac-Toe Game | Generating OTP in Java.Â |Â Search Matrix.Â |Â Pattern programs [Tricks]