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. (Python)

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 = 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.") 

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

Step: 3

blur-text-image

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 Kroenke

4th Edition

0136086535, 9780136086536

More Books

Students also viewed these Databases questions