Python Forum
Simple Eight-Puzzle not working!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Simple Eight-Puzzle not working!
#1
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:

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!
Reply
#2
Create a new board, instead of changing the existing one. Only update the new board if the square is not occupied. You can then go through the existing board in order and add the number to some other square on the new board. It should not be necessary to keep track of numbers already used since you are taking the existing board in order, but doing so will tell you when you are using a duplicate number. Also, learn to pass args to and get them from a function, instead of globals http://www.tutorialspoint.com/python/pyt...ctions.htm
One problem is here
                board_bot[0] = board_mid[1]
                board_bot[1] = " "  ## should be mid[1]
##
## You can call a function instead of repeating everything
def swap_places(board_top, board_mid, board_bot):
    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")

    return board_top, board_mid, board_bot 
Reply
#3
Thanks! I shall try to incorporate the swap function.

(May-03-2018, 09:15 PM)woooee Wrote: Create a new board, instead of changing the existing one. Only update the new board if the square is not occupied. You can then go through the existing board in order and add the number to some other square on the new board.

I'm sorry but I don't understand what you mean. How would I do this?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  8 puzzle game aliyark145 4 10,720 May-30-2020, 05:54 PM
Last Post: Alkis
  A sign-Reversal Puzzle HY2000 2 2,480 Dec-05-2019, 11:55 AM
Last Post: HY2000
  Cross word puzzle solve using python constraint library aliyark145 1 3,294 Nov-29-2018, 10:53 AM
Last Post: Larz60+
  Simple IF statement not working as intended gortexxx 2 2,789 May-17-2018, 07:54 PM
Last Post: gortexxx
  Supposedly simple program not working! Please help! BenjaminDJ 5 4,058 Feb-20-2018, 08:43 PM
Last Post: BenjaminDJ
  Simple Python program not working AudioKev 3 3,218 Jan-23-2018, 09:57 PM
Last Post: Larz60+
  Magic Square Puzzle Harnick 1 4,886 Aug-09-2017, 04:51 PM
Last Post: nilamo
  Image Puzzle Digitalchemist 6 7,325 Jun-05-2017, 07:56 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020