Created
September 6, 2020 17:18
-
-
Save madsunrise/85e7a29cf83f4bd4cf0924eceb6ac4a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# `random` module is used to shuffle field, see: | |
# https://docs.python.org/3/library/random.html#random.shuffle | |
import random | |
# Empty tile, there's only one empty cell on a field: | |
EMPTY_MARK = 'x' | |
# Dictionary of possible moves if a form of: | |
# key -> delta to move the empty tile on a field. | |
MOVES = { | |
'w': -4, | |
's': 4, | |
'a': -1, | |
'd': 1, | |
} | |
def shuffle_field(): | |
""" | |
This function is used to create a field at the very start of the game. | |
:return: list with 16 randomly shuffled tiles, | |
one of which is a empty space. | |
""" | |
field = (list(range(1, 16))) | |
random.shuffle(field) | |
return field | |
def solvability_field(field): | |
""" | |
Эта функция проверяет решаемость раскладки, используя параметр беспорядка | |
""" | |
chaos = 0 | |
for i in range(15): | |
for j in range(i, 15): | |
if field[i] > field[j]: | |
chaos += 1 | |
return chaos % 2 == 0 | |
def print_field(field): | |
""" | |
This function prints field to user. | |
:param field: current field state to be printed. | |
:return: None | |
""" | |
print("╔══╤══╤══╤══╗") | |
print("║{:2}|{:2}|{:2}|{:2}║".format(field[0], field[1], field[2], field[3])) | |
print("║{:2}|{:2}|{:2}|{:2}║".format(field[4], field[5], field[6], field[7])) | |
print("║{:2}|{:2}|{:2}|{:2}║".format(field[8], field[9], field[10], field[11])) | |
print("║{:2}|{:2}|{:2}|{:2}║".format(field[12], field[13], field[14], field[15])) | |
print("╚══╧══╧══╧══╝") | |
def is_game_finished(field): | |
""" | |
This function checks if the game is finished. | |
:param field: current field state. | |
:return: True if the game is finished, False otherwise. | |
""" | |
completed_field = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, "x"] | |
return field == completed_field # change 1 | |
# def perform_move(field, key): | |
# """ | |
# Moves empty-tile inside the field. | |
# | |
# :param field: current field state. | |
# :param key: move direction. | |
# :return: new field state (after the move). | |
# :raises: IndexError if the move can't me done. | |
# """ | |
# field[field.index("x")], field[field.index("x") + key] = field[field.index("x") + key], field[field("x")] | |
# return field | |
def perform_move(field, key): # change 3 | |
indexX = field.index(EMPTY_MARK) | |
field[indexX] = field[indexX + key] | |
field[indexX + key] = EMPTY_MARK | |
def handle_user_input(): | |
""" | |
Handles user input. | |
List of accepted moves: | |
'w' - up, | |
's' - down, | |
'a' - left, | |
'd' - right | |
:return: <str> current move. | |
""" | |
input_key = input() | |
key = None | |
if input_key == "w": | |
key = -4 | |
elif input_key == "s": | |
key = 4 | |
elif input_key == "a": | |
key = -1 | |
elif input_key == "d": | |
key = 1 | |
return key | |
def main(): | |
""" | |
The main function. It starts when the program is called. | |
It also calls other functions. | |
:return: None | |
""" | |
#shuffle_field() # change 4 | |
field = shuffle_field() | |
#solvability_field(field) # change 5 | |
while not solvability_field(field): | |
print('нерешаемый расклад. еще раз') | |
#shuffle_field() # change 6 | |
field = shuffle_field() | |
#solvability_field(field) # change 7 | |
field.append(EMPTY_MARK) | |
print_field(field) | |
while not is_game_finished(field): | |
#handle_user_input() # change 2 | |
key = handle_user_input() | |
#perform_move(field, key) # change 8 | |
perform_move(field, key) # change 9 | |
print_field(field) | |
if __name__ == '__main__': | |
# See what this means: | |
# http://stackoverflow.com/questions/419163/what-does-if-name-main-do | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment