Function will freeze program (on occasion)

Reply

Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Function will freeze program (on occasion)

 
0
  #1
Jan 12th, 2007
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:
[php]
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]
[/php]
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!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: Function will freeze program (on occasion)

 
0
  #2
Jan 13th, 2007
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

[php]
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
[/php]
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 156
Reputation: ghostdog74 is on a distinguished road 
Solved Threads: 44
ghostdog74 ghostdog74 is offline Offline
Junior Poster

Re: Function will freeze program (on occasion)

 
0
  #3
Jan 13th, 2007
Originally Posted by Matt Tacular View Post
  1. ....
  2. if northAmericaCheckDone == 1:
  3. return ai_countryToAddMen
  4. break
you have a return and a break after that. the "break" can be omitted.
Last edited by ghostdog74; Jan 13th, 2007 at 4:15 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 152
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: Function will freeze program (on occasion)

 
0
  #4
Jan 13th, 2007
Originally Posted by Matt Tacular View Post
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:
  1. ...
  2. while True:
  3. if counter2 >= len(attachedCountryTotalList[counter]):
  4. northAmericaCheckDone = 2
  5. break
  6. if attachedCountryTotalList[counter][counter2] in firstTurnsCountries:
  7. ai_countryToAddMen = northAmericaList[counter]
  8. northAmericaCheckDone = 1
  9. break
  10. if attachedCountryTotalList[counter][counter2] in ai_countries:
  11. 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
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,190
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 341
woooee woooee is offline Offline
Veteran Poster

Re: Function will freeze program (on occasion)

 
0
  #5
Jan 14th, 2007
... 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.
  1. ctr_1 = 0
  2. while True :
  3. ctr_1 += 1
  4. if ctr_1 > max_loops :
  5. print "ctr_1 exceeded max", ctr_1, max_loops
  6. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 2018 | Replies: 4
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC