Micro-project On
PUZZLE GAME IN ANDROID STUDIO
Submitted By:
Srno. Student Name : Roll no:
1. Ritika Anabhavne . 243206
2. Bhumika Jadhav. 243217
3. Samidha Khadye. 243223
For
Mobile application development
(22617)
Faculty Name:
Irshad khan
1
rt: Puzzle Game in Android Studio
1. Project Title
Sliding Puzzle Game (3x3 Tile Game) in Android Studio
2. Objective
The aim of this project is to design and develop a simple sliding
puzzle game for Android using Java and XML in Android Studio.
The game features a 3x3 grid with tiles numbered from 1 to 8
and one empty space. The player must rearrange the tiles into
the correct order by sliding them.
3. Tools and Technologies Used
• IDE: Android Studio
• Programming Language: Java
• XML: For UI design
• Android SDK: Minimum SDK 21 (Lollipop)
• Layout Manager: GridLayout
4. Features of the Game
2
• 3x3 grid puzzle game.
• Randomized (shuffled) tiles at the start.
• Clickable tiles that can move if adjacent to the empty
space.
• Win condition detection.
• Toast message displayed on winning.
5. Application Architecture
• Layout (XML)
• The game interface uses a GridLayout with 9 buttons,
each representing a tile.
• Activity (Java)
• The MainActivity.java handles:
• Initialization of buttons.
• Tile click handling.
• Movement logic.
• Puzzle shuffling.
• Win condition check.
3
6. Core Functionalities and Logic
Shuffling Tiles: Randomly arrange numbers 1 to 8 and an
empty tile.
Tile Movement: Swap tiles only if the selected tile is adjacent
to the empty one.
Win Condition: Check if the tiles are in order from 1 to 8, with
the empty tile at the end.
7. Code
4
Activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
xmlns:android="http://schemas.android.com/apk/res/androi
d"
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="3"
android:rowCount="3"
android:padding="16dp"
android:alignmentMode="alignMargins"
android:useDefaultMargins="true">
<!-- 9 buttons (8 tiles + 1 empty space) -->
<Button
android:id="@+id/btn0"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
5
<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
6
android:id="@+id/btn4"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="@+id/btn5"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="@+id/btn6"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="@+id/btn7"
7
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
<Button
android:id="@+id/btn8"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_rowWeight="1" />
</GridLayout>
8
MainActivity.java
package com.example.puzzlegame;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
private Button[] buttons = new Button[9];
private int emptyIndex = 8;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
9
setContentView(R.layout.activity_main);
GridLayout gridLayout = findViewById(R.id.gridLayout);
for (int i = 0; i < 9; i++) {
String btnID = "btn" + i;
int resID = getResources().getIdentifier(btnID, "id",
getPackageName());
buttons[i] = findViewById(resID);
int finalI = i;
buttons[i].setOnClickListener(v -> onTileClick(finalI));
}
shuffle();
}
private void shuffle() {
ArrayList<String> numbers = new ArrayList<>();
for (int i = 1; i <= 8; i++) numbers.add(String.valueOf(i));
numbers.add(""); // empty tile
Collections.shuffle(numbers);
10
for (int i = 0; i < 9; i++) {
buttons[i].setText(numbers.get(i));
if (numbers.get(i).equals("")) emptyIndex = i;
}
}
private void onTileClick(int index) {
if (isAdjacent(index, emptyIndex)) {
buttons[emptyIndex].setText(buttons[index].getText());
buttons[index].setText("");
emptyIndex = index;
if (checkWin()) {
Toast.makeText(this, "You Win!",
Toast.LENGTH_LONG).show();
}
}
}
private boolean isAdjacent(int i1, int i2) {
int row1 = i1 / 3, col1 = i1 % 3;
int row2 = i2 / 3, col2 = i2 % 3;
11
return (Math.abs(row1 - row2) + Math.abs(col1 - col2))
== 1;
}
private boolean checkWin() {
for (int i = 0; i < 8; i++) {
if
(!buttons[i].getText().toString().equals(String.valueOf(i + 1))) {
return false;
}
}
return buttons[8].getText().toString().equals("");
}
}
12
8. Output
13
9. Conclusion
The project successfully demonstrates the creation of a basic
sliding puzzle game in Android Studio. It showcases the use of
UI layouts, event-driven programming, and core game logic
implementation. The game is functional, engaging, and a great
foundation for further development into a full-featured puzzle
application.
14