收藏 分享(赏)

扑克牌游戏编程.doc

上传人:yjrm16270 文档编号:6982801 上传时间:2019-04-29 格式:DOC 页数:12 大小:70KB
下载 相关 举报
扑克牌游戏编程.doc_第1页
第1页 / 共12页
扑克牌游戏编程.doc_第2页
第2页 / 共12页
扑克牌游戏编程.doc_第3页
第3页 / 共12页
扑克牌游戏编程.doc_第4页
第4页 / 共12页
扑克牌游戏编程.doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

1、/*第 1 题 扑克牌游戏-源代码及关键源代码注解如下:*/* This Program was written entirely by the author Frank Vokoun. /*preprocessor directives*/#include #include #include #include #include #include / used to seed the random number generator/*class Playing_Card /扑克类private:int m_Value;char m_Face3; /扑克的数字大小char m_Suit ; /扑

2、克的花色(黑、红、梅、方)public:Playing_Card(); / CONSTRUCTORvoid showcard(); / Displays an object of class Playing_Cardvoid Card_Make(int);class Deck /一副扑克(52 张)private:Playing_Card m_cardarray52; / Object array of class Playing_Cardint m_lastdelt;public:Deck(); / CONSTRUCTOR initializes empty Deck objectvoid

3、MakeDeck() ; / makes a complete object of class Deckvoid Deal_One(); / Deals a card from top of the deckvoid ShowDeck(); / Displays a Deck object to the screenvoid DeckShuff(int); / “Shuffles“ a Deck object for int timesvoid Remove_Card(); / Removes used card from Deck Prevents ; / mistakenly using

4、a nonexistant card/ can bu used to send a card ?/*prototypes* void Program_Init(); / Program informationint main();void Center_Text(char ); / Centers textint get_number(); / Gets a integer from userchar Get_Key(); / Gets a char from user, pauses programvoid DeckMakeDriver(); / Tests Several Function

5、s including Makedeckint getRandInt(int min, int max); / Random number generatorvoid prog_close(); / pauses before program termination/*Main*int main()/*/* int main: Main Function Calls other functions/*/* expects: None./* Returns: Integer 0;/* Side effects None/*/* Tasks (1) Seeds the Random number

6、generator/* (2) Calls Program Init function for Title etc./* (3) Call DeckMakeDriver to Test MakeDeck and DeckShuff./*/*srand( (unsigned)time( NULL ) ); / Seeds GetRandIntint Card_Number = 0;Program_Init(); / Showd title etc.DeckMakeDriver(); / The Main Function Driver Tests Deck and/ Playing_Card c

7、lassesprog_close(); / pauses screen outputreturn 1;Playing_Card:Playing_Card() / CONSTRUCTOR/*/* Playing_Card Playing_Card CONSTRUCTOR FOR CLASS Playing_Card/*/* expects: None./* Returns: None/* Side effects None/*/* Tasks Constructs object of class playing_card replaces default constructor/*/ Const

8、ructor replaces default constructorint i;for(i=1;i key to Continue“);cin.get();char Get_Key()/*/* char Get_Key(): Uses a input prompt to get a char/* /* expects: None./* Returns: Input char./* Side effects: Pauses Program execution (desired effect)./*/* Task(s): Gets a char from the user/*/*char x;x

9、 = cin.get();cout Uses calling members Playing_Card object /* Returns: None /* Side Effects: Displays /* /* Task(s): Displays the object of class Playing_Card to screen/* /*cout Input_Integer;return Input_Integer;void Playing_Card:Card_Make(int num)/*/* Get_Card(int): Creates a Playing_Card object b

10、ased on an interger input./* Expects: int /* Returns: None - uses Calling members Playing_Card object/* Side Effects: None/* /*Tasks: 1. assign a face value/* 2. assign a m_Suit(char type)/* 3. assign a point value/*/*int i = 0;char j;int face_num = num % 13; switch(face_num) / Assigns a Face value

11、for string cardscase 0: strcpy(m_Face,“ A“);break;case 9: strcpy(m_Face,“10“);break;case 10: strcpy(m_Face,“ J“);break;case 11: strcpy(m_Face,“ Q“);break;case 12: strcpy(m_Face,“ K“);break;default:j = char(face_num + 49); / Fills string with number mod 13if(i12 void DeckMakeDriver() /*/* DeckMakeDri

12、ver(): Used to test the various deck functions Deckmake,/* Deckshuff, Deckcopy etc./* Expects: None./* Returns: None./* Side effects: None /*/* Tasks: 1. Call make Deck /* 2. Show Deck /* 3. Call shuffle /* 4. Call show deck /* 5. While !Done Call /* a. Deal one /* b. Show card /*/*Note the dot oper

13、ator is need to access object because this is not a member of the class/*Deck deck_1;deck_1.MakeDeck(); / Creates the Deck.deck_1.ShowDeck(); / Displays deck to screen.Get_Key(); / Pauses Program.deck_1.DeckShuff(250); / Shuffles the deck 250 timesdeck_1.ShowDeck(); / Displays deck to screen.cout y;

14、y = toupper(y);while(y = Y );void Deck:MakeDeck() / creates a full deck not a construct/* /* Deck Make Deck(): Creates a Deck Object /* Expects: none - uses calling functions deck object/* Returns: none - uses calling functions Deck object /* Side effects: none /* Tasks: 1. Create a Full Deck struct

15、ure of unique card structures/* a. have the decks last delt variable = to the top card/*m_lastdelt = 51; / Set Deck to emptywhile(m_lastdelt -1) / iterate until deck is full. m_cardarraym_lastdelt.Card_Make(m_lastdelt); / call card make for every card objectm_lastdelt-; / inside the deck object ie c

16、ardarrayvoid Deck:ShowDeck() /* /* void ShowDeck: Displays the deck in a organized readable fashion to the screen/* Expects: none shows calling functions object of class deck /* Returns: none/* Side Effects: None /* Tasks: 1. set an index to lastdealt + 1 /* 2. call ShowCard passing in cardindex /*i

17、nt index = m_lastdelt + 1;int newline = 0;Center_Text(“Current Deck of Cards from top to bottom“);cout uses calling functions Deck object/* Returns: none - manipulates calling functions object of class Deck USING/* A THIS POINTER/* Side effects: no unintended effects./*/* Task(s) 1. To randomly arra

18、nge the elements of the Deck structure Playing_Card/* array./* A. split the Deck structure into two halves at a random location/* between 20 and 35./* B. Alternating between the two halves move a random number of /* Playing_Card structures to the original deck structure, Until/* it is full of cards

19、(52 cards in a deck).* this is also the /* number of cards available in the to halves./* C. Since it is a full unused deck set the lastdelt variable to -1/* /*/*int x, split; /split 是分开成两部分的位置,如上部分、下部分Center_Text(“Shuffling Deck“);cout m_cardarrayi;topdeckindex+;i+;for(i=(split);im_cardarrayi;bottom

20、deckindex+;i+;int deckoutindex = 0; / set deck to fills index to zeroint numcardstomovetop;int numcardstomovebottom;int j;int h = 0;bottomdeckindex = 52 - split; / set index to num cards in bottomtopdeckindex =split; / set index to num cards in topwhile(deckoutindex 0) / check for available cardsthi

21、s-m_cardarraydeckoutindex = bottomdeck.m_cardarraybottomdeckindex;deckoutindex+;bottomdeckindex-;for(h=0;h 0) deckoutindex+;topdeckindex-;this-m_lastdelt = -1; / Return a complete shuffled deckvoid prog_close()/*/* void prog_close: Waits for user input to end/* Inputs: None/* Returns: None/* Side ef

22、fects: Output text to screen/waits for user input to /* end program./*/* task(s) 1. Pauses Program so the last output can be seen/*/*cout key to Continue“ endl;cout endl endl;Get_Key(); / Necesary for clear input.cin.get();void Deck:Remove_Card()/*/* Deck Remove_Card(Deck deck_1): Removes the card d

23、elt from dealing deck./* Expects: The Deck that delt the card./* Returns: The Deck without the card delt./* Side effects: None./*/* Task(s): Remove card delt from Deck/*m_cardarraym_lastdelt= Playing_Card(); / reinits used card prevents mistakesvoid Deck:Deal_One() /*/* Deck Deal one(Deck):Deals a c

24、ard from the top of the deck /* Expects: A deck to deal from /* Returns: The deck minus the card delt /* Side effects: lastdealt in the deck is incremented /*/* Tasks: 1. Check if lastdealt = 51 if so Error /* 2. Increment last dealt /* 3. Return cardlast dealt /*if(m_lastdelt != 51) / Checks for av

25、ailable cardsm_lastdelt+;cout.width(5);cout “ Card delt“;m_cardarraym_lastdelt.showcard();elsecout “Out of range Error“;prog_close();Deck : Deck() / CONSTRUCTOR replaces default/*/* Deck Deck Class Deck CONSTRUCTOR /* Expects: A deck to deal from /* Returns: The deck minus the card delt /* Side effe

26、cts: lastdealt in the deck is incremented /*/* Tasks: 1. Check if lastdealt = 51 if so Error /* 2. Increment last dealt /* 3. Return cardlast dealt /*int lastdelt = 0;int i;for(i=0;i=51;i+)m_cardarray1 = Playing_Card(); / calls constructor class Playing_Card / for every Playing_Card object in / the class Decks array.

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报