-
-
Save totoro2003/1190df754f83a97e6f04e0bdfc1cce59 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
import random | |
import sys | |
def print_board(board): | |
'''prints a board in | |
an elegant format thats | |
easy to understand''' | |
for row in board: | |
print(' '.join(row)) | |
def win_check(board): | |
'''Checks whether the board is in | |
a sorted state, and returns true | |
if so.''' | |
if board == [["1", "2", "3"], ["4", "5", "6"], ["7", "8", "_"]]: | |
print("Good Job bro!!!!") | |
return True | |
def shuffle(board, times): | |
'''Shuffles the board and returns | |
a new board ready for use. The times | |
parameter should be an even number, | |
but it isn't mandatory''' | |
iter_num = 0 | |
#This number determines how i will switch a certain value | |
for number in range(times): | |
first_row, first_col = random.randint(0, 2), random.randint(0, 2) | |
if iter_num % 2: | |
second_row = first_row | |
second_col = first_col + 1 if first_col < 2 else -1 | |
#The second collumn is left or right of the first collumn. Same | |
#for the rows, and which one depends on the iter_num value | |
else: | |
second_col = first_col | |
second_row = first_row + 1 if first_row < 2 else -1 | |
board[first_row][first_col], board[second_row][second_col] = board[second_row][second_col], board[first_row][first_col] | |
#Switches the values of the first and second coordinates, so that shuffling is a bit closer to real life | |
return board | |
def get_placement(board): | |
'''Searches a board to look for the empty tile. | |
instead of looking for a specific value, the | |
function look for the value that isn't a number. | |
this way, you can easily change the value without | |
modifying the function.''' | |
for row in range(len(board)): | |
for col in range(len(board[row])): | |
if not board[row][col].isdigit(): | |
#the odd one out is the only one that is not an integer (or digit) | |
return row, col | |
def change_spot(board, directions, placement_to_change, chosen_direction = "up"): | |
'''changes the spot when the player chooses a direction to change | |
it with''' | |
switch_row, switch_col = placement_to_change[0], placement_to_change[1] | |
#increases the multidemensional array x/y by 1/-1 depending on the direction | |
if chosen_direction in ["up", "down"]: | |
placement_to_change[0] = switch_row + directions.get(chosen_direction) | |
elif chosen_direction in ["left", "right"]: | |
placement_to_change[1] = switch_col + directions.get(chosen_direction) | |
return placement_to_change[0], placement_to_change[1] | |
def main(): | |
#this line makes the code backwards compatible with python 2 | |
user_input = input if sys.version[0] == '3' else raw_input | |
board = [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '_']] | |
print_board(board) | |
shuffle_times = user_input("How many times would you like to shuffle (whole number): ") | |
#If the input value is not a number, it will become 4 by defualt | |
shuffle_times = 4 if not shuffle_times.isdigit() else shuffle_times | |
shuffle(board, int(shuffle_times)) | |
print_board(board) | |
directions = { | |
"up" : -1, | |
"down" : 1, | |
"left" : -1, | |
"right" : 1 | |
} | |
while True: | |
row, col = get_placement(board) | |
chosen_direction = user_input("up, down, left, or right. choose one: ").lower() | |
#make it lower so that if the input begins with an uppercase letter its goooooood >:) | |
if chosen_direction == "quit": | |
quit() | |
#quits the program if the player chooses so | |
new_row, new_col = change_spot(board, directions, [row, col], chosen_direction) | |
try: | |
#If the index is negative, it raises an error instead of starting from the last value of the array | |
if new_row < 0 or new_col < 0: | |
raise IndexError | |
board[row][col], board[new_row][new_col] = board[new_row][new_col], board[row][col] | |
#switches the two values if the player actually knows left from right and up from down. otherwise it throws | |
#an error and continues on to the next cycle....sad, isnt it? | |
except IndexError: | |
continue | |
#if the player tries to go too far to the point that they index is too much, it continues to the next cycle | |
print_board(board) | |
if (win_check(board)): | |
break; | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment