Question: Im working with python and am unsure how to do this part # createDeck() produces a new, cannonically ordered, |S|*N card # deck. A deck
Im working with python and am unsure how to do this part
# createDeck() produces a new, cannonically ordered, |S|*N card # deck. A deck is implemented as a list of cards: each card is a # tuple, (v, s), where 0 >> createDeck(1) # [(1, 'spades'), (1, 'hearts'), (1, 'clubs'), (1, 'diamonds')] # >>> createDeck(2) # [(1, 'spades'), (2, 'spades'), (1, 'hearts'), (2, 'hearts'), (1, 'clubs'), (2, 'clubs'), (1, 'diamonds'), (2, 'diamonds')] # >>> createDeck() # [(1, 'spades'), (2, 'spades'), (3, 'spades'), ... (12, 'diamonds'), (13, 'diamonds')] # # where the second example above has been edited for clarity. Note # that the default, N=13, is to produce a standard 52 card deck having # the standard four suits specified in the function signature. def createDeck(N=13, S=('spades', 'hearts', 'clubs', 'diamonds')): deck=[] #loop for suit in S: #loop from 1 to N for i in range(1,N+1): #create tuple with rank i and current suit t=(i,suit) #append the tuple on the list deck.append(t) return deck #print(createDeck()) ###################################################################### # Construct the representation of a given card using special unicode # characters for hearts, diamonds, clubs, and spades. # # This function is provided for you; it need not be changed. def displayCard(c): suits = {'spades':'\u2660', 'hearts':'\u2661', 'diamonds':'\u2662', 'clubs':'\u2663'} return(''.join( [ str(c[0]), suits[c[1]] ] ))
###################################################################### # simpleShuffle(D) takes a deck, D, and modifies it to scramble the # order of the cards. There is a significant literature on # constructing "fair" random permutations of a sequence; what we are # after here is simple to implement but may not be entirely fair (for # an example of a fair shuffling algorithm, see the Fisher-Yates-Knuth # algorithm). # # simpleShuffle(D) takes as input list of elements. It should step # through the list, at each point exchanging the current element with # an element chosen at random from the remainder of the list # (including the present element). In other words, if we are # considering the 3rd element of a 10 element list (0-indexed as # usual), we select an index between 3 and 9, inclusive, and exchange # list[3] with list[0] before advancing to the 4th element of the list # and repeating the process. # # simpleShuffle(D) should return the permuted deck. # # Note that you will need to use the randint() function, which has # been imported for you from the random module at the top of this # file. def simpleShuffle(D)
A game is represented as a dictionary with keys: # stacks list of player stacks, where each stack # table list of cards currently on table (initially is a list of cards 1) next - index of player next to play (O or 1, initially 0) debt # penalty cards owed by next player (initially 0) # newGame() should first create a new shuffled deck using createDeck() # and simpleShuffle(). It should then return a dictionary describing # the initial state of the current game, where the shuffled deck has # been evenly divided amongst the players. So, for example (linefeed # added for clarity): # >>> newGame (2, # S-( ' spades, 'next' : O, 'hearts' ) ) { # 'table' : [], 'stacks ':[[(2, 'debt":0, 'spades'), (1, 'hearts')], [(2, 'hearts'), (1, 'spades')]]} # Note the division of the shuffled deck into two equal stacks, one # for player 0 and one for player 1. def newGame (N-13, S= ( spades . , hearts , , , clubs ' , ' diamonds ' ) ) : pass
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
