Skip to content

Commit 27f24a8

Browse files
Merge pull request #2667 from Shikhar9425/master-7
Game_of_Fifteen.py
2 parents 4242935 + a4e5016 commit 27f24a8

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

Game of Fifteen/Game_of_Fifteen.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import random
2+
3+
def create_board():
4+
numbers = list(range(1, 16))
5+
random.shuffle(numbers)
6+
numbers.append(None)
7+
8+
board = [numbers[i:i+4] for i in range(0, 16, 4)]
9+
return board
10+
11+
def display_board(board):
12+
for row in board:
13+
print(' | '.join(str(num).rjust(2) if num is not None else ' ' for num in row))
14+
print('-' * 23)
15+
16+
def get_empty_position(board):
17+
for i in range(4):
18+
for j in range(4):
19+
if board[i][j] is None:
20+
return i, j
21+
22+
def is_valid_move(move, empty_row, empty_col):
23+
row, col = move
24+
return (0 <= row < 4 and 0 <= col < 4 and
25+
(row == empty_row and abs(col - empty_col) == 1 or
26+
col == empty_col and abs(row - empty_row) == 1))
27+
28+
def make_move(board, move):
29+
empty_row, empty_col = get_empty_position(board)
30+
row, col = move
31+
board[empty_row][empty_col], board[row][col] = board[row][col], board[empty_row][empty_col]
32+
33+
def check_win(board):
34+
return all(board[i][j] == i*4 + j + 1 for i in range(4) for j in range(4)) and board[3][3] is None
35+
36+
def game_of_fifteen():
37+
board = create_board()
38+
39+
print("Welcome to the Game of Fifteen!")
40+
print("Arrange the numbers in numerical order by sliding the tiles.")
41+
print("Enter 'Q' to quit.")
42+
43+
while not check_win(board):
44+
display_board(board)
45+
move = input("Enter the number you want to move (1-15) or 'Q' to quit: ")
46+
47+
if move.lower() == 'q':
48+
print("Quitting the game...")
49+
break
50+
51+
if not move.isdigit() or int(move) not in range(1, 16):
52+
print("Invalid input. Please enter a number from 1 to 15.")
53+
continue
54+
55+
move = int(move)
56+
empty_row, empty_col = get_empty_position(board)
57+
58+
moves = [(empty_row - 1, empty_col), (empty_row + 1, empty_col),
59+
(empty_row, empty_col - 1), (empty_row, empty_col + 1)]
60+
61+
if any(is_valid_move(move, row, col) for row, col in moves):
62+
make_move(board, (empty_row, empty_col))
63+
else:
64+
print("Invalid move. Try again.")
65+
66+
if check_win(board):
67+
print("Congratulations! You solved the puzzle.")
68+
69+
if __name__ == "__main__":
70+
game_of_fifteen()

Game of Fifteen/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
Package/Script Name: Game of Fifteen (15-puzzle)
2+
3+
Short Description: This is a simple Python script for playing the classic Game of Fifteen, also known as the 15-puzzle. The script generates a 4x4 grid with numbers from 1 to 15 randomly shuffled, leaving one empty space. The player's objective is to rearrange the numbers in numerical order by sliding the tiles into the empty space.
4+
5+
Functionalities:
6+
7+
Create and shuffle the initial 4x4 board with numbers from 1 to 15 and an empty space.
8+
Display the current state of the board in the terminal.
9+
Allow the player to make valid moves by sliding tiles to the empty space.
10+
Check for a win condition when the numbers are arranged in numerical order, and the empty space is in the bottom-right corner.
11+
Allow the player to quit the game at any time.
12+
Setup Instructions:
13+
14+
Make sure you have Python installed on your system. If you don't have it, you can download it from the official website: https://www.python.org/downloads/
15+
Copy and paste the provided script into a new file named game_of_fifteen.py or any desired name.
16+
Save the file in the desired directory.
17+
How to Run the Script:
18+
19+
Open a terminal or command prompt.
20+
Navigate to the directory where you saved the game_of_fifteen.py file using the cd command.
21+
Run the script by entering the following command:
22+
Copy code
23+
python game_of_fifteen.py
24+
Detailed Explanation:
25+
The script begins by defining functions for creating the initial board, displaying the board, checking valid moves, and checking for a win condition. The create_board function generates a shuffled 4x4 board, and the display_board function prints the current state of the board in the terminal.
26+
27+
The main game_of_fifteen function runs the game loop until the player wins or quits. It prompts the player to enter a number to move, validates the input, and slides the selected tile to the empty space if the move is valid. The game continues until the player solves the puzzle or quits.
28+
29+
Output:
30+
When running the script, the output will be text-based, displaying the current state of the Game of Fifteen board and messages for the player's moves and the game's progress. The script will print the board after each player's move.
31+
32+
As this is a text-based game, there are no images, gifs, or videos to display for the output.
33+
34+
Author:
35+
Shikhar9425

0 commit comments

Comments
 (0)