def movePiece(g_state, piece, start, dest, zone):
    #print "----"
    #print "locations before move", [C.indexToChessLocation(g_state.getSTATE()["ON_p_" + str(x)]) for x in range(1,7)]

    STATE = g_state.getSTATE()
    zoneList = []
    for z in g_state.getZONES():
        if z != zone: zoneList.append(z)
    pieceNumber = 0
    for x in range(1,7): 
        if STATE["p_" + str(x)] == piece: 
            pieceNumber = x

    # error check
    if pieceNumber == 0:
        print "Could not find pice in state"
        return g_state

    if STATE['ON_p_' + str(pieceNumber)] != C.locationToIndex(start):
        print "p_" + str(pieceNumber) + " is not on " + str(start)
        return g_state
    x,y = dest
    if not STATE['Rp_' + str(pieceNumber) + "_" + str(x) + str(y)]:
        print "p_" + str(pieceNumber) + " cannot reach " + C.locationToChessLocation(dest)
        print 'Rp_' + str(pieceNumber) + "_" + str(x) + str(y) + ' = ' + str(STATE['Rp_' + str(pieceNumber) + "_" + str(x) + str(y)]) 
        print "locations", ["p_" + str(x) + ":"+ C.indexToChessLocation(g_state.getSTATE()["ON_p_" + str(x)]) for x in range(1,7)]
        print zone
        raw_input("enter to contine")
        return g_state

    STATE['ON_p_' + str(pieceNumber)] = STATE["x_" + str(C.locationToIndex(dest))]  
    g_state.setSTATE(STATE)
    #print "----------SOURCE-DEST------------"
    #print C.locationToChessLocation(start), C.locationToChessLocation(dest)
    #print "----------ZONE------------"
    #print zone
    zone = T.transition(T.objectify(zone), piece, C.locationToChessLocation(start), C.locationToChessLocation(dest))
    zone = T.stringify(zone)
    #print zone
    zoneList.append(zone)
    #print "----------ZONE------------"

    setR(g_state)
    g_state.setZONES(zoneList)
    
    #print "locations after move", [C.indexToChessLocation(g_state.getSTATE()["ON_p_" + str(x)]) for x in range(1,7)]
    #print "----"
    return g_state
def takeATurn(g_state, currentMover, nextMover, movesSoFar):
    global maxDepth
    global FinalResults
    maxDepth = maxDepth - 1

    if maxDepth == 0:
        #print "reached maxDepth"
        maxDepth += 1
        return g_state, movesSoFar

    if CUT(g_state): 
        print "CUT State Reached"
        FinalResults.append(movesSoFar[2:])
        #raw_input("enter to contine")
        maxDepth += 1
        return g_state, movesSoFar

    #if   currentMover == "P1": print "Black Turn"
    #elif currentMover == "P2": print "White Turn"

    pMoves =  MV(g_state, currentMover, g_state.getZONES())
    #print "-- move options"
    #pprint(pMoves)

    isHappy = False
    if len(pMoves) == 0:
        print movesSoFar, "(No more moves along this path)"
        #raw_input("enter to continue")
        maxDepth += 1
        return g_state, "No more moves along this path"

    for move in pMoves:
        bckupl = backupLocations(g_state)
        bckupz = backupZones(g_state)

        #raw_input("Beginnig of loop")
        piece = move[0]
        zone = move[1]
        dest = C.chessLocationToLocation(move[2])
        pieceNumber = getPieceNumber(g_state, piece)
        source = C.indexToLocation(g_state.getSTATE()["ON_p_"+str(pieceNumber)])


        moveString = str(C.locationToChessLocation(source)) + '-' + str(C.locationToChessLocation((dest)))
        #print moveString
        #pprint(move)
        statshState = movePiece(g_state,piece,source,dest,zone)
        result = takeATurn(statshState, nextMover, currentMover, movesSoFar + ', ' + moveString)

        cutResult = CUT(statshState)
        isHappy = (currentMover == "P1" and not cutResult) or (currentMover == "P2" and cutResult)

        if isHappy: #no more moves are needed to be changed 
            #raw_input(currentMover+ " is happy. enter to continue")
            #restoreLocations(g_state, bckup)
            maxDepth += 1
            return statshState, str(C.locationToChessLocation(source)) + '-' + str(C.locationToChessLocation((dest))) + ", " + result[1]
        else:
            restoreLocations(g_state, bckupl)
            restoreZones(g_state, bckupz)
            setR(g_state)
            #raw_input(currentMover+ " is not happy. enter to continue")


    maxDepth += 1
    return g_state, " There was no sulution down this path "