收藏 分销(赏)

扑克牌游戏编程.doc

上传人:pc****0 文档编号:7151140 上传时间:2024-12-27 格式: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 MakeDeck(

3、);/ 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/

4、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 g

5、etRandInt(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 fu

6、nction 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 outp

7、utreturn 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;

8、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)

9、;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./*Si

10、de 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.wid

11、th(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!=

12、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

13、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

14、/*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: strc

15、py(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 fu

16、nctions 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

17、 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

18、 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

19、/* 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_lastde

20、lt-;/ 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 Show

21、Card 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/

22、*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 structure

23、s 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

24、(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 deckout

25、index = 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之间的随

26、机数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_cardarraytopdeckind

27、ex;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 c

28、an 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/*

展开阅读全文
部分上传会员的收益排行 01、路***(¥15400+),02、曲****(¥15300+),
03、wei****016(¥13200+),04、大***流(¥12600+),
05、Fis****915(¥4200+),06、h****i(¥4100+),
07、Q**(¥3400+),08、自******点(¥2400+),
09、h*****x(¥1400+),10、c****e(¥1100+),
11、be*****ha(¥800+),12、13********8(¥800+)。
相似文档                                   自信AI助手自信AI助手
搜索标签

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        获赠5币

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:4008-655-100  投诉/维权电话:4009-655-100

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :gzh.png    weibo.png    LOFTER.png 

客服