Permanent Alliance

OrionVeteran

Deity
Joined
Dec 25, 2003
Messages
2,443
Location
Newport News VA
I need to determine if a player has a "Permanent Alliance" and then determine which player the alliance is shared with.

The following line might indicate a team has an alliance, but I'm not sure.

Code:
Team.getNumMembers() > 1

I'd like to have the following function or something similar, which would return True or False:

Code:
if isHasPermanentAlliance(iPlayer1, iPlayer2):

Any ideas?
 
OK here is a function to determine if a specified player has a Permanent Alliance:

Code:
def isHasPermanentAlliance(iPlayer):
	zHasAlliance = False			
			
	for i in range(gc.getMAX_PLAYERS()):
		if i == iPlayer:
			pPlayer = gc.getPlayer(i)
			if not pPlayer.isNone() and not pPlayer.isBarbarian() and pPlayer.isAlive():
				iTeam = pPlayer.getTeam()
				Team = gc.getTeam(iTeam)
				if Team.getNumMembers() > 1:
					zHasAlliance = True
					break
					
	return zHasAlliance

I'm not sure how to do the second function (getting the who with).
 
What about e.g.

PHP:
def isHasPermanentAlliance(iPlayer):
        iActualTeam = gc.getPlayer(iPlayer).getTeam()
        if gc.getTeam(iActualTeam).getNumMembers() <= 1:
                ###there's no one else
                return -1
        else:			
                for i in range(gc.getMAX_PLAYERS()):
                        if i == iPlayer:continue
                        pPlayer = gc.getPlayer(i)
                        if not pPlayer.isNone() and not pPlayer.isBarbarian() and pPlayer.isAlive():
                                iTeam = pPlayer.getTeam()
                                if iTeam ==iActualTeam:
                                        return i
 
What about e.g.

PHP:
def isHasPermanentAlliance(iPlayer):
        iActualTeam = gc.getPlayer(iPlayer).getTeam()
        if gc.getTeam(iActualTeam).getNumMembers() <= 1:
                ###there's no one else
                return -1
        else:			
                for i in range(gc.getMAX_PLAYERS()):
                        if i == iPlayer:continue
                        pPlayer = gc.getPlayer(i)
                        if not pPlayer.isNone() and not pPlayer.isBarbarian() and pPlayer.isAlive():
                                iTeam = pPlayer.getTeam()
                                if iTeam ==iActualTeam:
                                        return i

Hmmm... The term "team" really gets me confused sometimes. Doesn't the following line say that iActualTeam does not have an Permanent Alliance?

Code:
if gc.getTeam(iActualTeam).getNumMembers() <= 1:

...and doesn't the following line say that i and iPlayer are one and the same?

Code:
if i == iPlayer:

if so, how can iPlayer and i share a permanent alliance, when they are one and the same?

What I am looking for is iPlayer1 and iPlayer2 sharing the alliance. Forgive me if I missed something obvious, but I just don't get how your example confirms that two different players share the same alliance. It might work if you have the following:

Code:
if i != iPlayer:

Very Respectfully,
 
Revising this might work:

Code:
def isHasPermanentAlliance(iPlayer): 
        iActualTeam = gc.getPlayer(iPlayer).getTeam() 
        if gc.getTeam(iActualTeam).getNumMembers() > 1:  ###there's is an alliance 
                            
                for i in range(gc.getMAX_PLAYERS()): 
                        if i != iPlayer:
                                pPlayer = gc.getPlayer(i) 
                                 if not pPlayer.isNone() and not pPlayer.isBarbarian() and pPlayer.isAlive(): 
                                         iTeam = pPlayer.getTeam() 
                                         if iTeam == iActualTeam: 
                                                 return i
 
...And I think this might even be better:

Code:
def isHasSharedPermanentAlliance(iPlayer1, iPlayer2):
	zHasSharedAlliance = False
	iPlayer1Team = gc.getPlayer(iPlayer1).getTeam()
	# Make sure iPlayer1 has an alliance
	if gc.getTeam(iPlayer1Team).getNumMembers() > 1:
		for i in range(gc.getMAX_PLAYERS()):
			if i != iPlayer1 and i == iPlayer2:
				pPlayer2 = gc.getPlayer(i)
				if not pPlayer2.isNone() and not pPlayer2.isBarbarian() and pPlayer2.isAlive(): 
					iPlayer2Team = pPlayer2.getTeam()
					if iPlayer2Team == iPlayer1Team:
						zHasSharedAlliance = True
	
	return zHasSharedAlliance

What do you think?
 
I think you like looping when you don't need to.

If the only thing you ever do in a loop is an "if" when the loop variable it is one specific thing, then just look up that one specific thing without the loop.

That is, instead of
Code:
	for i in range(gc.getMAX_PLAYERS()):
		if i == iPlayer2 :
			pPlayer2 = gc.getPlayer(i)
			blah
			blah
			blah
Just do
Code:
	pPlayer2 = gc.getPlayer(iPlayer2)
	blah
	blah
	blah
Which saves you from looping over all the other players that you were not interested in. No looping. No comparing values you don't care about to the value you already have. Faster. Better. (Probably not worth 6 million dollars.)

Oh, and for future reference the condition
Code:
if i != iPlayer1 and i == iPlayer2:
is somewhat redundant since if it is iPlayer2 then it isn't iPlayer1 unless you call the function with them set to the same value (which you could check for separately, before trying to do anything with the values, if you want to validate the input) so just the "i == iPlayer2" part of the expression is sufficient to get what you want.
 
Hmmm... The term "team" really gets me confused sometimes. Doesn't the following line say that iActualTeam does not have an Permanent Alliance?

Code:
if gc.getTeam(iActualTeam).getNumMembers() <= 1:

Sure ;). That's why it returns with a value afterwards, so the function doesn't execute further.

...and doesn't the following line say that i and iPlayer are one and the same?

Code:
if i == iPlayer:

if so, how can iPlayer and i share a permanent alliance, when they are one and the same?

You missed the "continue" statement afterwards ;). That command leads to skipping the whole code in the loop afterwards. So it prevents that the loop identifies the player being in the alliance with himself.
 
I think you like looping when you don't need to.

If the only thing you ever do in a loop is an "if" when the loop variable it is one specific thing, then just look up that one specific thing without the loop.

That is, instead of
Code:
	for i in range(gc.getMAX_PLAYERS()):
		if i == iPlayer2 :
			pPlayer2 = gc.getPlayer(i)
			blah
			blah
			blah
Just do
Code:
	pPlayer2 = gc.getPlayer(iPlayer2)
	blah
	blah
	blah
Which saves you from looping over all the other players that you were not interested in. No looping. No comparing values you don't care about to the value you already have. Faster. Better. (Probably not worth 6 million dollars.)

Oh, and for future reference the condition
Code:
if i != iPlayer1 and i == iPlayer2:
is somewhat redundant since if it is iPlayer2 then it isn't iPlayer1 unless you call the function with them set to the same value (which you could check for separately, before trying to do anything with the values, if you want to validate the input) so just the "i == iPlayer2" part of the expression is sufficient to get what you want.

Could it really be this simple?

Code:
def isHasSharedPermanentAlliance(iPlayer1, iPlayer2):
	iTeam1 = gc.getPlayer(iPlayer1).getTeam()
	iTeam2 = gc.getPlayer(iPlayer2).getTeam()
	if iTeam1 == iTeam2:
		return True

	return = False

If so, I assume that a successful negotiation to establish a permanent alliance simply changes the Team of one player to match the other.
 
OK no loop is required if I use the function above. However, in my bigger function, I still need to identify the following:

iPlayer1 (Pre-specified)
iPlayer2
iPlayer3

All 3 players must have met each other.
iPlayer2 must not have an alliance with iPlayer3
iPlayer2 and iplayer3 must not be None
iPlayer2 and iplayer3 must not be a Barbarion
iPlayer2 and iplayer3 must be alive

What's the most efficient way of identifying iPlayer2 and iPlayer3?
 
Back
Top Bottom