Reputation: 3
I am making a poker game where I want to be able to customize the amount of players there are. I am trying to make a function that would move the dealer position to the next player at the table. I have a list of player objects and each have the boolean is_dealer. In the function I want to be able to make the boolean true for the next player on the list and make it false for the current player I am iterating through. My problem is that I don't know how to get the last player in the list to pass the position to the first player in the list.
def move_positions(self):
for people in range(number_of_players):
if self.players[people].is_dealer==True:
self.players[people].is_dealer= False
self.players[people+1].is_dealer=True
players is my list of player objects.
Upvotes: 0
Views: 71
Reputation: 11209
Exit the loop as soon as you find the dealer. Handle the case where the last player is the dealer. Also, remove comparison operator with boolean.
for people in range(number_of_players):
if self.players[people].is_dealer:
self.players[people].is_dealer = False
self.players[(people+1)%number_of_players].is_dealer = True
break
Upvotes: 0
Reputation: 10809
I would recommend that, instead of finding the current dealer each time you want to advance the dealer, you simply have a variable that keeps track of who the current dealer is. That being said, this snippet doesn't do that: It finds the current dealer, and then sets the next player to be the dealer, wrapping around if necessary:
def move_positions(self):
dealer_index = next(index for index, player in enumerate(self.players) if player.is_dealer)
self.players[dealer_index].is_dealer = False
self.player[(dealer_index+1)%number_of_players].is_dealer = True
Upvotes: 1