Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
67 changes: 67 additions & 0 deletions Codes/Python/Number-Puzzle-Game/Puzzle_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import random

# Function to display the board
def display(board):
for row in board:
print(" ".join(str(x) if x != 0 else " " for x in row))
print()

# Function to find the position of 0 (empty space)
def find_empty(board):
for i in range(3):
for j in range(3):
if board[i][j] == 0:
return i, j

# Function to check if the puzzle is solved
def is_solved(board):
target = [[1, 2, 3],
[4, 5, 6],
[7, 8, 0]]
return board == target

# Function to make a move
def move(board, direction):
i, j = find_empty(board)
if direction == 'up' and i < 2:
board[i][j], board[i+1][j] = board[i+1][j], board[i][j]
elif direction == 'down' and i > 0:
board[i][j], board[i-1][j] = board[i-1][j], board[i][j]
elif direction == 'left' and j < 2:
board[i][j], board[i][j+1] = board[i][j+1], board[i][j]
elif direction == 'right' and j > 0:
board[i][j], board[i][j-1] = board[i][j-1], board[i][j]
else:
print("❌ Invalid move!")

# Function to shuffle the board
def shuffle_board(board):
moves = ['up', 'down', 'left', 'right']
for _ in range(50):
move(board, random.choice(moves))

# Main Game
def puzzle_game():
board = [[1, 2, 3],
[4, 5, 6],
[7, 8, 0]]

shuffle_board(board)
print("🧩 Welcome to the Number Puzzle Game!")
print("Arrange the numbers from 1 to 8. Use commands: up, down, left, right\n")

while True:
display(board)
if is_solved(board):
print("🎉 Congratulations! You solved the puzzle!")
break

move_input = input("Move (up/down/left/right or 'quit'): ").lower()
if move_input == "quit":
print("👋 Thanks for playing!")
break
move(board, move_input)

# Run the game
if __name__ == "__main__":
puzzle_game()
121 changes: 121 additions & 0 deletions Codes/Python/Number-Puzzle-Game/Readme
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# 🧩 Number Puzzle Game

A lightweight, console-based **Number Puzzle Game** implemented in Python.
The objective is to arrange the numbers **1–8** in ascending order by sliding tiles into the empty space.

---

## 📖 Overview

This project is a simple implementation of the classic 8-puzzle (3×3 sliding puzzle).
It demonstrates basic Python concepts such as loops, lists, conditionals, and user interaction.

The puzzle starts in a randomized configuration, and the player can move the empty space using simple commands until all tiles are correctly arranged.

---

## 🎯 Objective

Arrange the numbers to achieve the following order:

1 2 3
4 5 6
7 8

yaml
Copy code

---

## ⚙️ Requirements

- Python **3.8** or higher
- Works on **Windows**, **macOS**, and **Linux**

---

## 🚀 Getting Started

### 1. Clone the Repository
```bash
git clone https://github.com/<your-username>/Number-Puzzle-Game.git
cd Number-Puzzle-Game
2. Run the Game
bash
Copy code
python Puzzle_game.py
3. Play
Use the following commands to move the blank space:

css
Copy code
up
down
left
right
Type quit to exit the game.

🧠 Game Logic
The game board is represented as a 3×3 list of lists.

The blank space is represented by 0.

A random shuffle generates the initial layout.

The player moves the blank space using direction commands.

The game checks if the current state matches the solved configuration.

🧩 Example Gameplay
sql
Copy code
🧩 Arrange 1–8 in order using up/down/left/right.

1 3
4 2 5
7 8 6

Move (up/down/left/right or 'quit'): up
📁 Project Structure
bash
Copy code
Number-Puzzle-Game/
├── Puzzle_game.py # Main game script
└── README.md # Project documentation
✨ Features
✅ Interactive command-line gameplay

✅ Randomized puzzle generation

✅ Win condition detection

✅ Lightweight and easy to understand

🔮 Future Enhancements
Support for arrow key controls

Add a Tkinter GUI version

Implement an AI solver for demonstration

Option to select puzzle size (3×3, 4×4, etc.)

🤝 Contributing
Contributions are welcome!
To contribute:

Fork this repository

Create a new branch (feature/new-feature)

Commit your changes

Submit a pull request

🧾 License
This project is licensed under the MIT License.

👩‍💻 Author
Srishti Kumari
B.Tech CSE (DATA SCIENCE), Haldia Institute of Technology
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.