遊戲系列~21點撲克牌(1)
#include<ctime>
//#include<stdio>
#include<iostream>
//#include<stdlib>
#include<conio.h>
#include <bits/stdc++.h>
using namespace std;
//定義撲克類
class game
{
private:
int wins,lose,draw,money;
int bet,playerturns,cputurns;
float player,cpu,card;//此處CARD在和BET交換數據的時候會有警告,可多定義一個INT型避免。
char flag;
public:
game();
void results(float player,float cpu,int bet,int &money,int &draw,int &wins,int &lose);
void wait(int milli);
void sign();
void special(float &player,float &cpu);
void rules();
void pause();
float random(long hign,long low);
void print(int wins,int lose,int draw,int money);
void replay(char &flag);
void hit(float &total);
void results(float player,int cpu,int bet,int &money,int &draw,int &win,int &lose);
void BET(int &bet,int &money);
void deal(float &player,float &cpu,int &playerturns,int &cputurns);
void deal2(float &player,float &cpu,int &playerturns,int &cputurns);
void ask();
};
//---------------------------------------------------------------------
//完成虛構函數
game::game()
{
wins=0;
lose=0;
draw=0;
money=0;
player=0.0;
cpu=0.0;
bet=0;
playerturns=0;
cputurns=0;
card=0;
flag=‘a‘;
}
//---------------------------------------------------------------------
void game::rules()
{
cout<<"\t*********************************************************"<<endl;
cout<<"\t*\t\t歡迎玩這個撲克21點遊戲\t\t\t*"<<endl;
cout<<"\t*\t\t\t規則如下\t\t\t*"<<endl;
cout<<"\t*\t\t一:你不能超過5張牌.\t\t\t*"<<endl;
cout<<"\t*\t\t二:可以追加賭註.\t\t\t*"<<endl;
cout<<"\t*\t\t三:同意按y.\t\t\t\t\*"<<endl;
cout<<"\t*********************************************************"<<endl;
cout<<"\t\t\t";
pause();//此處暫停幾秒
ask();
}
//---------------------------------------------------------------------
void game::ask()
{
cout<<endl<<endl;
cout<<"\t\t\t你想玩這個遊戲麽?"<<endl;
cin>>flag;
if((flag==‘y‘)||(flag==‘Y‘)) //判斷是否進入遊戲
{
cout<<"\t\t\t你將有100元開始這個遊戲"<<endl;
money=100;//設置最初賭註
cout<<"\t\t\t";
pause();
}
else exit(1);
BET(bet,money);
deal(player,cpu,playerturns,cputurns);//開始處理
}
//---------------------------------------------------------------------
void game::BET(int &bet,int &money)
{
int sign;
system("cls");
if(money<=0)
{
cout<<"\t\t\t你已經沒有賭註了,請關閉後重新開始"<<endl;
exit(1);
}
cout<<"\t\t\t你現在有:"<<money<<"$賭註"<<endl;
cout<<"\t\t\t你想下註多少:";
do{ //判斷輸入賭註是否正確
sign=0;
cin>>bet;
if((bet<0)&&(bet*-1<=money))
{
bet=bet*-1;
break;
}
else if((bet>0)&&(bet<=money))
break;
else
{
cout<<"輸入錯誤,請重新輸入:";
sign=1;
}
}while(sign=1);
money=money-bet;
}
//---------------------------------------------------------------------
void game::deal(float &player,float &cpu,int &playerturns,int &cputurns)
{
float playercard1,playercard2,cpucard1,cpucard2;
playerturns=2;
cputurns=2;
playercard1=random(13,1); //開始利用隨機數分牌
cout<<"\n\t\t\t正在處理中"<<endl;
wait(350);
playercard2=random(13,1);
wait(150);
cpucard1=random(13,1);
wait(350);
cpucard2=random(13,1);
if(playercard1>=10)
{
playercard1=0.5;
}
if(playercard2>=10)
{
playercard2=0.5;
}
if(cpucard1>=10)
{
cpucard1=0.5;
}
if(cpucard2>=10)
{
cpucard2=0.5;
}
player=playercard1+playercard2;
cpu=cpucard1+cpucard2;
cout<<"\t\t\t你的點數為:"<<player<<endl<<endl;
cout<<"你的兩張牌是:";
cout<<"["<<playercard1<<"]";
cout<<"["<<playercard2<<"]";
cout<<endl;
cout<<"\t\t\t電腦有一張"<<cpucard1<<"顯示"<<endl;
cout<<endl;
cout<<"電腦的牌是:";
cout<<"[*]"<<"["<<cpucard1<<"]"<<endl;
deal2(player,cpu,playerturns,cputurns);//調用發牌函數
}
//---------------------------------------------------------------------
void game::deal2(float &player,float &cpu,int &playerturns,int &cputurns)
{
do{
cout<<"\t\t\t你想繼續要牌麽?";
cin>>flag;
if(((flag==‘o‘)||(flag==‘O‘))&&(playerturns<5)) //判斷是否高級玩家
{
do
{
card=random(13,1);
if(card>10)
{
card=0.5;
}
cout<<"\t\t\t下張牌是:"<<card<<endl; //顯示下張牌
cout<<"\t\t\t你想要這張牌麽?(輸入p繼續要牌)"<<endl;
cin>>flag;
if((flag==‘p‘)||(flag==‘P‘))
{
playerturns++;
player=player+card;
cout<<"\t\t\t你現在的點數是:"<<player<<endl;
}
if(playerturns>=5)
{
cout<<"\t\t\t\n你的牌已經到了五張,不能再要牌了"<<endl;break;
}
}while(flag==‘p‘);
}
else if((flag==‘y‘)||(flag==‘Y‘)) //此處設置普通玩家
{
playerturns++;
if(player>21)
{
cout<<"\t\t\t你暴莊了,你輸了";
lose++;
replay(flag);
}
if(playerturns>5)
{
cout<<"\t\t\t你的牌已經到了五張,不能再要牌了"<<endl;break;
}
else if((playerturns<6)&&((flag==‘y‘)||(flag==‘Y‘)))
{
cout<<endl;
hit(player);
}
}
}while((flag==‘y‘)||(flag==‘Y‘));
cout<<"\t\t\t你想追加賭註麽?"<<endl; //問是否需要追加賭註
cin>>flag;
if((flag==‘y‘)||(flag==‘Y‘))
{
cout<<"\t\t\t請輸入你想追加的賭註:";
do{
cin>>card;
if(money==0) //判斷是否能追加賭註
{
cout<<"\t\t\t你已經沒有賭註了,電腦開始要牌。"<<endl;
break;
}
if((card)>money)
{
cout<<"\t\t\t你的賭註不夠。"<<endl;
cout<<"\t\t\t請輸入0到"<<money<<"之間的數字:"<<endl;
}
}while((card)>money);
bet=bet+card;
money=money-card;
}
for(;(cpu<16)&&(cputurns<5);cputurns++) //給電腦發牌
{
cout<<endl;
cout<<"\t\t\t電腦要了一張牌"<<endl;
hit(cpu);
if(cpu>21)
{
cpu=cpu-card;
break;
}
}
cout<<endl;
cout<<endl;
cout<<"\t\t\t電腦的牌點數為:"<<cpu<<endl;
cout<<"\t\t\t你的點數為:"<<player<<endl;
cout<<endl;
if(((player<=21)&&(cpu<=21))||(playerturns==6)||(cputurns==5)) //特殊判斷
{
special(player,cpu);
}
results(player,cpu,bet,money,draw,wins,lose);
replay(flag);
print(wins,lose,draw,money);
cout<<endl;
}
//---------------------------------------------------------------------
void game::hit(float &total) //分牌函數
{
float card;
card=random(13,1);
if(card>=10)
{
card=0.5;
}
total=total+card;
cout<<"\t\t\t要的牌是:"<<card<<endl;
cout<<"\t\t\t總共的點數是:"<<total<<endl;
}
//---------------------------------------------------------------------
void game::pause()
{
cout<<"按任意鍵繼續"<<endl;
getch();
}
//---------------------------------------------------------------------
void game::wait(int milli)
{
clock_t start; //調用TIME.H內函數
start=clock();
while((clock()-start)<milli)
;
}
//---------------------------------------------------------------------
float game::random(long high,long low) //得到隨機數
{
float ran;
srand((unsigned)time(NULL));
ran= rand()%(high-(low-1))+low;
return(ran);
}
//---------------------------------------------------------------------
void game::replay(char &flag)
{
cout<<"\n\t\t\t你想繼續玩這個遊戲麽:";
cin>>flag;
while((flag==‘y‘)||(flag==‘Y‘)) //此處設置遊戲循環
{
BET(bet,money);
deal(player,cpu,playerturns,cputurns);
}
print(wins,lose,draw,money);
}
//---------------------------------------------------------------------
void game::print(int wins,int lose,int draw,int money) //輸出結果
{
cout<<"\t\t\t贏:"<<wins<<endl;
cout<<"\t\t\t輸:"<<lose<<endl;
cout<<"\t\t\t和:"<<draw<<endl;
cout<<"\t\t\t剩余錢數:"<<money<<endl;
sign();
}
//---------------------------------------------------------------------
void game::results(float player,float cpu,int bet,int &money,int &draw,int &wins,int &lose)
{
if(cpu==player) //輸出結果
{
cout<<"\t\t\t點數相同,莊家贏"<<endl;
draw++;
}
if(player>21)
{
cout<<"\t\t\t你暴莊了,加油"<<endl;
lose++;
}
else
{
if(cpu<player)
{
cout<<"\n\t\t\t恭喜,你贏了";
money=money+(bet*2);
wins++;
}
}
if(cpu>21)
{
cout<<"\t\t\t莊家加油"<<endl;
if(player<21)
{
cout<<"\n\t\t\t你贏了";
wins++;
money=money+(bet*2);
}
}
else
{
if(cpu>player)
{
cout<<"\t\t\t你輸了"<<endl;
lose++;
}
}
}
//---------------------------------------------------------------------
void game::sign()
{
cout<<"\n\n\n\n\t\t\t新手第一次寫,多多指教"<<endl;
exit(1);
}
//---------------------------------------------------------------------
void game::special(float &player,float &cpu)
/*如果要五張牌還不超過21點而對方沒到五張牌,那就算贏.如果兩個人都五張牌且都沒
有超過21點,那點數小的贏.*/
{
if((player<=21)&&(cpu<=21)&&(playerturns==6)&&(cputurns<5))
{
cout<<"\n\t\t\t恭喜,你贏了";
money=money+(bet*2);
wins++;
}
if((player<=21)&&(cpu<=21)&&(playerturns<6)&&(cputurns==5))
{
cout<<"\t\t\t你輸了"<<endl;
lose++;
}
if((player<=21)&&(cpu<=21)&&(playerturns==6)&&(cputurns==5))
{
if(player<cpu)
{
cout<<"\n\t\t\t恭喜,你贏了";
money=money+(bet*2);
wins++;
}
else
{
cout<<"\t\t\t你輸了"<<endl;
lose++;
}
}
replay(flag);
}
//---------------------------------------------------------------------
int main()
{
game deck;
deck.rules();
}
//---------------------------------------------------------------------
遊戲系列~21點撲克牌(1)