首页 > > 详细

Python辅导儿童纸牌游戏 Computer Science I Foundations讲解Python

Introduction
Requirement
CS1210 Computer Science I: Foundations
Homework 5: Ruba Mazzetto, Completed
Due Monday, October 31 at 11:59PM
Introduction
In Homework 4, you completed the simple implementation of the Italian children’s card game called
Ruba Mazzetto. In this homework, we’ll finally finish the complete version of the game that pits you
against any number of other players, and includes provisions for taking more than one card each turn. In
the interest of brevity, you can refer to the descriptions of Homework 3 and Homework 4 for more
information about the game.
Background
For this assignment, we’ll start with a refactored version of the Homework 4 solution, that incorporates
one small change to the rules of the game 1 and significantly revamps the structure of the play() function.
Your job is to complete this refactored version to support taking more than one card per turn, where the
cards matched must sum to the value of the card played. So, for example, the solution to Homework 5 will
behave as follows (excerpt taken from the middle of a 3-player game):
Pl aye r 0 :
St ash : [P l aye r 1] 7♠ (3) , [P l aye r 2] 6♥ (2)
Ta b l e : [3] 10♦, [4] 3♦
My hand : [0] 10♣, [1] 10♠
Pl ay wh i ch ca rd? 0
Se l ec t ma t ch i ng i nd ece s sepa r a t ed by spa c es (b l ank t o d i sca rd) : 1 4
Pl aye r 0 p l ays 10♣ and . . .
. . . . t ake s 3♦ f rom t ab l e
. . . . s t ea l s p l aye r 1 s t ash 7♠
=========
Pl aye r 1 :
St ash : [P l aye r 0] 10♣ (5) , [P l aye r 2] 6♥ (2)
Ta b l e : [3] 10♦
Pl aye r 1 p l ays 6♦ and . . .
. . . . s t ea l s p l aye r 2 s t ash 6♥
=========
Pl aye r 2 :
St ash : [P l aye r 0] 10♣ (5) , [P l aye r 1] 6♦ (3)
Ta b l e : [3] 10♦
Pl aye r 2 add s 1♦ t o t ab l e
1
The small change referred to here has to do with what happens to the cards that remain on the table at the end
of the game. In the ‘‘real’’ version of the game, the last person to match cards from the table or another player’s
stash is gifted all of the remaining cards on the table when everyone else has finished playing their cards. This
change has been incorporated into the play() function.
1 Revised October 24, 2016
Note how Player 0 uses the 10♣ to match a 3♦ from the table and the 7♣ from Player 1’s stash (because
10 = 3 + 7).
As for HW4, to make this homework feasible, you will be provided a code skeleton containing nearly all
of the code you will need for the solution. Your assignment is to complete the functions getMove() and
pickMove(), but you should begin by studying the code skeleton file carefully.
def play(N=2, K=10)
The play() function has undergone the most significant reworking since the previous assignment. Aside
from an additional variable, which changes the parameters of the deck you play with, the big change is in
the main iteration that continues until the deck is exhausted. Previously, this loop was divided into two
sections: one for the human player and one for everyone else. The new structure merges these two
sections into a single loop and relegates the differentiation of the human player from the computer
player(s) to getMove(). The last change to play() involves assignment of any cards remaining on the table
to the last player who successfully matched, a job handled by the addition of a variable, last, that is reset
each time a player makes a match.
def getMove(i, N, H, T, P)
The purpose of getMove() has changed significantly from HW4. In HW4, getMove() was to return a
single ‘‘best’’ move for a given (automated) player, where each move consisted only of matching a single
card from the player’s hand with a single card from the table or the top card from an opposing player’s
stash. But now that we permit matching a single card from the player’s hand with the sum of multiple
cards from the table or from the top of each opposing player’s stash, we need to generate and then
consider the space of all possible legal moves. Each move is represented as (c, M), where c is the index
into the player’s hand correspond to the card they will play and M is a list of indeces selected from the
table and/or the opposing player’s stashes such that sum(M)≡ H[i][c][0]. If (c, M) is (c,[]), then the
move corresponds to discarding c to the table.
Your job is to implement helper(c,v, j, M, L) to generate all legal moves, from which one will be
selected and returned as the value of getMove(). Let’s examine getMove() more closely:
(1) The first step is to determine if the argument i relates to the human player (i ≡0). If so, then move
selection is relegated to pickMove().
(2) The second step invokes the as-yet-unwritten helper function helper(c,v, j, M, L) repeatedly
(once for each card held by the current player) to generate the list L of all legal moves.
(3) The third step returns a random card to discard if no legal moves were found.
(4) The final step returns the legal move that maximizes the number of cards captured by the current
player (note that other heuristics might be used to make a ‘‘best’’ move selection from the list L of
legal moves).
The helper() function is a recursive function that constructs the set of legal moves. If you think of the
len(T) cards on the table and the up to N − 1 player stashes (excluding the current player’s stash) as
defining a space of size 2 len(T)+N−1 , you will need to enumerate the up to this many card combinations by
considering possible inclusion or exclusion of each successive card. The arguments to
helper(c,v, j, M, L) are designed to help you do so.
(1) The first argument, c, is the index of the card you are trying to match from player i’s hand.
(2) The second argument, v, initially the value of c, represents how many ‘‘points’’ remain to match
with the addition of each successive card. When this number reaches 0, you know the cards
you’ve selected so far sum to the original value you were trying to match.
2 Revised October 24, 2016
(3) The third argument, j, initially 0, is the index of the card you are currently considering adding to
the legal move you are construction. On each successive recursive call, you’ll increase j by 1 to
indicate you are going to consider the next card, from 0 through N up to N + len(T) − 1. At each
recursive step, you will want to explore both adding the jth card to the current legal move or not
adding it.
(4) The fourth argument, M, initially [], is the legal move under construction. Each time you elect to
include an additional card, you will need to extend M to include that card’s index.
(5) The last argument, L, initially [], is the list of legal moves you have found so far. Each new leg al
move you discover should be added to L, and the updated L should be the value returned from
helper().
def pickMove(i, N, H, T, P)
pickMove() is called by getMove() when the first argument i to getMove() corresponds to the human
player. pickMove() interacts with the user to obtain a legal move (c, M). Note that pickMove() must
guarantee that the move is both legal in every sense and that the cards selected from the table and the top
of any opposing player’s stash do, in fact, sum to the value of the card selected from the player’s hand.
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 start working on your assignment in discussion
section and continue working on it in next week’s discussion section. We ask that you upload a 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. Once you upload your final solution to ICON (when the assignment is complete),
your will be asked to fill out a separate short survey about your work on the assignment.
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. 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 hav e several sources for help. 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 (see the announcement on ICON
entitled General TA Office Hours start Thursday, September 1 for times and locations). You may also
attend my office hours Tuesdays from 1:30 to 3:30 in my office (14 MLH). Finally, you may email your
code with a specific question to me or to your TA, but please be sure that you include CS1210 on the
subject line (also, this is the slowest way of getting help, due to the volume of mail we are likely to
receive).
What to Hand In
As before, for credit, you must make sure your function definitions exactly match the ‘‘signatures’’ giv en
in this problem specification. Your files should be uploaded to the HW5 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, hw5.py, can be downloaded from ICON), substituting your own name for mine, obviously.
obviously. Note that the template file is incomplete and will not load in its present state.
3 Revised October 24, 2016
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.
4 Revised October 24, 2016

 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!