




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
1、C+課程設(shè)計報告對抗游戲1、 實(shí)驗?zāi)康模?1.加深對c+程序設(shè)計的理解,了解程序設(shè)計的流程。 2.增強(qiáng)對程序模塊化思想的概念 3.加深對類、構(gòu)造函數(shù),析構(gòu)函數(shù),友元函數(shù)的理解。2、 實(shí)驗內(nèi)容: 1.閱讀程序設(shè)計要求和源代碼,理解和分類各部分的功能。 2.設(shè)計一個類內(nèi)定義的函數(shù),實(shí)現(xiàn)增加賭注的功能。 3.設(shè)計友元函數(shù)實(shí)現(xiàn)比大小的功能和輸出結(jié)果功能。 4.寫出main函數(shù),整合各部分功能,完成程序。3、 實(shí)驗環(huán)境:VC+6.04、 算法或核心技術(shù)介紹: 1.利用類,實(shí)現(xiàn)各部分的定義。 2.在友元函數(shù)中利用if函數(shù)實(shí)現(xiàn)比較大小的功能。5、 附件程序代碼如下:#include "iostre
2、am"#include "math.h"#include "stdlib.h"#include "ctime"using namespace std;int flag;class CCard int nCard1,nCard2; /兩張牌 static int nLoop; /出牌次數(shù),游戲結(jié)束時給出總的出牌次數(shù) int nScore; /總分 int nWin,nLose; static nDraw; /贏,輸,平的次數(shù) int nBet; /賭注 public: int Bet;CCard(); /構(gòu)造函數(shù) int Se
3、tCard(); /出兩張牌 void Show(); /顯示牌點(diǎn) int AddBet(int); /增加賭注,失敗返回0 void friend Compare(CCard&,CCard&); /友元函數(shù),比較牌點(diǎn)大小,并顯示結(jié)果 void friend ShowResult(CCard&,CCard&); /友元函數(shù),輸出輸贏結(jié)果及最后積分 ;int CCard:nDraw=0;int CCard:nLoop=0;CCard:CCard()nScore=20;Bet=nScore;nWin=0;nLose=0;nBet=0;int CCard:SetCar
4、d()for(int i=0;i<=100000000;i+);srand(unsigned)time(0);nCard1=(rand()%13+1);nCard2=(rand()%13+1);return nCard1;void CCard:Show() if(nCard1>1&&nCard1<=10) cout<<nCard1<<" " else switch(nCard1) case 1: nCard1=1;cout<<"A"<<" "break;
5、 case 11:cout<<"J"<<" "break; case 12:cout<<"Q"<<" "break; case 13:cout<<"K"<<" " if(nCard2>1&&nCard2<=10) cout<<nCard2<<" " else switch(nCard2) case 1: nCard2=1;cout&l
6、t;<"A"<<" "break; case 11:cout<<"J"<<" "break; case 12:cout<<"Q"<<" "break; case 13:cout<<"K"<<" " cout<<endl;int CCard:AddBet(int a)nBet=a; if(nBet>nScore|nBet<1)
7、 return 0; return 1;void Compare(CCard& user,CCard& computer) if(user.nCard1=user.nCard2)&&(computer.nCard1!=computer.nCard2)user.nScore+=computer.nBet; computer.nScore-=computer.nBet; user.nLoop+; computer.nLoop+; user.nWin+; computer.nLose+; user.Bet=user.nScore; computer.Bet=compu
8、ter.nScore; if(computer.nScore<=0)ShowResult(user,computer);return; cout<<"這局玩家贏了,請繼續(xù)努力哦"<<endl; else if(user.nCard1!=user.nCard2)&&(computer.nCard1=computer.nCard2) computer.nScore+=user.nBet; user.nScore-=user.nBet; user.nLoop+; computer.nLoop+; computer.nWin+; use
9、r.nLose+; user.Bet=user.nScore; computer.Bet=computer.nScore; if(user.nScore<=0)ShowResult(user,computer);return; cout<<"這局計算機(jī)贏了,真遺憾"<<endl; elseif(user.nCard1+user.nCard2)>(computer.nCard1+computer.nCard2) user.nScore+=computer.nBet; computer.nScore-=computer.nBet; user.
10、nLoop+; computer.nLoop+; user.nWin+; computer.nLose+; user.Bet=user.nScore; computer.Bet=computer.nScore; if(computer.nScore<=0)ShowResult(user,computer);return; cout<<"這局玩家贏了,請繼續(xù)努力哦"<<endl; else if(user.nCard1+user.nCard2)<(computer.nCard1+computer.nCard2) computer.nScor
11、e+=user.nBet; user.nScore-=user.nBet; user.nLoop+; computer.nLoop+; computer.nWin+; user.nLose+; user.Bet=user.nScore; computer.Bet=computer.nScore; if(user.nScore<=0)ShowResult(user,computer);return; cout<<"這局計算機(jī)贏了,真遺憾"<<endl; else cout<<"和局"<<endl; us
12、er.nLoop+;computer.nDraw+; void ShowResult(CCard& user,CCard& computer) cout<<"-游戲結(jié)束!結(jié)果:-"<<endl; if(user.nScore<=0) cout<<"計算機(jī)勝出,玩家努力哦!"<<endl; else cout<<"恭喜玩家勝出!"<<endl; cout<<"出牌數(shù):"<<user.nLoop<
13、<endl; cout<<"玩家積分:"<<user.nScore<<endl; cout<<"玩家勝的次數(shù):"<<user.nWin<<endl; cout<<"玩家負(fù)的次數(shù):"<<user.nLose<<endl; cout<<"玩家平的次數(shù):"<<user.nDraw<<endl; cout<<"計算機(jī)積分:"<<co
14、mputer.nScore<<endl; cout<<"計算機(jī)勝的次數(shù):"<<computer.nWin<<endl; cout<<"計算機(jī)負(fù)的次數(shù):"<<computer.nLose<<endl; cout<<"計算機(jī)平的次數(shù):"<<computer.nDraw<<endl; cout<<"-"<<endl; flag=1; user.nLoop=0; user.nDra
15、w=0; cout<<"游戲結(jié)束,按任意鍵退出"<<endl; system("pause");void main() select: flag=0; int start,ucard,ccard,bet,cont,sel=1;/sel=0,玩家下注,sel=1,計下注/card第一張 char ch; CCard user,computer; cout<<"開始游戲請按1"<<" "<<"放棄請選0"<<endl;bingi
16、n :if(flag=1) system("cls"); goto select; cin>>start;switch(start)case 1: cout<<"開始發(fā)牌,期待一下吧" user.SetCard();ucard=user.SetCard(); computer.SetCard();ccard=computer.SetCard(); break; case 0:exit(0);cout<<endl;cout<<"你的第一張牌是:"<<endl;/if(ucard
17、<=10 && ucard>1) cout<<ucard;elseswitch(ucard) case 1 :cout<<" A" break; case 11:cout<<" J" break; case 12:cout<<" Q" break; case 13:cout<<" K"cout<<endl;cout<<"是否選擇下注:"<<endl;cout<<
18、"選1下注,選0放棄"<<endl;cin>>cont;switch(cont) case 0:sel=0;goto nextplayer;cout<<"你現(xiàn)在還有"<<user.Bet<<"賭注"<<endl;bet:cout<<"請押賭注"<<endl;cin>>bet;if(bet>5) cout<<"押注請不要超過5哦,重試一下吧"<<endl; go
19、to bet;if(!(user.AddBet(bet)&&computer.AddBet(bet) cout<<"押注要大于0喲,再來一次"<<endl; goto bet;nextplayer: if(sel=0) switch(ccard) case 14:bet=5;break; case 13:bet=3;break; case 12:bet=2;break; case 11:bet=1;break; case 10:bet=4;break; case 9:bet=1;break; case 8:bet=3;break; c
20、ase 7:bet=4;break; case 6:bet=5;break; case 5:bet=1;break; case 4:bet=5;break; case 3:bet=2;break; case 2:bet=3;break; while(!(user.AddBet(bet)&&computer.AddBet(bet) bet-; cout<<"計算機(jī)下注"<<bet<<",你是否接受Y/N"<<endl; cin>>ch; switch(ch) case 'Y': case 'y': break; case 'N': case 'n': cout<<
溫馨提示
- 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)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 修改利用合同范本賺錢
- 住宅合同范本
- 低價轉(zhuǎn)讓轉(zhuǎn)租合同范本
- 出售積水別墅合同范例
- 代理退保委托合同范本
- 信用貸購車合同范例
- 關(guān)于承包保潔合同范本
- 《物資采購合同范本》
- 養(yǎng)殖回購合同范本
- 買裝修制式合同范本
- 巴馬格紡絲控制系統(tǒng)軟件說明書(共46頁)
- 肺結(jié)核患者管理ppt課件
- 煤矸石綜合利用項目可行性研究報告寫作范文
- 清華大學(xué)MBA課程——運(yùn)籌學(xué)
- 《計量經(jīng)濟(jì)學(xué)》超全題庫及答案(完整版)
- 濕法冶金浸出凈化和沉積PPT課件
- 生產(chǎn)現(xiàn)場作業(yè)十不干PPT課件
- 雨污水管網(wǎng)勞務(wù)施工分包合同
- 通信桿路工程施工
- 初中物理光學(xué)經(jīng)典題(共23頁)
- 封條VVTT檢查流程程序參考模板
評論
0/150
提交評論