In: Computer Science
2. Implement a drawing card game:using python
2.1 Build a class Card() that contains 2 variables: rank and suits
(10 points)
2.2 Build a Deck that contains 52 cards with ranks and suits.
(20 points)
2.3 Two players will take turn to draw two cards from a shuffle
decks. (30 points)
You must implement a function call
playerTurn(deck) to handle a player turn.
playerTurn(deck) should
include these features:
2.3.1 Draw 2 cards from the Deck created in 2.2 2.3.2 Display ranks and suits of these cards
2.3.3 Return the sum of ranks of these cards. Notice that: ‘J’ = 11, ‘Q’ = 12, ‘K’ = 13, and ‘A’ = 1
2.4 The sum results will be compared between 2 players and who gets the larger in total will win that round. Notice that the game needs to keep track total number of player’s win round. (10 points)
2.5 Players have option to repeat as many times as they want. If the users choose ‘Y’, they will continue to draw from the original deck. (The deck will not be shuffled or added during the whole game. In other word, a card cannot be drawn twice during a game). (10 points)
'''
Python version : 2.7
Python program to simulate the game with standard deck of cards
'''
import random
# card class
class Card:
# constructor
def __init__(self, rank,suit):
self._rank = rank
self._suit = suit
# returns the string representation of the card
def __str__(self):
return 'Rank : '+str(self._rank)+' Suit : '+self._suit
# returns the rank of the card
def get_rank(self):
return self._rank
# returns the suit of the card
def get_suit(self):
return self._suit
# function to create and return shuffled deck of 52 cards
def create_shuffled_deck():
# define the valid suits and ranks
suits = ['C','D','S','H']
ranks = ['A','2','3','4','5','6','7','8','9','10','J','Q','K']
# create an empty deck
deck = []
# loop to populate the cards in the deck
for i in range(len(suits)):
for j in range(len(ranks)):
deck.append(Card(ranks[j],suits[i]))
# shuffle the deck
random.shuffle(deck)
# return the deck
return deck
# function to return the points of a card
def getCardPoints(card):
try:
rank = int(card.get_rank())
return rank
except ValueError:
if card.get_rank() == 'A':
return 1
elif card.get_rank() == 'J':
return 11
elif card.get_rank() == 'Q':
return 12
else:
return 13
# function to draw 2 cards from the deck, print the cards drawn and return the sum of the 2 cards
def playerTurn(deck):
# draw 2 cards
card1 = deck.pop(0)
card2 = deck.pop(1)
# print the cards
print(card1)
print(card2)
# calculate the sum
sum = getCardPoints(card1) + getCardPoints(card2)
# return the sum
return sum
# main function to simulate the game
def main():
# create a shuffled deck
deck = create_shuffled_deck()
# variables to store the wins of player1 and player2
winsPlayer1 = 0
winsPlayer2 = 0
cont = 'y'
# loop that will continue till the user wants or till deck is empty
while(cont.lower() == 'y'):
# check if deck is empty, then exit the game
if(deck != None and len(deck) == 0):
print('Deck empty. Please start the program again')
break
# draw cards for player 1
print('Player 1 cards: ')
player1 = playerTurn(deck)
# draw cards for player 2
print('Player 2 cards: ')
player2 = playerTurn(deck)
# determine who won
if player1 > player2:
print('Player 1 wins')
winsPlayer1 += 1
elif player1 < player2:
print('Player 2 wins')
winsPlayer2 += 1
else:
print("It's a tie")
# prompt the user if he/she wants to play again
cont = raw_input('Do you want to play another round (y/n) ? ')
print('')
# print the winnings of player 1 and player 2
print('Player1 won %d rounds' %(winsPlayer1))
print('Player2 won %d rounds' %(winsPlayer2))
print('Thank you for playing the game')
#call the main function
main()
#end of program
Code Screenshot:
Output: