Reputation: 337
This is what I got so far. This game can have multiple players up to len(players).
I'd like it to keep prompting the different players each time to make their move. So, for example, if there are 3 players A B C, if it was player's A turn, I want the next player to be player B, then the next player to be C, and then loop back to player A.
Using only while loops, if statements, and booleans if possible.
PLAYERS = 'ABCD'
def next_gamer(gamer):
count = 0
while count < len(GAMERS):
if gamer == GAMERS[0]:
return GAMERS[count + 1]
if gamer == GAMERS[-1]
return GAMER[0]
count = count + 1
Upvotes: 0
Views: 287
Reputation: 81
If you can't use the .index() method, I think what you are trying to do is this:
def next_gamer(gamer):
count = 0
while count < len(GAMERS):
if gamer == GAMERS[count]:
return GAMERS[count + 1]
count = count + 1
return GAMERS[0]
Upvotes: 2
Reputation: 81
You can just have your function return the next gamer in your list GAMERS using a simple statement like this one which works regardless of which gamer you are currently at in the game:
return GAMERS[GAMERS.index(gamer)+1]
unless the present gamer is the last in the list which you can test using the following if statement:
if GAMERS.index(gamer) == len(GAMERS) - 1:
return GAMERS[0]
in which case you return the first gamer.
Upvotes: 0
Reputation: 1763
My solution to your previous question, involving generators :
turn=0
def gameIsFinished():
global turn
turn = turn+1
return turn >10
def cycle(iterable):
while True:
for item in iterable:
if gameIsFinished():
return
yield item
for player in cycle([1,2,3]):
print player
Upvotes: 0