Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a program to play the following simple solitaire game. N cards are dealt face up onto the table. If two cards have a matching

Write a program to play the following simple solitaire game. N cards are dealt face up onto the table. If two cards have a matching rank, new cards are dealt face up on top of them. Dealing continues until the deck is empty or no two stacks have matching ranks. The player wins if all the cards are dealt. Run simulations to find the probability of winning with various values of N.

Written in python. Please use the code below. Don't start this one from scratch.

from Deck import * from random import shuffle class SolitaireGame(object): """ a simple Solitaire Game: N cards are dealt face up on the table. If two cards have a matching rank, new cards are dealt face up on top of them. Dealing continues until the deck is empty, or no two stacks have matching ranks. The player wins if all the cards are dealt.""" def __init__(self, N): """Constructor pre: N is an integer, denotes the number of piles, s.t. 1 < N < 50 post: self.size is the number of piles""" if N < 1 or N>50: raise ValueError self.size = N def newGame(self): """ Creates a new game post: creates an instance of Solitaire game with N empty places""" self.deck = Deck() # creating a deck of cards self.deck.shuffle() # shuffling them in place self.places = [self.deck.deal() for i in range(self.size)] # creating self.size(N) piles, with one card in each def playRound(self): """ a round of a game pre: all piles are not empty post: piles with same rank get new cards on top of them, returns True if successful, and False if no cards were placed into piles""" #find two piles (in self.places) with the cards with the same rank, # say at position i and j #deal new cards from the deck into piles i and j #return True #if piles with cards of the same rank were found, then return False def playGame(self): """ plays a Solitaire game post: returns True, if player wins, and False otherwise""" roundResult = True # initially, to enter the while loop's body while roundResult: roundResult = self.playRound() if roundResult == True: # all cards were dealt from the deck, success! return True else: # not all cards were dealt from the deck, the player lost return False def __str__(self): """ prints out the layout of top cards at the moment, in self.size(N) piles""" #Tests # just one presented s = SolitaireGame(10) result = s.playGame() if result == True: print("The player won!") else: print("The player lost.") 
# Deck.py from random import randrange from Card import Card class Deck(object): #------------------------------------------------------------ def __init__(self): """post: Creates a 52 card deck in standard order""" cards = [] for suit in Card.SUITS: for rank in Card.RANKS: cards.append(Card(rank,suit)) self.cards = cards #------------------------------------------------------------ def size(self): """Cards left post: Returns the number of cards in self""" return len(self.cards) #------------------------------------------------------------ def deal(self): """Deal a single card pre: self.size() > 0 post: Returns the next card, and removes it from self.card if the deck is not empty, otherwise returns False""" if self.size() > 0: return self.cards.pop() else: return False #------------------------------------------------------------ def shuffle(self): """Shuffles the deck post: randomizes the order of cards in self""" n = self.size() cards = self.cards for i,card in enumerate(cards): pos = randrange(i,n) cards[i] = cards[pos] cards[pos] = card
# Card.py # updated for Bridge with Ace being the greatest # class Card(object): '''A simple playing card. A Card is characterized by two components: rank: an integer value in the range 2-14, inclusive (Two-Ace) suit: a character in 'cdhs' for clubs, diamonds, hearts, and spades.''' #------------------------------------------------------------ SUITS = 'cdhs' SUIT_NAMES = ['Clubs', 'Diamonds', 'Hearts', 'Spades'] RANKS = list(range(2,15)) RANK_NAMES = ['Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten', 'Jack', 'Queen', 'King', 'Ace'] #------------------------------------------------------------ def __init__(self, rank, suit): '''Constructor pre: rank in range(1,14) and suit in 'cdhs' post: self has the given rank and suit''' self.rank_num = rank self.suit_char = suit #------------------------------------------------------------ def suit(self): '''Card suit post: Returns the suit of self as a single character''' return self.suit_char #------------------------------------------------------------ def rank(self): '''Card rank post: Returns the rank of self as an int''' return self.rank_num #------------------------------------------------------------ def suitName(self): '''Card suit name post: Returns one of ('clubs', 'diamonds', 'hearts', 'spades') corrresponding to self's suit.''' index = self.SUITS.index(self.suit_char) return self.SUIT_NAMES[index] #------------------------------------------------------------ def rankName(self): '''Card rank name post: Returns one of ('ace', 'two', 'three', ..., 'king') corresponding to self's rank.''' index = self.RANKS.index(self.rank_num) return self.RANK_NAMES[index] #------------------------------------------------------------ def __str__(self): '''String representation post: Returns string representing self, e.g. 'Ace of Spades' ''' return self.rankName() + ' of ' + self.suitName() #------------------------------------------------------------ def __eq__(self, other): '''post returns True if two cards are equal, False otherwise''' return (self.suit_char == other.suit_char and self.rank_num == other.rank_num) #------------------------------------------------------------ def __lt__(self, other): '''post: returns True if self < other, False otherwise''' if self.suit_char == other.suit_char: return self.rank_num < other.rank_num else: return self.suit_char < other.suit_char #------------------------------------------------------------ def __ne__(self, other): '''post: returns True if two cards are not equal, False otherwise''' return not(self == other) #------------------------------------------------------------ def __le__(self, other): '''post: returns True if self <= other, False otherwise''' return self < other or self == other

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

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

Explain strong and weak atoms with examples.

Answered: 1 week ago

Question

Explain the alkaline nature of aqueous solution of making soda.

Answered: 1 week ago

Question

Comment on the pH value of lattice solutions of salts.

Answered: 1 week ago

Question

(7) How are you measuring progress and benefits?

Answered: 1 week ago

Question

(4) What level of commitment do people have towards the strategy?

Answered: 1 week ago

Question

(2) What do they not do so well?

Answered: 1 week ago