Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

WRITE UNITTESTS FOR THE PYTHON CODE BELOW: A) Test that if you create a card with numeric rank 12, its rank_name will be Queen B)

WRITE UNITTESTS FOR THE PYTHON CODE BELOW:

A) Test that if you create a card with numeric rank 12, its rank_name will be "Queen"

B) Test that if you create a card with rank 1, its rank_name will be "Ace"

C) Test that if you create a card instance with rank 3, its rank_name will be 3 D) Test that if you create a card instance with suit 1, its suit_name will be "Clubs"

E) Test that if you invoke the str method of a card instance that is created with suit=2, rank=7, it returns the string

"7 of Hearts

F) Test that if you create a Deck instance, it will have a list of 52 Cards in its cards instance variable

______________________________________________________________________________________________________________________________________________________

In [ ]: import random

import unittest

class Card(object): suit_names = ["Diamonds","Clubs","Hearts","Spades"] rank_levels = [1,2,3,4,5,6,7,8,9,10,11,12,13] faces = {1:"Ace",11:"Jack",12:"Queen",13:"King"}

def __init__(self, suit=0,rank=2): self.suit_name = self.suit_names[suit] if rank in self.faces: # self.rank_name handles printed representation self.rank_name = self.faces[rank]

else: self.rank_name = rank self.rank_num = rank # To handle winning comparison

def __str__(self): return "{} of {}".format(self.rank_name,self.suit_name)

class Deck(object): def __init__(self): # Don't need any input to create a deck of cards self.cards = [] for suit in range(4): for rank in range(1,14): card = Card(suit,rank) self.cards.append(card) # appends in a sorted order

def __str__(self): total = [] for card in self.cards: total.append(card.__str__()) return " ".join(total) # returns a multi-line string listing each card

def pop_card(self, i=-1): return self.cards.pop(i) # this card is no longer in the deck -- taken off

def shuffle(self): random.shuffle(self.cards)

def replace_card(self, card): card_strs = [] # forming an empty list for c in self.cards: card_strs.append(c.__str__()) if card.__str__() not in card_strs: self.cards.append(card) # append it to the list

def sort_cards(self):# remakes the deck in a sorted way self.cards = [] for suit in range(4): for rank in range(1,14): card = Card(suit,rank) self.cards.append(card)

def play_war_game(testing=False): # Call this with testing = True and it won't print out all the game stuff player1 = Deck() player2 = Deck()

p1_score = 0

p2_score = 0

player1.shuffle() player2.shuffle() if not testing: print(" *** BEGIN THE GAME *** ") for i in range(52): p1_card = player1.pop_card() p2_card = player2.pop_card() if not testing:

 print("Player 1 plays", p1_card,"& Player 2 plays", p2_card) 

if p1_card.rank_num > p2_card.rank_num: if not testing: print("\tPlayer 1 wins a point!")

 p1_score += 1 

elif p1_card.rank_num < p2_card.rank_num: if not testing: print("\tPlayer 2 wins a point!") p2_score += 1

else: if not testing: print("Tie. Next turn.")

In [ ]: result = play_war_game() print(""" ****** TOTAL SCORES: Player 1: {} Player 2: {} """.format(result[ if result[0] != "Tie": print(result[0], "wins")

else: print("TIE!")

___________________________________________________________________________________

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 14th East European Conference Adbis 2010 Novi Sad Serbia September 2010 Proceedings Lncs 6295

Authors: Barbara Catania ,Mirjana Ivanovic ,Bernhard Thalheim

2010th Edition

3642155758, 978-3642155758

More Books

Students also viewed these Databases questions