## Introduction

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.