Python Language Please Part II: Play One Round (20 points) Complete the function
ID: 3718212 • Letter: P
Question
Python Language Please
Part II: Play One Round (20 points) Complete the function play-normal.round ), which takes twos arguments, in this order: 1. player!: a Player object that represents Player #1 2, player 2: a Player object that represents Player #2 Each Player object has equal number of Card objects in its cards attribute. The function draws cards from each player's hand (use the draw.card ) method in the Player class) until a player wins the round or the players run out of cards. Here is an example of how you might have Player #1 draw a card: player1_card -player1.draw_card ) If, on the first draw, the rank of Player #1's card is greater than the rank of Player #2's card, then Player #1 wins the round and the function returns the tuple ( 1, 2 ) , where l indicates Player #1 and 2 indicates the points won by the player. The function also adds 2 to player1-score. As in the real War card game, Player #1 earns 2 points because he "wins" two cards: his own, as well as Player #2's card. The cards are discarded and are not added to either player's card list. Similarly, if the rank of Player #2's card is greater than the rank of Player #1's card, then Player #2 wins the round and the function returns the tuple (2, 2 ) . The function also adds 2 to player2..score. Here is an exampleExplanation / Answer
Python3 solution
play_normal_round(player1, player2):
#Rounds
draws = 1
while True:
##Draw cards. This returns null if there are no crads left
player1_card = player1.draw_card()
player2_card = player2.draw_card()
##Print the cards
print("Player 1 drew "+player1_card)
print("Player 2 drew "+player2_card)
##Check if the cards are not null ie,check if the players have any cards left
##If one of player1_card/player2_card is null then they don't have anymore cards to continue the game
if player1_card == null or player2_card == null:
return (0, 0)
##Player1 wins
if player1_card > player2_card:
points = 2*draws
print("Player 1 won the round, scoring "+str(points)+" points")
##Update player 1 score
player1._score += points
return (1, points)
##Player2 wins
if player2_card > player1_card:
points = 2*draws
print("Player 2 won the round, scoring "+str(points)+" points")
##Update player 2 score
player2._score += points
return (2, points)
##If equal increment draws and continue
if player1_card == player2_card:
draws += 1
print("WAR!")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.