Skip to content

format code with autopep8 #2818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 10, 2023
Merged
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
16 changes: 13 additions & 3 deletions Game of Fifteen/Game_of_Fifteen.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import random


def create_board():
numbers = list(range(1, 16))
random.shuffle(numbers)
Expand All @@ -8,31 +9,38 @@ def create_board():
board = [numbers[i:i+4] for i in range(0, 16, 4)]
return board


def display_board(board):
for row in board:
print(' | '.join(str(num).rjust(2) if num is not None else ' ' for num in row))
print(' | '.join(str(num).rjust(2)
if num is not None else ' ' for num in row))
print('-' * 23)


def get_empty_position(board):
for i in range(4):
for j in range(4):
if board[i][j] is None:
return i, j


def is_valid_move(move, empty_row, empty_col):
row, col = move
return (0 <= row < 4 and 0 <= col < 4 and
(row == empty_row and abs(col - empty_col) == 1 or
col == empty_col and abs(row - empty_row) == 1))
col == empty_col and abs(row - empty_row) == 1))


def make_move(board, move):
empty_row, empty_col = get_empty_position(board)
row, col = move
board[empty_row][empty_col], board[row][col] = board[row][col], board[empty_row][empty_col]


def check_win(board):
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


def game_of_fifteen():
board = create_board()

Expand All @@ -42,7 +50,8 @@ def game_of_fifteen():

while not check_win(board):
display_board(board)
move = input("Enter the number you want to move (1-15) or 'Q' to quit: ")
move = input(
"Enter the number you want to move (1-15) or 'Q' to quit: ")

if move.lower() == 'q':
print("Quitting the game...")
Expand All @@ -66,5 +75,6 @@ def game_of_fifteen():
if check_win(board):
print("Congratulations! You solved the puzzle.")


if __name__ == "__main__":
game_of_fifteen()