May-03-2018, 07:15 PM
Hi there!
I've been told to make a program that randomly shuffles a solved Eight-Puzzle as to create a puzzle that is definitely solvable (ie not random, because it might not have a solution).
This here is my code:
Does anyone know why this is happening? And if so, please could you enlighten me?
Thanks!
I've been told to make a program that randomly shuffles a solved Eight-Puzzle as to create a puzzle that is definitely solvable (ie not random, because it might not have a solution).
This here is my code:
import random solved_top = [" ", "1", "2"] solved_mid = ["3", "4", "5"] solved_bot = ["6", "7", "8"] board_top = [] board_mid = [] board_bot = [] def shuffle_board(): global board_top, board_mid, board_bot, solved_top, solved_mid, solved_bot board_top = solved_top board_mid = solved_mid board_bot = solved_bot for i in range(10): do_random_move() def do_random_move(): global board_top, board_mid, board_bot #Test for which line of the board has the blank space. if board_top.count(" ") == 1: #If board_top has the space. randomise_1 = random.random() position = board_top.index(" ") #Position = wherever the space is in this line. #Horizontal swap. if randomise_1 >= 0.5: if position == 0: board_top[0] = board_top[1] board_top[1] = " " elif position == 1: randomise_2 = random.random() if randomise_2 >= 0.5: board_top[1] = board_top[0] board_top[0] = " " else: board_top[1] = board_top[2] board_top[2] = " " elif position == 2: board_top[2] = board_top[1] board_top[1] = " " else: print("ERROR: do_random_move(); second IF statement; top horizontal variant") #Vertical swap. else: if position == 0: board_top[0] = board_mid[0] board_mid[0] = " " elif position == 1: board_top[1] = board_mid[1] board_mid[1] = " " elif position == 2: board_top[2] = board_mid[2] board_mid[2] = " " else: print("ERROR: do_random_move(); second IF statement; top vertical variant") elif board_mid.count(" ") == 1: #If space is in middle line. randomise_1 = random.random() position = board_mid.index(" ") #Horizontal if randomise_1 >= 0.5: if position == 0: board_mid[0] = board_mid[1] board_mid[1] = " " elif position == 1: randomise_2 = random.random() if randomise_2 >= 0.5: board_mid[1] = board_mid[0] board_mid[0] = " " else: board_mid[1] = board_mid[2] board_mid[2] = " " elif position == 2: board_mid[2] = board_mid[1] board_mid[1] = " " else: print("ERROR: do_random_move(); second IF statement; mid horizontal variant") #Vertical else: if position == 0: randomise_2 = random.random() if randomise_2 >= 0.5: board_mid[0] = board_top[0] board_top[0] = " " else: board_mid[0] = board_bot[0] board_bot[0] = " " elif position == 1: randomise_2 = random.random() if randomise_2 >= 0.5: board_mid[1] = board_top[1] board_top[1] = " " else: board_mid[1] = board_bot[1] board_bot[1] = " " elif position == 2: randomise_2 = random.random() if randomise_2 >= 0.5: board_mid[2] = board_top[2] board_top[2] = " " else: board_mid[2] = board_bot[2] board_bot[2] = " " else: print("ERROR: do_random_move(); second IF statement; mid vertical variant") elif board_bot.count(" ") == 1: #bottom line has the space randomise_1 = random.random() position = board_bot.index(" ") if randomise_1 >= 0.5: if position == 0: board_bot[0] = board_mid[1] board_bot[1] = " " elif position == 1: randomise_2 = random.random() if randomise_2 >= 0.5: board_bot[1] = board_bot[0] board_bot[0] = " " else: board_bot[1] = board_bot[2] board_bot[2] = " " elif position == 2: board_bot[2] = board_bot[1] board_bot[1] = " " else: print("ERROR: do_random_move(); second IF statement; bot horizontal variant") else: if position == 0: board_bot[0] = board_mid[0] board_mid[0] = " " elif position == 1: board_bot[1] = board_mid[1] board_mid[1] = " " elif position == 2: board_bot[2] = board_mid[2] board_mid[2] = " " else: print("ERROR: do_random_move(); second IF statement; bot vertical variant") else: print("ERROR: do_random_move(); first IF statement") shuffle_board() print(board_top) print(board_mid) print(board_bot)Occasionally, I will get repeated numbers when I print out my final shuffled Eight-Puzzle. Confused, I tried increasing the number of shuffles in line 15, and by doing so, even more numbers were being cloned!
Does anyone know why this is happening? And if so, please could you enlighten me?
Thanks!