扑克牌游戏编程.doc_第1页
扑克牌游戏编程.doc_第2页
扑克牌游戏编程.doc_第3页
扑克牌游戏编程.doc_第4页
扑克牌游戏编程.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/*第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 ; /扑克的花色(黑、红、梅、方)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 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 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 Functions 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 effectsNone/*/*Tasks(1) Seeds the Random number 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 classesprog_close();/ pauses screen outputreturn 1;Playing_Card:Playing_Card() / CONSTRUCTOR/*/*Playing_Card Playing_Card CONSTRUCTOR FOR CLASS Playing_Card/*/*expects:None./*Returns:None/*Side effectsNone/*/*TasksConstructs object of class playing_card replaces default constructor/*/ Constructor replaces default constructorint i;for(i=1;i=3;)/ inits string variable to blanksm_Facei = ;i+;m_Suit = ;/ inits char variable to blankm_Value = 0;void Program_Init()/*/*void Program_Init():Shows Title/* /*expectsNone/*returns:None/*Side Effects:Text is centered on screen/*/*Task(s)Output program information to screen./*/*Center_Text(Lab #5);cout endl;Center_Text(A Deck Driver);cout endl n ; Center_Text(By Frank Vokoun); cout endl n ;Center_Text(C) 2001, Frank Vokoun);cout endl n ;Center_Text(Hit the 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 = 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 ;cout m_Face ;cout.width(1);cout m_Suit;cout ;void Center_Text(char ShellText80)/*/*Void Center_Text:Displays text centered on the screen./*/*expects:The text to display./*Returns:None./*Side effects:Outputs Centered text on screen./*/*int length;int center;length= strlen(ShellText);center = (80 - length)/2;for(;center!=0;center-)cputs( );cputs(ShellText); int get_number()/*/*/*int get_number:Gets an integer from the user./*expects:None./*Returns:Integer./*Side effects:None./*Task(s):Asks the user to enter an integer then returns that integer./*./*int Input_Integer = 0;Center_Text(Please enter an integer between 0 and 51. 52 to quit.);cout Input_Integer;return Input_Integer;void Playing_Card:Card_Make(int num)/*/* Get_Card(int):Creates a Playing_Card object based 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 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(i3)m_Facei = ; i+;m_Facei = j;i+;m_Facei = NULL; break; if(num 12 & num 25 & num 38 & num = 51)m_Suit = 4;if(face_num = 10)m_Value = 10;void DeckMakeDriver() /*/* DeckMakeDriver():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 operator 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 endl endl endl;char y;dodeck_1.Deal_One();cout endl;cout endl endl Y to Deal One more N to quit y;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 structure 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 cardarray void 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 /*int index = m_lastdelt + 1;int newline = 0;Center_Text(Current Deck of Cards from top to bottom);cout endl;while(index=51)if(newline % 11 = 0) 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 arrange 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 (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 endl;for(x=0;x=times;x+)/ iterate input number of timessplit = getRandInt(20,35);/ Get split locationDeck topdeck;/ Create 2 new unfilled decksDeck bottomdeck;int i;int bottomdeckindex = 1;int topdeckindex = 1;for(i=0;im_cardarrayi;topdeckindex+;i+;for(i=(split);im_cardarrayi;bottomdeckindex+;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 = 51)numcardstomovetop = getRandInt(2,7); /从上部分抽取的张数,是2-7之间的随机数numcardstomovebottom = getRandInt(2,7);/ Move a random number of cards(2-7) for(j=0;j 0)/ check for available cards this-m_cardarraydeckoutindex = bottomdeck.m_cardarraybottomdeckindex;deckoutindex+;bottomdeckindex-;for(h=0;h 0) & (deckoutindex m_cardarraydeckoutindex=topdeck.m_cardarraytopdeckindex;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 effects:Output text to screen/waits for user input to /*end program./*/*task(s)1. Pauses Program so the last output can be seen/*/*cout endl endl;cout Hit the 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 delt 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

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论