C++程序設(shè)計(jì)電子寵物_第1頁
C++程序設(shè)計(jì)電子寵物_第2頁
C++程序設(shè)計(jì)電子寵物_第3頁
已閱讀5頁,還剩10頁未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

1、# i n elude< i o s t r e a#in elude <stdlib.h>using n amespaee std;class pet/建立一個(gè)寵物類 public:pet(i nt a=0,i nt h=0,i nt t=0,i nt m=0,i nt he=0,i nt st=15) age=a;hu nger=h;thirst=t;mood=m;health=he;step=st; int geta() retur n age;/構(gòu)造函數(shù)/提取各個(gè)私有成員int geth() retur n hun ger;int gett() retur n thi

2、rst;int getm() return mood;int gethe() retur n health;int getst() return step;void p2();void show();void emerge ncy(i nt w);void feed()hun ger-;step=step-5;void dri nk() thirst-;step=step-3;void doctor() step=step-10;void touch(i nt w);void fun (i nt w);virtual int getl() retur n age;/饑餓、口渴以及心情隨時(shí)間的變

3、化函數(shù)/各個(gè)數(shù)值的輸岀函數(shù)/各種特殊狀態(tài)函數(shù)/喂食函數(shù)喂水函數(shù)/看醫(yī)生函數(shù)/撫摸函數(shù)玩耍函數(shù)提取壽命值virtual void acti on (i nt act,i nt w,i nt emg) /主人采取各種動作函數(shù),定義為純虛函數(shù)/年齡/饑餓值/ 口渴值心情指數(shù)健康指數(shù)行動點(diǎn)數(shù)/饑餓、口渴以及心情隨時(shí)間的變化函數(shù)private:int age;int hun ger;int thirst;int mood;int health;int step;in li ne void pet:p2()int m;m=ra nd()%15;if(m<5) mood=mood-2;else if(

4、m<12) mood-;elsemood+;age+;hun ger=hu nger+; thirst=thirst+;in li ne void pet:show()/各個(gè)數(shù)值的輸岀函數(shù)cout«"現(xiàn)在的狀態(tài):"<<endl; cout«"年齡:"<<age;cout«"饑餓度:"<<hu nger;cout«"口渴度:"thirst;cout«"心情指數(shù):"vvmood; cout«&quo

5、t;健康指數(shù):"vvhealth;in li ne void pet:emerge ncy(i nt emg)step=step+5;if(emg=20)health-;if(hu nger=3)mood-;coutvv"主人,我已經(jīng)很餓了if(hu nger=-2)mood-;coutvv"主人,我已經(jīng)很飽了/各種特殊狀態(tài)函數(shù)"<<e ndl;"<<e ndl;if(hu nger>3)mood=mood-2;health-;coutvv"再不給我吃東西,我就要餓死啦!"<<e n

6、dl;if(hu ngerv-2)mood=mood-2;health-;coutvv"再給我吃東西,我就要撐死啦!"<<e ndl;if(thirst=2)mood-;cout«"這天好熱啊,主人,給口喝的吧。"<<e ndl;if(thirst=-2)mood-;cout«"面對著一大杯水,你的寵物惡心地說道:主人,我已經(jīng)喝夠了?!?"<<endl;if(thirst>2)mood=mood-2;health-;coutvv"偉大的主人在上,我已經(jīng)渴的快受不鳥

7、啦。"<<e ndl;if(thirst<-2)mood=mood-2;health-;coutvv"好吧,我都喝撐的都吃不下飯了。"<<e ndl;if(hu nger>6|thirst>5)age=100;cout<< "??!天吶,你降道雷劈了這個(gè)萬惡的主人吧!”說完,某寵物口吐白沫,到底不起。"<<endl;if(hu nger>5&&thirst>4)age=100;coutvv"如果有來世的話,打死我也不跟你混了。"<

8、;<e ndl;if(health<-6)age=100;coutvv"你骨瘦如柴的寵物最后飽含深情地看了你一眼,緩緩倒下了。"<<e ndl;in li ne void pet:touch(i nt w)/ 撫摸函數(shù)step=step-3;switch(w)mood=mood+2;break;case 1:mood+; break;case 2:mood+; break;case 3: mood-; thirst-; break;case 4: mood-; hun ger-; break;in li ne void pet:fu n(i nt w

9、)/玩耍函數(shù)step=step-8;switch(w)case 0:mood=mood+2;break;case 2:hun ger+;thirst+;health+;break;case 3:hun ger=hu nger+2;thirst+;health+;break;case 4:hun ger+;thirst+;health+;break;void in structio n()/飼養(yǎng)說明cout«"飼養(yǎng)說明:"<<endl;cout<<"每個(gè)時(shí)間段作為主人的你新增5點(diǎn)行動點(diǎn),未用完可以累積到下一時(shí)段,初始有20點(diǎn)。&qu

10、ot;<<endl;cout«"當(dāng)行動點(diǎn)相當(dāng)?shù)蜁r(shí),岀現(xiàn)bad end,飼養(yǎng)結(jié)束。"<<endl;coutvv"每天有三個(gè)時(shí)間段,你可以進(jìn)行5項(xiàng)操作,喂食、喂水、撫摸、玩耍、不做任何事。"<<endl;coutvv"行動點(diǎn)對應(yīng)消耗:喂食5點(diǎn),喂水3點(diǎn),撫摸3點(diǎn),玩耍8點(diǎn)。"<<endl;coutvv"選擇不做任何事,則跳到下一時(shí)間段。"<<endl;coutvv"當(dāng)饑餓與口渴相當(dāng)?shù)偷臅r(shí)候,或者健康指數(shù)相當(dāng)?shù)偷臅r(shí)候,會岀現(xiàn)bad end,寵

11、物死亡。"vvendl;coutvv"當(dāng)寵物年齡到達(dá)壽命極限時(shí),岀現(xiàn)happy end,寵物壽終正寢。"vvendl;coutvv"PS :不同的天氣對寵物的心情和健康又影響。"vvendl;coutvv"在不同天氣中進(jìn)行撫摸玩耍會有特殊情況發(fā)生,請注意。"vvendl;coutvv""vve ndl;class rabbit:virtual public pet/pet 類派生的兔子類public:rabbit(i nt l=31)life=l;in t getl()return life;void a

12、ctio n(int act,i nt w,i nt emg);/主人對小牛的動作函數(shù),虛函數(shù)成員private:int life;/兔子的壽命;in li ne void rabbit:acti on (i nt act,i nt w,i nt emg)switch(act)case 0:in structi on() ;break;case 1:feed();break;case 2:drin k();break;touch(w);break;case 4:fun( w);break;case 5:break;case 6:emg=0;doctor();break;class horse:

13、virtual public pet/pet 類派生的小馬駒類public:horse(i nt 1=41)life=l;in t getl()return life;void actio n(int act,i nt w,i nt emg);/主人對小牛的動作函數(shù),虛函數(shù)成員private:int life;小馬的壽命;in li ne void horse:acti on (i nt act,i nt w,i nt emg)switch(act)case 0:in structi on() ;break;case 1:feed();break;case 2:drin k();break;c

14、ase 3:touch(w);break;case 4:fun( w);break;case 5:break;emg=O;doctor();break;class calf:virtual public pet/pet 類派生的小牛類public:calf(i nt l=51) life=l;in t getl()return life;void actio n(int act,i nt w,i nt emg);/主人對小牛的動作函數(shù),虛函數(shù)成員private:int life;/小牛的壽命;in li ne void calf:acti on (i nt act,i nt w,i nt em

15、g)switch(act)case 0:in structi on() ;break;case 1:feed();break;case 2:drin k();break;case 3:touch(w);break;case 4:fun( w);break;case 5:break;case 6:emg=0;doctor();break;void petshop(i nt select)寵物外形switch(select)coutvv""vvendl;coutvv"1* *1")""vve ndl;coutvv"("

16、vve ndl;coutvv" ) ) _.-._"vve ndl;coutvv")/ ,*,*"vve ndl;coutvv"*> -*"vve ndl;coutvv"*0 *'* "vve ndl;coutvv"(丫(;*) "vve ndl;coutvv"*, "vve ndl;coutvv"(_,-* * *一)一)"vve ndl;coutvv"(,-( ,*"vve ndl;coutvv""

17、vvendl;break;case 2:cout«"恭喜,你已經(jīng)領(lǐng)養(yǎng)了一匹小馬駒。"<<e ndl;cout«""<<e ndl;cout«"%_"vve ndl;coutvv"%-*/./"vve ndl;coutvv"%-*/ *."vve ndl;coutvv"%* ."vve ndl;coutvv"%* :0"vve ndl;cout«"%*"<<e n

18、dl;cout«"%*"vve ndl;coutvv"%*"vve ndl;"vve ndl;coutvv"%*: *(,;"vve ndl;coutvv"%*.-.*"vve ndl;coutvv"%*coutvv"break;"vve ndl;"<<e ndl;"vven dl;coutvv"恭喜,你已經(jīng)領(lǐng)養(yǎng)了一頭小牛犢。COut«"COut«"/"<<e n

19、dl;COut«"/ / /"<<e ndl;COut«"| | |"<<e ndl;COut«"/"<<e ndl;COut«"/-|/ |"<<e ndl;COut«"/ = /| |O| |O| |/ = /"<<e ndl;COut«"-| | 11 1 -一-"<<e ndl;COut«"|/|"<&

20、lt;e ndl;COut«"/"<<e ndl;COut«"(OO)"<<e ndl;COut«"/./"<<e ndl;COut«"-1"<<e ndl;COut«"| |/_/|"<<e ndl;COut«"| |/|"<<e ndl;COut«"| |"<<e ndl;COut«&quo

21、t;| | |"<<e ndl;COut«"| | |"<<e ndl;COut«""<<endl;break;void period( int t)/ 時(shí)間函數(shù)t=t%3;switch(t)case O:cout<<"上午。"<<endl;break;case 1:cout<<"下午。"<<endl;break;case 2:cout<<"晚上。"<<end

22、l;break;int weather()/ 天氣函數(shù)int w;w=ra nd()%5; switch(w)case 0:cout<<"天氣晴。"<<e ndl;break;case 1:cout<<"天氣陰。"<<e ndl;break;case 2:cout<<"天氣小雨。"<<e ndl;break;case 3:cout<<"天氣暴雨。"<<e ndl;break;case 4:cout<<&quo

23、t;天氣return w;大風(fēng)。"<<e ndl;break;void raise(i nt select) int life,date,w,act;int t=0;/動態(tài)建立對象,實(shí)現(xiàn)領(lǐng)養(yǎng)int emg,Nstep,Nage; pet *petx;switch(select)case 1:petx=new rabbit; break;case 2:petx=new horse; break;case 3:petx=new calf;break;life=petx->getl();for(date=1;date<life;date+)cout<<&

24、quot;今天是你領(lǐng)養(yǎng)第/提取壽命/開始飼養(yǎng),如無意外,則到壽命時(shí)循環(huán)結(jié)束w=weather(); t=t%3;petx->p2();while(t<3)"wdatevv"日"<<endl;調(diào)用天氣函數(shù)/t為控制時(shí)間整形/調(diào)用饑餓、口渴以及心情隨時(shí)間的變化函數(shù)/控制時(shí)間的循環(huán)emg=ra nd()%20;/emg為隨機(jī)數(shù),控制是否生病"<<en dl;COUtvv"現(xiàn)在是"period(t);cout<<"-/調(diào)用時(shí)間函數(shù)"vven dl;cout<<e

25、ndl;doNstep=petx->getst(); cout<<"你的行動點(diǎn)數(shù)還有: coutvv"你的寵物" petx->show();/提取行動點(diǎn)數(shù)"vvNstepvve ndl;/調(diào)用數(shù)值輸岀函數(shù)if(emg-19)=1)cout<<" 健康狀況:生病"<<e ndl;elsecout«"健康狀況:良好"<<e ndl;cout«"請選擇你想做的事:"<<endl;cout<<&quo

26、t;0、參看說明1、喂食2、喂水3、撫摸4、逗它玩耍 5、不做任何 事";if(emg-19)=1)cout«" 6、看醫(yī)生。"<<endl;elsecout«e ndl;coutvv"請選擇:"cin> >act;cout«""<<e ndl;petx->actio n(act,w,emg);/ 調(diào)用主人動作函數(shù)while(act+1)%6);petx->emerge ncy(emg);/調(diào)用特殊狀態(tài)函數(shù)Nage=petx->geta();

27、/ 提取年齡if(Nage>life)cout«""<<e ndl;coutvv"很遺憾,由于飼養(yǎng)不善,你的寵物已經(jīng)死亡。"<<endl;cout«""<<e ndl;break;/非正常死亡,bad endif(NstepvO)coutvv""<<endl;coutvv"真不幸,寵物的飼養(yǎng)已經(jīng)讓你感到心力交瘁。"<<endl;coutvv"看來你和它共同的生活要就此告一段落了。"vvend

28、l;coutvv"唉,你的修行還未夠班啊,繼續(xù)努力吧。"vvendl;coutvv""vve ndl;break;/非正常結(jié)束飼養(yǎng),bad endt+;coutvv""vve ndl;if(Nage>life|Nstepv0)break;/非正常死亡,跳岀循環(huán)system("cls");調(diào)用系統(tǒng)清屏函數(shù)if(date=life)"vve ndl;-"vven dl;/正常死亡coutvv"coutvv"恭喜,你的寵物已經(jīng)壽終正寢。int adopt()/領(lǐng)養(yǎng)函數(shù),實(shí)現(xiàn)寵物的領(lǐng)養(yǎng)int sei;int chi;char

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論