I have a function which should work, and it actually does... just not all the time. Sometimes it just freezes. Can anyone identify a problem in the ai_northAmericaCheck(): function?

Here is the code:

import random

ai_countries = []
firstTurnsCountries = []

northAmericaList = [0,1,2,3,4,5,6,7,8,9]
availableCountries = range(20)

country0value = 1  # <--- Variables used to store army counts for countries
country1value = 1
country2value = 1
country3value = 1      
country4value = 1      
country5value = 1      
country6value = 1      
country7value = 1      
country8value = 1      
country9value = 1      
country10value = 1     
country11value = 1
country12value = 1
country13value = 1
country14value = 1
country15value = 1
country16value = 1
country17value = 1
country18value = 1
country19value = 1

# Below: List that provides easy access to above variables
countryVariableList = [country0value, country1value, country2value,
                       country3value, country4value, country5value,
                       country6value, country7value, country8value,
                       country9value, country10value, country11value,
                       country12value, country13value, country14value,
                       country15value, country16value, country17value,
                       country18value, country19value]

attachedCountry0List = [1, 3]       # Variables limit attacking and                 
attachedCountry1List = [0, 2, 3, 4] # fortifying to attached countries only        
attachedCountry2List = [1, 4, 5, 13]         
attachedCountry3List = [0, 1, 4, 6]          
attachedCountry4List = [1, 2, 3, 5, 6, 7]    
attachedCountry5List = [2, 4, 7]             
attachedCountry6List = [3, 4, 7, 8]          
attachedCountry7List = [4, 5, 6, 8]
attachedCountry8List = [6, 7, 9]
attachedCountry9List = [8, 10, 11]
attachedCountry10List = [9, 11, 12]
attachedCountry11List = [9, 10, 12]
attachedCountry12List = [10, 11]
attachedCountry13List = [2, 14, 15]
attachedCountry14List = [13, 15, 16, 19]
attachedCountry15List = [13, 14, 16, 17]
attachedCountry16List = [14, 15, 17, 18, 19]
attachedCountry17List = [15, 16, 18]
attachedCountry18List = [16, 17, 19]
attachedCountry19List = [14, 16, 18]

# Below: List that provides easy access to above lists
attachedCountryTotalList = [attachedCountry0List, attachedCountry1List,
                            attachedCountry2List, attachedCountry3List,
                            attachedCountry4List, attachedCountry5List,
                            attachedCountry6List, attachedCountry7List,
                            attachedCountry8List, attachedCountry9List,
                            attachedCountry10List, attachedCountry11List,
                            attachedCountry12List, attachedCountry13List,
                            attachedCountry14List, attachedCountry15List,
                            attachedCountry16List, attachedCountry17List,
                            attachedCountry18List, attachedCountry19List]

def countryDivider(countryNum):
    while True:
        if not availableCountries: # check if list is spent/empty
            break
        q = random.choice(availableCountries) # pick one available country
        firstTurnsCountries.append(q) # add it to first list
        availableCountries.remove(q) # remove it from the available list
        q = random.choice(availableCountries) # repeat for second list
        ai_countries.append(q)
        availableCountries.remove(q)
    firstTurnsCountries.sort()
    ai_countries.sort()

def ai_northAmericaCheck():
    counter = 0
    global ai_countryToAddMen
    northAmericaCheckDone = 2
    while True:
        if counter > len(northAmericaList):
            ai_countryToAddMen = "none"
            return ai_countryToAddMen
            break
        if northAmericaList[counter] in ai_countries:
            counter2 = 0
            while True:
                if counter2 >= len(attachedCountryTotalList[counter]):
                    northAmericaCheckDone = 2
                    break
                if attachedCountryTotalList[counter][counter2] in firstTurnsCountries:
                    ai_countryToAddMen = northAmericaList[counter]
                    northAmericaCheckDone = 1
                    break
                if attachedCountryTotalList[counter][counter2] in ai_countries:
                    counter2 = counter2 + 1
        if northAmericaCheckDone == 1:
            return ai_countryToAddMen
            break
        if northAmericaList[counter] in firstTurnsCountries:
            counter = counter + 1

countryDivider(9)
ai_northAmericaCheck()
print ai_countryToAddMen

reinforcements = 3
countryVariableList[ai_countryToAddMen] = countryVariableList[ai_countryToAddMen] + reinforcements
print countryVariableList[ai_countryToAddMen]

The game I'm working on is quite lengthy so I included only the parts that the function needs in order to run. Another problem though, is that instead of freezing only some of the time, like it does in this code example, it freezes 95% of the time in my game. So I figured I'd start with the function itself.

Thanks for everybody's time and help!

Recommended Answers

All 4 Replies

I altered the function and have yet for it to freeze, now to incorporate it into my actuall game and see if it works...

But can anyone look it over and see if they notice any flaws?

Thanks

def ai_northAmericaCheck():
    global ai_countryToAddMen
    northAmericaCheckDone = 2
    for i in range(len(northAmericaList)):
        if northAmericaList[i] in ai_countries:
            for i2 in range(len(attachedCountryTotalList[i])):
                if attachedCountryTotalList[i][i2] in firstTurnsCountries:
                    ai_countryToAddMen = northAmericaList[i]
                    northAmericaCheckDone = 1
                    break
        if northAmericaCheckDone == 1:
            return ai_countryToAddMen
            break
....    
        if northAmericaCheckDone == 1:
            return ai_countryToAddMen
            break

you have a return and a break after that. the "break" can be omitted.

I have a function which should work, and it actually does... just not all the time. Sometimes it just freezes. Can anyone identify a problem in the ai_northAmericaCheck(): function?

Here is the code:

...
            while True:
                if counter2 >= len(attachedCountryTotalList[counter]):
                    northAmericaCheckDone = 2
                    break
                if attachedCountryTotalList[counter][counter2] in firstTurnsCountries:
                    ai_countryToAddMen = northAmericaList[counter]
                    northAmericaCheckDone = 1
                    break
                if attachedCountryTotalList[counter][counter2] in ai_countries:
                    counter2 = counter2 + 1

There are two ways to get out of this loop, abbreviating the variable names for convenience:

(1) counter2 >= len(aCTL[counter])
(2) aCTL[counter][counter2] in fTC

It might be the case that if, for example, ai has the first turn, then ai_countries == firstTurnsCountries, in which case the first time you hit a country not in ai_countries, then counter2 would never be incremented (so (1) never happens), but (2) would never happen either. Result: endless loop.

If that's not the case, then something like it probably is ... one of the while loops has an eternally unfulfilled condition.

Jeff

... one of the while loops has an eternally unfulfilled condition.

This is the only thing that could cause this problem (so of course it's something else). Add a separate counter to each while loop and exit after max cycles.

ctr_1 = 0
while True :
   ctr_1 += 1
   if ctr_1 > max_loops :
      print "ctr_1 exceeded max", ctr_1, max_loops
      break

This will at least tell you which loop has the problem. Also, this is something you may want to do when testing any while() loop.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.