Python Help: Picking a random Player

Kael

Deity
Joined
May 6, 2002
Messages
17,403
Location
Paris, France
Im banging my head into a wall in what I would think should be an easy function. I just need to select a random player. This is what I'm trying to do now:

iNumPlayers = gc.getGame().countCivPlayersAlive()
numPlayer2 = random.randint(1, iNumPlayers)
rndPlayer = gc.getPlayer(numPlayer2)

My function is dying on this, saying that Im trying to pass 1, 1, 0 to the random function. As far as I can tell the countCivPlayersAlive call isn't returning a simple int like I would expect, and the api reference would suggest.

Anyone have any ideas for a working way to get a random player?

Thanks,
Kael

ps Yes I know declaring 3 variables to step through picking a random player is messy, but I did it that way just to simplify it for troubleshooting.
 
Firstly - if you use the code tags for code it makes it alot easier on people trying to quote your code!

I'm not familier with the random.randint function - I use CyGame().getSorenRandNum(int, Str). Str can be anything as far as I am aware. I'm not sure this would fix your problem, but I'm pretty sure gc.getGame().countCivPlayersAlive() should be working fine.

So:
Code:
iNumPlayers = gc.getGame().countCivPlayersAlive()
numPlayer2 = CyGame().getSorenRandNum(iNumPlayers, "Bob")
rndPlayer = gc.getPlayer(numPlayer2)

I tend to use the random number option quite a bit, so I give it it's own function in most of my mods. One thing to bear in mind is that it returns a result between 0, and the max value (not including the max), so is perfect for Object indicies.

Hope this helps!
 
Er, that's not a good way to do it. gc.getGame().countCivPlayersAlive() will return the number of alive players, true, but it won't determine what their numbers are. For example, if the game starts with players 1, 2, 3 and 4, and player 2 dies, the function will return that there are 3 players. You would be unable to randomly pick player 4, and if it randomly picks player 2, it will point to a dead player.

A better way to do it (off the top of my head, I won't vouch for this working correctly):
Code:
listPlayers = []
for iPlayer in range(gc.getMAX_PLAYERS()):
 pPlayer = gc.getPlayer(iPlayer)
 if (pPlayer.isAlive()):
  listPlayers.append(iPlayer)
iRandNum = CyGame().getSorenRandNum(len(listPlayers), "Bob")
rndPlayer = gc.getPlayer(listPlayers[iRandNum])

Bh
 
Bhruic said:
Er, that's not a good way to do it. gc.getGame().countCivPlayersAlive() will return the number of alive players, true, but it won't determine what their numbers are. For example, if the game starts with players 1, 2, 3 and 4, and player 2 dies, the function will return that there are 3 players. You would be unable to randomly pick player 4, and if it randomly picks player 2, it will point to a dead player.

Errr... good point.
 
Bhruic said:
Er, that's not a good way to do it. gc.getGame().countCivPlayersAlive() will return the number of alive players, true, but it won't determine what their numbers are. For example, if the game starts with players 1, 2, 3 and 4, and player 2 dies, the function will return that there are 3 players. You would be unable to randomly pick player 4, and if it randomly picks player 2, it will point to a dead player.

A better way to do it (off the top of my head, I won't vouch for this working correctly):
Code:
listPlayers = []
for iPlayer in range(gc.getMAX_PLAYERS()):
 pPlayer = gc.getPlayer(iPlayer)
 if (pPlayer.isAlive()):
  listPlayers.append(iPlayer)
iRandNum = CyGame().getSorenRandNum(len(listPlayers), "Bob")
rndPlayer = gc.getPlayer(listPlayers[iRandNum])

Bh

Thank you so much guys. Bhruic your code worked perfectly.
 
I was just wondering where do you find all those functions.... I saw that they import CvPythonExtension so it all the functions must be in this file but I dont find this file....I always code in c++ or c sharp and try to understand python.

could you tell me where I should start to understand the code and where is CvPythonExtension

tnx
 
Sir Aragorn said:
I was just wondering where do you find all those functions.... I saw that they import CvPythonExtension so it all the functions must be in this file but I dont find this file....I always code in c++ or c sharp and try to understand python.

could you tell me where I should start to understand the code and where is CvPythonExtension

tnx

I do almost all of my python in CvEventManager.py in the python directory. The actually file isn't that critical, the talented guys (ie: not me) write their code in seperate files and inherit them.

If you are looking for the function definitions check out: http://civilization4.net/files/modding/PythonAPI/index2.html
 
Back
Top Bottom