Javatpoint Logo
Javatpoint Logo

Sliding Puzzle Game in Java

The sliding puzzle game is a classic and entertaining puzzle that has captivated people for generations. The objective of the game is to arrange the numbered tiles in the correct order by sliding them into the empty space. It seemingly simple task becomes increasingly challenging as the size of the puzzle increases.

In this section, we will explore how to create a sliding puzzle game in Java. We will cover the basic concepts of the game, the implementation of the puzzle, and the graphical user interface (GUI) using Java Swing.

Understanding the Sliding Puzzle

The sliding puzzle consists of a square grid with numbered tiles, usually arranged in random order. The grid has one empty space that allows tiles to slide into it. The goal is to rearrange the tiles by sliding them into the empty space until they are ordered sequentially.

For implementation, we will create a Puzzle class to represent the game board. The class will include methods to shuffle the tiles, move them, and check whether the puzzle is solved.


Let's start by creating a Tile class to represent each numbered tile. Each tile will have a value and a position on the grid.

Now, let's create the Puzzle class that includes the grid, methods for shuffling, moving tiles, and checking for a solved puzzle.

Creating the GUI for Puzzle

Now, we have our puzzle logic. Let's create a graphical user interface using Java Swing. We will use a JFrame to represent the game window and JButton elements for each tile.

In the createGUI method, you can create and arrange JButton elements for each tile, set their appearance, and add action listeners to handle tile movements.

Creating a complete Java program for a sliding puzzle game with a graphical user interface involves a significant amount of code. Below is a simplified version of the code, including the Tile, Puzzle, and PuzzleGUI classes. Note that the code is simplified for illustration purposes and may require further refinement for a production-level application.

File Name:


Sliding Puzzle Game in Java

The code provides a basic structure for a sliding puzzle game in Java using Swing. Note that the moveTile() and isSolved() methods in the Puzzle class need to be implemented according to the specific logic of the sliding puzzle game. Additionally, we can enhance the GUI and add more features to make the game more interactive and visually appealing.


Creating a sliding puzzle game in Java provides a great opportunity to apply object-oriented programming principles and learn about graphical user interfaces. The project can be expanded by adding features like a timer, move counter, or different puzzle sizes. By completing this project, we will not only enhance your Java skills but also have a fun and interactive game to share with others. Happy coding!

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA