Java project on Artificial Intelligence

Build an 8-Puzzle Solver – the perfect Java project on artificial intelligence! Learn A search, BFS, and DFS algorithms while creating an interactive sliding tile game with GUI. Includes full source code, step-by-step guide, and cool features like dark mode & animations.

You can get code of Java project on artificial intelligence from my Git Hub repository

Core Features of Learn AI with a fun puzzle game

  1. Multiple Solving Algorithms:
    • Manhattan Distance Heuristic: Optimizes the solution by considering the sum of distances each tile is from its goal position.
    • Hamming Distance Heuristic: Counts the number of misplaced tiles.
    • Breadth-First Search (BFS): Ensures the shortest path to the solution.
    • Depth-First Search (DFS): Explores paths deeply before backtracking.
    • Bidirectional BFS: Searches from both the initial and goal states to reduce computation time.
    • Iterative Deepening A (IDA)**: Dynamically adjusts depth limits to optimize performance.
  2. Graphical User Interface (GUI):
    • The solver incorporates a modern, user-friendly interface built using Java Swing.
    • It features rounded buttons, dynamic animations, and a theme-based color palette (white and blue) for an aesthetically pleasing experience.
    • The interface also includes real-time stats displaying the number of nodes expanded and time taken to find the solution.
  3. Smooth Animation & Sound Effects:
    • The movement of tiles is animated for better visualization.
    • Each move is accompanied by subtle sound effects for enhanced interaction.
    • A success sound is played upon solving the puzzle.
  4. Adaptive Speed Controls:
    • A speed slider allows users to control the animation speed dynamically.
    • A pause and step mode provides better control over the solution visualization.

Implementation Details

The solver works by utilizing a priority queue for algorithms like A* and BFS, ensuring an optimal path to the solution. Each board state is stored as a node with its associated cost function. The heuristic selected determines the way each state is evaluated, enabling more efficient solving methods. The code ensures that unsolvable puzzles are detected beforehand, preventing unnecessary computations.

Java project on artificial intelligence

Key Features of Our 8-Puzzle Solver

1. Graphical User Interface (GUI)

The application features a Swing-based GUI that visually represents the puzzle grid, allowing users to interact with it seamlessly. It includes:

  • Tile Buttons – Representing puzzle numbers with an empty space for movement.
  • Control Panel – Buttons to shuffle, solve, pause, or step through the solution.
  • Statistics Display – Showing solution steps, nodes expanded, and elapsed time.

Key Features:

  1. Interactive Puzzle Board
    • Tiles are JButton objects.
    • Clicking adjacent tiles moves them manually.
    javaCopybuttons[i][j].addActionListener(e -> { if (isAdjacent(row, col, x, y)) { swapTiles(row, col, x, y); // Move tile updateBoard(); // Refresh UI } });
  2. Algorithm Selection (JComboBox)javaCopyString[] ALGORITHMS = {“A* (Manhattan)”, “A* (Hamming)”, “BFS”, “DFS”, “Bidirectional BFS”, “IDA*”}; JComboBox<String> algorithmSelector = new JComboBox<>(ALGORITHMS);
  3. Animation System (Timer)
    • Steps through the solution at adjustable speed.
    javaCopyanimationTimer = new Timer(speedSlider.getValue(), e -> { if (!isPaused && currentStep < solutionSteps.size()) { updateBoard(solutionSteps.get(currentStep++)); } });
  4. Dark Mode Toggle
    • Changes UI colors dynamically.
    javaCopyprivate void toggleDarkMode(boolean enabled) { Color bgColor = enabled ? new Color(60, 63, 65) : Color.WHITE; frame.getContentPane().setBackground(bgColor); }
  5. Sound Effects (SoundPlayer class)
    • Plays move/shuffle/success sounds.
    javaCopyif (soundEnabled) soundPlayer.playMoveSound();

2. Efficient Puzzle Solver Implementation

The solver uses a priority queue and heuristic-based search to efficiently explore possible moves and find the optimal solution. It also:

  • Supports multiple heuristic functions for better path estimation.
  • Expands nodes efficiently while keeping track of visited states to avoid redundant calculations.
  • Provides an animation of the solution step by step for a better visual understanding.

3. Smooth Animations and Sound Effects

To enhance user experience, the solver includes:

  • Animated Tile Movements – Displaying the transitions step by step.
  • Sound Effects – Indicating tile movement and successful solving.

4. Pause and Step Execution

The solver allows users to:

  • Pause and resume an ongoing solution process.
  • Step through each move manually, making it easier to analyze the solving process.

5. Checking Puzzle Solvability

Since not all 8-puzzle configurations are solvable, the application includes a function to verify solvability before attempting to solve it. It does so by:

  • Counting inversions in the puzzle state.
  • Checking if the count is even (indicating solvability).

How the Solution Works

  1. Starting the Game
    • When you launch the program, you’ll see a 3×3 grid with numbered tiles (1-8) and one empty space
    • The tiles are initially in a solvable configuration
  2. Making Moves
    • Click any tile that’s directly next to the empty space (up, down, left, or right)
    • The tile will slide into the empty space
    • Example: If “5” is next to the empty spot, clicking “5” will move it
  3. Winning the Game
    • Arrange all tiles in order from 1-8
    • The empty space should be in the bottom-right corner
How to solve 8-puzzle using A* in Java

Using the Solver Features

  1. Shuffle Button
    • Creates a new random puzzle (always solvable)
    • Great for practicing or trying different challenges
  2. Solve Options
    • Select an algorithm from the dropdown menu:
      • A (Manhattan): Smartest solver (recommended)
      • BFS: Finds shortest solution (but slower)
      • DFS: Fast but may not find best path
    • Click “Solve Puzzle” to see the computer solve it
  3. Watching Solutions
    • The solver shows each move step-by-step
    • Use the speed slider to make it faster/slower
    • Pause anytime with the pause button

How It Works (Technical Insight)

  1. Smart Solving
    • The program checks if the puzzle is solvable using math (counting inversions)
    • Different algorithms explore possible moves:
      • A* uses “Manhattan Distance” to estimate how close tiles are to their correct spots
      • BFS tries every possible move in order
      • DFS goes deep down one path before trying others
  2. Visual Feedback
    • Green highlight shows which tile just moved
    • The step counter shows progress
    • Stats display how many moves were considered
  3. Behind the Scenes
    • The solver runs in a separate thread so the game stays responsive
    • Your moves and the computer’s moves use the same rules

Tips for Players

  • Use dark mode if you prefer a darker color scheme
  • Start with manual solving to understand the puzzle
  • Try the “Step” button to move through solutions one move at a time
  • Compare how different algorithms solve the same puzzle

Enhancing User Experience

  • The GUI layout is responsive and adjusts elements dynamically.
  • A dark mode toggle is available for user preference.
  • Tooltip descriptions for heuristic selection help users understand the algorithm choices.

Conclusion

This 8-Puzzle Solver is a powerful yet user-friendly Java application that brings the classic sliding puzzle to life with intelligent solving algorithms, interactive gameplay, and real-time visualizations. Whether you’re manually moving tiles or letting AI solve it using BFS, DFS, or A* search, the game provides an engaging way to understand heuristic-based problem-solving while tracking performance metrics like move count and solving time. With features like adjustable animation speed, dark/light mode, and step-by-step solutions, it bridges entertainment and education—perfect for puzzle lovers and programming enthusiasts alike. 

Leave a comment