Misul
Misul

Reputation: 1

How to alternate between players?

I have to make a card game in which the starting player changes after each round or mini round (up to me to decide) but I have no clue how to alternate between the 2 players. After reading online, I tried using the cycle function from itertools but that creates further problems...

The problem is that I do not know how to alternate between the 2 players without messing up the rest of the program (displaying the player's hand and the scoring system)

To clarify in advance, the code snippets I am about to provide work perfectly as long as I do not alternate between the starting players.

Code:

print("Player 1, your cards are: ", hands[0])
print("Player 2, your cards are: ", hands[1])

In this case I want the hands index to change accordingly to the Player if i use the cycle function.

if cards.bigger_card(hands[0][play_1 - 1], hands[1][play_2 - 1], trump[0][1]) == 0:
    print("Congrats Player 2! You won this mini round.")
    score["score_2"] += 1
else:
    print("Congrats Player 1! You won this mini round.")
    score["score_1"] += 1

Here the score should update according to the Player that won the round.

Upvotes: 0

Views: 782

Answers (1)

yaputra jordi
yaputra jordi

Reputation: 533

I'm not sure if I've understood your question well enough. But I'll try.

To simply alternate between two index i.e. 0, 1, you can add a variable to store current turn and alternating it like this:

currentTurn = 0
def switchTurn():
  return (currentTurn + 1) % 2

Usage:

print(currentTurn)
# output: 0

switchTurn()
print(currentTurn)
# output: 1

print(hands[currentTurn])
# output player 2nd's hands (or cards)

Upvotes: 1

Related Questions