讲解:Java:CSF002B Cards and HandsJava、Java

代写卡牌游戏基础,实现Cards和Hands等棋类游戏类。Understand the Class and ProblemThere are two basic classes we’ll need this week:Card: A class like the one presented in the modules, but with a few changes.Hand: A class that represents the cards held by a single player.Here are eight cards, each of which contains both a value (‘A’, ‘2’, ‘3’, … ‘T’, ‘J’, ‘Q’,’ K’) and a suit (spades, hearts, diamonds, clubs)Notice that I am using the char ‘T’ to describe the value 10. (Ignore the Joker, which we will not need.)The dealer uses a Deck object (not implemented this week) to deal Hand objects to the players. The dealer may or may not be a player who gets a hand of his or her own (poker dealers in casinos don’t receive a hand, but most other games involve the dealer getting a hand).Card: The Card class has two members: value (a char) and suit (an enum). But we add a new bool, errorFlag, which can inform a client that a card is in an illegal state. We’ll want the usual constructors, mutators, accessors and toString() methods for the class. We only allow standard cards, like (‘A’, clubs), (‘9’, hearts) and (‘T’, diamonds), no jokers or other special cards.Hand: As you can see, a Hand object usually contains several cards, so we’ll need an array of Card objects (myCards) as the principal member of the Hand class. Since each game deals a different number of cards into its players hands, and even within a game the number of cards in a hand will increase or decrease, we must keep track of this with an int value (numCards). We’ll need constructors, mutators, etc., of course. We’ll also want a way for the hand to receive a card (from the deck or somewhere else), and play a card (to the table or to another player). These two methods will be called takeCard() and playCard(), respectively. Since this class has no information about the game being played, it always puts new cards received by takeCard() into the next available location of the array (index position numCards) and plays a card via playCard() from the highest occupied location (index position numCards - 1). The client game application would somehow prepare this highest position with the correct card to be played before calling Hand’s playCard() method. This detail is not our concern.Phase 1: The Card ClassA Public enum TypeDefine the Suit enum, { clubs, diamonds, hearts, spades }, inside the Card class prototype.Private Member DataInclude three members:123char value;Suit suit;bool errorFlag;& Public MethodsCard(char value = ‘A’, Suit suit = spades) - The constructor should call the proper mutator(s). Because we have the errorFlag member, the constructor (via the mutator), can set this flag to true when it gets bad data; it does not have to assign default values upon receipt of bad data. This is a new technique for us. The default card (no parameters passed) is the (‘A’, spades).string toString() - a stringizer that the client can use prior to displaying the card. It provides a clean representation of the card. If errorFlag == true, it should return correspondingly reasonable reflection of this fact (something like “[ invalid ]” rather than a suit and value).bool set(char value, Suit suit) - a mutator that accepts the legal values established in the earlier section. When bad values are passed, errorFlag is set to true and other values can be left in any state (even partially set). If good values are passed, they are stored and errorFlag is set to false. Make use of the private helper, listed below.No mutator for errorFlag - that would not make sense.Accessors for suit, value and errorFlag.bool equals(Card card) - returns true if all the fields (members) are equal and false, otherwise.Private Methodsstatic bool isValid(char value, Suit suit) - a static helper method that returns true or false, depending on the legality of the parameters. Note that, although it may be impossible for suit to be illegal (due to its enum-ness), we pass it, anyway, in anticipation of possible changes to the type from enum to, say, char or int, someday. We only need to test value, at this time.Note: we don’t need individual mutators for value or suit since they would not be useful for this particular class.Recommended test of Card classInstantiate at least three cards, one of them, passing illegal values. Display all three. Then makeJava代写:CSF002B Cards and Hands调试Java作业、Java实验作业帮写 good card bad using set() with an illegal value, and turn the bad card “good” by setting a legal value, and re-display. This is a simple test, but you can test more thoroughly if you wish.Example Test Run of Card Class/* --------------------------------------------------------- A of Spades [ illegal ] J of Clubs [ illegal ] Q of Spades J of Clubs Press any key to continue . . . ----------------------------------------------------------- */ Phase 2: The Hand ClassStatic Class ConstantsDefine a const public int value like MAX_CARDS and set it to something like 30 or 50 so a runaway program can’t try to create a monster array.Private Member Data12Card myCards[...];int numCards;Public MethodsHand() - a default constructor.void resetHand() - remove all cards from the hand (in the simplest way).bool takeCard(Card card) - adds card to the next available position in the myCards array if the parameter is an error-free Card object and if there is room in the Hand object for another card (according to MAX_CARDS). It returns false if the Hand was full and true otherwise, even if card was an invalid (error-containing) Card. So, if card is invalid but there would have been room for it, the method will return true, even though it did not add card to the hand.Card playCard() - returns and removes (effectively, not physically) the card in the top occupied position of the array.string toString() - a stringizer that the client can use prior to displaying the entire hand.Accessor for numCards.Card inspectCard(int k) - Accessor for an individual card. Returns a card with errorFlag = true if k is bad.Recommended test of Hand classCreate between two and five explicit Card objects and one Hand object. Use takeCard() on these few cards (resulting in many, unavoidable “duplicates” in the hand) in a loop to populate the hand until the maximum allowable cards is met (use this criterion to end the loop). Display the hand using toString(). Next, play each card in a loop, until the hand is empty. Display the card played as it is played, and finally, display the (now empty) hand, verifying that no cards remain. At some point in your program, test inspectCard() with both legal and illegal int arguments.Example Test Run of Hand Class/* ------------------------------------------------------------------------- Hand full After deal Hand = ( 3 of Clubs, T of Clubs, 9 of Hearts, 3 of Clubs, T of Clubs, 9 of Hear ts, 3 of Clubs, T of Clubs, 9 of Hearts, 3 of Clubs, T of Clubs, 9 of Hearts, 3 of Clubs, T of Clubs, 9 of Hearts, 3 of Clubs, T of Clubs, 9 of Hearts, 3 of Clu bs, T of Clubs, 9 of Hearts, 3 of Clubs, T of Clubs, 9 of Hearts, 3 of Clubs, T of Clubs, 9 of Hearts, 3 of Clubs, T of Clubs, 9 of Hearts, 3 of Clubs, T of Clu bs, 9 of Hearts, 3 of Clubs, T of Clubs, 9 of Hearts, 3 of Clubs, T of Clubs, 9 of Hearts, 3 of Clubs, T of Clubs, 9 of Hearts, 3 of Clubs, T of Clubs, 9 of Hea rts, 3 of Clubs, T of Clubs, 9 of Hearts, 3 of Clubs, T of Clubs ) Testing inspectCard() 9 of Hearts ** illegal ** Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs Playing 9 of Hearts Playing T of Clubs Playing 3 of Clubs After playing all cards Hand = ( ) Press any key to continue . . . ----------------------------------------------------------------------- */ There is no Option B this WeekOnce your assignment is graded and returned, you can view the instructor solution here.Your access code will be provided in your graded assignment comments. Find the assignment in the list, click “Take Survey” and you will see the solution. Even though it is called a “Quiz”, it is actually just a solution; there is no need to submit anything, just open the quiz and see the solution.转自:http://ass.3daixie.com/2019011414390096.html

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,098评论 5 476
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,213评论 2 380
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 149,960评论 0 336
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,519评论 1 273
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,512评论 5 364
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,533评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,914评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,574评论 0 256
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,804评论 1 296
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,563评论 2 319
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,644评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,350评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,933评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,908评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,146评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,847评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,361评论 2 342

推荐阅读更多精彩内容