samedi 24 février 2018

Nested if statements not working in Python

I have been trying to implement a Breadth first search algorithm in Python. I have a problem in running the nested if statements in #Movetop, #Movebottom, #Moveleft, #Moveright sections. Since, the nested if statements do not run, the counter in while loop never increases and all the associated operations defined in the nested if statements do not function. Hence, I am unable to accomplish the purpose of my code (which is to get output from the nested if statements). I tried researching about nested if in Python and to my knowledge it all looks syntactically correct. But can't understand why it still doesn't work !?

Any assistance will be well appreciated !

       # -*- coding: utf-8 -*-
"""
Spyder Editor

This is a temporary script file.
"""
import random
import os

gameboard = [1,2,3,4,5,6,7,8,0]

game_state_visited = []

Nodes_dict = {}
#alpha = 0
#beta = 0
#dummy = (21,21,21,21,21,21,21,21,21)


# Stores all the running states in a list
# =======================================

# Daughter list
# =============

# This function only prints the gameboard when called
# ===================================================

def displayboard(gameboard):
    print"-------------------------"
    print"|       |       |       |"
    print "|  ",(gameboard[0]),"  |  ",(gameboard[1]),"  |  ",    (gameboard[2]),"  |"
    print"-------------------------"
    print"|       |       |       |"
    print "|  ",(gameboard[3]),"  |  ",(gameboard[4]),"  |  ",(gameboard[5]),"  |"
    print"-------------------------"
    print"|       |       |       |"
    print "|  ",(gameboard[6]),"  |  ",(gameboard[7]),"  |  ",(gameboard[8]),"  |"
    print"-------------------------"

# This function only checks the gamestatus when called
# ====================================================   

def winstatus(gameboard):
        if gameboard[0]==1 and \
        gameboard[1]==2 and \
        gameboard[2]==3 and \
        gameboard[3]==4 and \
        gameboard[4]==5 and \
        gameboard[5]==6 and \
        gameboard[6]==7 and \
        gameboard[7]==8 and \
        gameboard[8]==0:
            return True
        else:
            return False

# This function re-shuffles the gameboard for avoiding winning status      when called
#     ===========================================================================    ====

def avoidwin(gameboard):
    if winstatus(gameboard):
        scramble(gameboard)
    else:
        if gameboard in game_state_visited:
            scramble(gameboard)
        else:
            game_state_visited.append(gameboard)
            return gameboard
            displayboard(gameboard)

# This function makes a left move based on available opportunity when     called. If not it passes control out of the function
#     ===========================================================================    =============================================

def moveleft(output):
    for i in output:
        if output[i]==0:
            alpha = 0
            beta = 0
            beta=output[i+1]
            output[i]=beta
            output[i+1]=alpha
            return output
            break
        else:
            continue


# This function makes a right move based on available opportunity when     called. If not it passes control out of the function
#     ===========================================================================    =============================================

def moveright(output):
        for i in output:
        if output[i]==0:
            alpha = 0
            beta = 0
            beta=output[i-1]
            output[i]=beta
            output[i-1]=alpha
            return output
            break
        else:
            continue


# This function makes a top move based on available opportunity when     called. If not it passes control out of the function
#     ===========================================================================    =============================================

def movetop(output):
    for i in output:
        if output[i]==0:
            alpha = 0
            beta = 0
            beta=output[i+3]
            output[i]=beta
            output[i+3]=alpha
            return output
            break
        else:
            continue



# This function makes a bottom move based on available opportunity when     called. If not it passes control out of the function
#     ===========================================================================    =============================================

def movebottom(output):
    for i in output:
        if output[i]==0:
            alpha = 0
            beta = 0
            beta=output[i-3]
            output[i]=beta
            output[i-3]=alpha
            return output
            break
        else:
            continue

# This is our game board reset
# ============================

def scramble(gameboard):
    random.shuffle(gameboard)
    avoidwin(gameboard)
    print gameboard
    return gameboard

def gameplay(gameboard):

    running_list =[]
    daughter_list = []
Node_no = 1
#Ticker = 1

output = scramble(gameboard)

#print "The original output from scramble is :"
displayboard(output)

running_list.append(output)

#print "The running list during initiation is :"
#print type(running_list)
#print running_list

first_state = tuple (output)
Nodes_dict.update({first_state:[1,0,0]})

while (Node_no < 1000):
    print "The current node number is"
    print Node_no
    Counter = 1
    #Ticker +=1
    running_list = daughter_list
    daughter_list = []

    for dummy in running_list:
        Counter+=1
        # Movetop:
        if ((dummy[0]==0 or dummy[1]==0 or dummy[2]==0 or dummy[3]==0 or dummy[4]==0 or dummy[5]==0) and \
            (dummy[6]!=0 and dummy[7]!=0 and dummy[8]!=0)):
            output = dummy
            movetop(output)
            #return(dummy)
            #print "Output of Movetop is :"
            checker = tuple(output)
            if checker not in Nodes_dict:
                displayboard(output)
                daughter_list.append(output)
                Node_no+=1
                Value = [Node_no,Counter,0]
                print Value
                Nodes_dict.update({checker:Value})
            else:
                pass 

        # Movebottom:
        if ((dummy[3]==0 or dummy[4]==0 or dummy[5]==0 or dummy[6]==0 or dummy[7]==0 or dummy[8]==0) and \
            (dummy[0]!=0 and dummy[1]!=0 and dummy[2]!=0)):
            output = dummy
            movebottom(output)
            #return(dummy)
            #print "Output of Movebottom is :"
            checker = tuple(output)
            if checker not in Nodes_dict:
                daughter_list.append(output)
                displayboard(output)
                Node_no+=1
                Value = [Node_no,Counter,0]
                print Value
                Nodes_dict.update({checker:Value})
            else:
                pass


        # Moveright:
        if ((dummy[1]==0 or dummy[2]==0 or dummy[4]==0 or dummy[5]==0 or dummy[7]==0 or dummy[8]==0) and \
            (dummy[0]!=0 and dummy[3]!=0 and dummy[6]!=0)):
            output = dummy
            moveright(output)
            #return(dummy)
            #print "Output of Moveright is :"
            checker = tuple(output)
            if checker not in Nodes_dict:
                displayboard(output)
                daughter_list.append(output)
                Node_no+=1
                Value = [Node_no,Counter,0]
                print Value
                Nodes_dict.update({checker:Value})
            else:
                pass


        # Moveleft:
        if ((dummy[0]==0 or dummy[1]==0 or dummy[3]==0 or dummy[4]==0 or dummy[7]==0 or dummy[6]==0) and \
            (dummy[2]!=0 and dummy[5]!=0 and dummy[8]!=0)):
            output = dummy
            moveleft(output)
            #return(dummy)
            #print "Output of Moveleft is :"
            checker = tuple(output)
            if checker not in Nodes_dict:
                displayboard(output)
                daughter_list.append(output)
                Node_no+=1
                Value = [Node_no,Counter,0]
                print Value
                Nodes_dict.update({checker:Value})
            else:
                pass


print "While loop terminated"
print Nodes_dict
document = open("output.txt","w")
document.write(Nodes_dict)

gameplay (gameboard)

Aucun commentaire:

Enregistrer un commentaire