PYthon please! Part II: Deal the Cards (10 points) Complete the function deal ca
ID: 3717232 • Letter: P
Question
PYthon please!
Part II: Deal the Cards (10 points) Complete the function deal cards(), which takes one argument, deck, is a list Cards objects (but not necessarily exactly 52 cards). The objects in deck might appear in any random (shuffled) order. The function creates and returns (as a tuple) two Player objects with the following values: • player num: 1 or 2, as appropriate • score: 0 • cards: the empty list ThefunctionappendsalternatingCardobjectsfromdecktothe cardsattributeofeachPlayerobject.More specifically, 1. Player 1 receives the Card object at deck[0]. 2. Player 2 receives the Card object at deck[1]. 3. Player 1 receives the Card object at deck[2]. 4. Player 2 receives the Card object at deck[3]. and so on, for all the cards in deck.
Part III: Play One Round (20 points) Complete the function play normal round(), which takes twos arguments, in this order: 1. player1: a Player object that represents Player #1 2. player2: a Player object that represents Player #2 EachPlayerobjecthasequalnumberofCardobjectsinits cardsattribute.Thefunctiondrawscardsfrom 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 theroundandthefunctionreturnsthetuple(1, 2),where1indicatesPlayer#1and2indicatesthepointswon 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 example: CSE 101 – Spring 2018 Homework #5 Page 3 Player 1 drew A? Player 2 drew 8ê Player 1 won the round, scoring 2 points. If the two cards have the same rank, then we have a war! The function draws another card from each player’s hand (if any). Cards are continually drawn until the players run out of cards or one player wins the war by drawing a card of higher rank than the other player. Each draw of the cards earns the eventual winner 2 additional points. Here is an example: Player 1 drew 10ê Player 2 drew 10 WAR! Player 1 drew 2? Player 2 drew 2 WAR! Player 1 drew 5? Player 2 drew Qê Player 2 won the round, scoring 6 points. For the above example, the function would return (2, 6) and would update the value of player2. score accordingly. If the two players start a war and then run out of cards, the round is considered a tie. In this case the function returnsthetuple(0, 0).
Part IV: Determine the Winner (10 points) Complete the function check game winner(), which takes the following arguments, in this order: 1. player1: a Player object that represents Player #1 2. player2: a Player object that represents Player #2 The function returns 1 if Player #1 has the higher score, 2 if Player #2 has the higher score, or 0 if the two players have the same score.
Part V: War Variant #1: Suit Rank (20 points) Complete the function play with suits(), which takes two arguments, in this order: 1. player1: a Player object that represents Player #1 2. player2: a Player object that represents Player #2 This function provides an alternate form of gameplay to the rules implemented in the play normal round() function. Rather than decide the winner using card ranks, the winner is decided using suits: 1. Hearts beat Spades and Diamonds CSE 101 – Spring 2018 Homework #5 Page 4 2. Spades beat Diamonds and Clubs 3. Diamonds beat Clubs 4. Clubs beat Hearts Here’s an example: Player 1 drew 6? Player 2 drew 2? Player 2 won the round, scoring 2 points. Wars are now caused when two cards of the same suit are drawn, as in the example below: Player 1 drew Q? Player 2 drew 8? WAR! Player 1 drew K? Player 2 drew 9? WAR! Player 1 drew A? Player 2 drew 8 Player 1 won the round, scoring 6 points. If the two players start a war and then run out of cards, the round is considered a tie. In this case the function returnsthetuple(0, 0). Hint: recall that each suit is represented using an ID number (see the suits dictionary in the war classes.py file).Youcanthesenumberstocomparethe suitpropertiesoftwoCardobjects.
Part VI: War Variant #2: Scouting (30 points) Complete the function play with scouts(), which takes two arguments, in this order: 1. player1: a Player object that represents Player #1 2. player2: a Player object that represents Player #2 This function provides an alternate form of gameplay to the rules implemented in the play normal round() function. Play proceeds as in normal gameplay except when a player draws a card of rank 5 or lower (i.e., 2, 3, 4 or 5). In such cases, if the player has at least one more card in his hand, the player scouts by drawing a second card from his hand. The combined value of the first card played and the scouted card is the value used in the battle (withJ=11,Q=12,K=13,A=14). Iftheplayerisunabletodrawasecondcard(becausehehasrunoutof cards), then the round simply continues as normal. Here is an example: Player 1 drew 9? Player 2 drew 5ê Player 2 drew K? Player 2 won the round, scoring 3 points. CSE 101 – Spring 2018 Homework #5 Page 5 Player #1’s 9 of Clubs is valued at 9, whereas Player #2’s 5 and King together are valued at 18 (18 = 5 + 13). Therefore, Player #2 wins the round. Since 3 total cards were draw this round, the winner earns 3 points. Below’s an example where scouting the next card did not help Player #1, and he lost an extra card: Player 1 drew 2 Player 1 drew 7 Player 2 drew Q Player 2 won the round, scoring 3 points. It is possible that scouting a card can cause one or more Wars to break out. The Wars themselves might cause additional scouting to happen: Player 1 drew Qê Player 2 drew 4ê Player 2 drew 8ê WAR! Player 1 drew 7? Player 2 drew 3? Player 2 drew Kê Player 2 won the round, scoring 6 points. If the two players start a war and then one (or both) runs out of cards, the round is considered a tie. In this case thefunctionreturnsthetuple(0, 0).
Explanation / Answer
Solution:
Below is the deal_cards() function which takes deck as argument and return two player objects as tuples with values in them . Please refer the comments to understand what the code is doing.
def deal_cards(deck):
cards1=[] # declare empty card list for player 1
cards2=[] # declare empty card list for player 2
# iterate over deck and assign alternating card objects to each player
for index in range(0,len(deck),2):
cards1.append(deck[index]) # add card to player 1
if index+1<len(deck):
cards2.append(deck[index+1]) # add card to player
player1 = Player(1,0,cards1)
player2 = Player(2,0,cards2)
return player1 , player2
Note: please remember indentation is important in python.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.