Python代写:CS1210 Ruba Mazzetto Continued

继续代写Ruba Mazzetto的扑克牌游戏,这次需要用递归来实现AI逻辑部分。


In Homework 3, you implemented portions of the Italian children’s card game called Ruba Mazetto. In this homework, we’ll complete a preliminary version of the game that pits you against any number of other players.

As a reminder, Ruba Mazetto (“steal my stash”) is an Italian children’s card game that, in some variants, is used be devious Italian grandmothers to teach young children basic addition (here, we’ll skip the addition game). Played by at least two and as many as four players, it uses a traditional 40-card Neapolitan deck (clubs, swords, cups and coins); here, we’ll use a regular card deck, throwing out the kings, queens and jacks to leave 40 cards with the traditional suits (clubs, spades, hearts and diamonds).

Here’s how the game is played (note: this description is somewhat simplified from that in homework 3). After shuffling the deck, the dealer deals three cards to each player, and lays four cards face up on the table. The remaining cards are stacked into a “draw pile,” face down, in the center of the table. The player then take turns: each turn, they play a single card which they can use to “capture” a face up card from the table if and only if the cards captured match the value of the card played. Captured cards are placed in the player’s “stash” that is, stacked in front of the player who captured them, with the capturing card face up on top of the stash. Note that a player may instead capture another player’s stash by matching the value of the top card. 1 If no captures are possible, the player adds their own card to the table, face up, for the other players to match (i.e., the player “discards” a card from his/her hand). When the players run out of cards, they are each dealt three more cards until eventually the deck is exhausted; at the same time, cards are added face up to the table if there are fewer than four remaining. Scoring at the end of the game is simple: the player with the largest point tally (the sum of all the cards in his or her stash) wins. Of course, what makes the game fun, at least for small children, is the notion of stealing the other player’s stash!

The flow of the game should be apparent from the trace. Here we are playing the two player version; play(4) would play the 4 player version of the game. In every case, player 0 is the human player, who must select which card to play and what to match, while all of the other players play automatically.

To make this homework feasible, you will be provided a code skeleton containing much of the code you will need for the solution. You will have to implement a few additional parts (or functions) to make the system complete, as well as complete a portion of the skeleton.

def displayCard(c)

displayCard() takes a tuple representing a card and returns a string, suitable for printing, that consists of the a two character sequence like 1♥ or 10♣. The special card suit symbols are provided for you in the partial definition of displayCard().

def showTable(N, D, T, H)

showTable() is responsible for printing out the state of the table each turn. It’s output looks like this:

Stash: [Player 0] 6♠ (*16), [Player 1] 2♦ (*18)
Table: [2] 3♣ , [3] 7♦

where each “stash” is identified by the player number (from 0 to N-1, inclusive), the top card and the number of cards in the pile, and the “table” is identified by an index (starting at N through N+len(T)-1, inclusive). Of course, showTable() should make appropriate use of displayCard().

Because this function deals with pretty much every important data structure in the game, it’s probably a good idea to review each one.

  • N: an int, the number of players in the game;
  • T: a list of tuples, each representing a card on the table; and
  • P: a dictionary, indexed by player number, containing each player’s stash.

More precisely, the value of P[i] is a list of tuples representing the cards in the player’s stash, with P[i][-1] being the last card played that captured cards for player i. Thus, P[i][-1] is the card that shows on top of the stash, and the card a competing player must match in order to capture the entirety of player i’s stash.

def deal(N, D, T, H)

This function deals 3 cards to each of N players from deck D, and then “completes” the table T ensuring the table has at least 4 cards showing. The last argument, H, is a list of lists, where H[i] represents the ith player’s current hand. Initially, H will be a list of empty lists, where len(H)==N, and T will be an empty list representing an empty table. Recall that, because D, T and H are all lists or dictionaries, any changes made to these structures will be propagated back to the calling context.

Also, note the skeleton for deal() provided uses Python’s error handling try/except construct, which will be covered in class shortly.

def getMove(H, T, P)

This function is what picks a move for an automated player. As currently implemented, it only matches cards from the automated player’s hand, H, with cards on the table, T. If a match is found, it returns a tuple (c, t) such that H[c][0] == T[t][0] (i.e., the card values match), or the special tuple (c, None) when there is no matching card on the table for any of the cards in H (in this case, getMove() may return any legal index from H, indicating the card that should be discarded to the table).

For this assignment, you will extend getMove(H, T, P) to allow for matching with other players’ stash stored in P. A good reference for this behavior is pickMove(), which provides the analagous functionality for the human player 0.

def play(N=2)

This function drives the entire game. It sets up the data structures that represent the state of the game, deals the cards, queries the human user for his/her move, inv okes getMove() for the automated players, checks all moves to ensure they are valid, and executes the moves by manipulating the data structures. Once the cards in the deck are exhausted, it tallies the values of each player’s stash so that the winner can be determined.

Most of this function is provided for you; there are only specific parts of the code that need to be completed, spefically, updating the game data structures based on the values returned by pickCard() and pickMove(), and extending the portion of the code that handles the values returned by getMove() in accordance with the extensions to getMove() outlined above. Note that the code to handle the values returned by the current implementation of getMove() is included, and can be used as a model for the rest of play().

The Ground Rules

Because this class is part of the NSF study we have previously described to you, the ground rules may differ slightly section by section. In your section, you will be expected to work in pairs, working only with your assigned partner.

For this homework, you will work with the same partner as homework 3. You should plan to work with your partner during both this week’s discussion sections and next week’s discussion section. We ask that you and your partner upload your partial solution to ICON at the end of each session (this will not be graded, it is just to see how far you get during the discussion section). You will also be asked to answer a short survey about your experience after each discussion section. Note that you are free to continue collaborating with your partner until the assignment is complete; once you upload your final solution to ICON when the assignment is complete (and we ask that you both upload the identical solution), you will be asked to fill out a separate short survey about your work and to evaluate your partnership.

Just as a reminder, under no circumstances should you turn in the work of someone else (including any code or material you may find on the Internet) as your own, nor should you share your own work with others except for your designated preassigned partner. It is fine, however, to discuss general concepts with your classmates as long as these discussions do not lead to the actual exchange of code fragments or written solutions.

You have several sources for help, in addition to discussing the work with your partner. Your first recourse should always be to post your question on the ICON discussion board. This is the fastest place to go for clarifications or disambiguation, or for help with Python in general (remember, don’t post your solution or any part of your solution). Second, if you must share a portion of your code, you can always attend a TA help session.

What to Hand In

As before, for credit, you must make sure your function definitions exactly match the “signatures” given in this problem specification. Your files should be uploaded to the HW4 dropbox folder in ICON by the specified time and date: late assignments will not be accepted.

You should start building your solution file according to the template at the end of this assignment (the template file,, can be downloaded from ICON), substituting your own name and your partner’s name for mine, obviously. You’ll want to delete the pass statements, which are only there to allow the file to load into Python in its present form (that is, with function signatures but no bodies). That way, you can play with the functions I provided for you as you get underway.

Make sure that you follow the instructions given for each function exactly, as the autograder expects these functions to follow the naming convention shown. Also note the use of the comment character, #, at the beginning of each line that is not intended to be interpreted by Python. You should use comments liberally to explain what you are doing to the human reader.