C++课程设计21点扑克牌游戏.docx

上传人:b****6 文档编号:5863107 上传时间:2023-01-01 格式:DOCX 页数:8 大小:18.99KB
下载 相关 举报
C++课程设计21点扑克牌游戏.docx_第1页
第1页 / 共8页
C++课程设计21点扑克牌游戏.docx_第2页
第2页 / 共8页
C++课程设计21点扑克牌游戏.docx_第3页
第3页 / 共8页
C++课程设计21点扑克牌游戏.docx_第4页
第4页 / 共8页
C++课程设计21点扑克牌游戏.docx_第5页
第5页 / 共8页
点击查看更多>>
下载资源
资源描述

C++课程设计21点扑克牌游戏.docx

《C++课程设计21点扑克牌游戏.docx》由会员分享,可在线阅读,更多相关《C++课程设计21点扑克牌游戏.docx(8页珍藏版)》请在冰豆网上搜索。

C++课程设计21点扑克牌游戏.docx

C++课程设计21点扑克牌游戏

此程序为21点扑克牌游戏,玩家一共可以要五张牌,但如果牌的点数超过21,则自动出局;在不超过21点的情况下,玩家与庄家比牌的点数大小,大者为赢,相同则为平局。

 

程序说明:

 

1)   将所有的函数与相应的数据封装到类中,并改写主程序。

使程序简化。

 

2)   程序中一共有13张扑克牌可以随时抽取,大于10的点数为0.5。

 

3)   超级玩家永远不会输掉,即超级玩家可以查看下一张牌,若大于21点,则可以拒绝,当然游戏规则上不能泄露这点秘密。

 

4)   超级玩家可以查看下一张牌,即输入指定的字符串或字符,然后按要求输入正确密码后才能看自己的和计算机的牌,并指定下一级牌的点数。

  

5)   每次要牌后可以设定赔率,即如果开始下的是10元钱的注,如果牌好,你可以要求继续下注,当然不能超过你所有的总钱数。

 

6)   将界面的提示说明改成中文界面,界面的解释详细友好,可以吸引更多的玩家。

 

类的封装 

设计一个类,将出牌,下注等的操作包含进去,玩家和计算机。

均为这个类的对象。

这样,在玩牌及判断输赢时,函数只需要用这两个对象的引用做参数即可,大大简化了函数。

 

具体的参考的数据结构如下; 

class CCard

{

private:

   int naPip[5];  //一共五张牌

   int nNumber;   //实际发了多少牌

   int nDollar;   //有多少钱

   int nGamble;   //赌注

   int nWin;      //赢局数

   int nLost;     //输局数

   int nDraw;     //平局数

public:

   CCard();     //构造函数,初始化

   void FirstPlayTwo();  // 最初两张牌

   int GetNumber();     //返回牌的张数

   double GetPip();     //返回点数

   void DidplayPip();    //依次显示牌面的点数

   void DidplayPip(int);    //除了第一张牌,依次全部显示牌面点数(针对计算机牌的显示)

   void TurnPlay();     //出一张牌

   void Win();     //赢了计算赌注

   void Lose();    //输了

   void Draw();    //平局

   int SetGamblec(int);   //设置赌注,赌本不够返回-1

   int GetMoney();   //返回钱数

   void DisplayInfo();    //显示必要的信息

   char * GetCurrentCard();    //返回当前牌点

   void DeleteCard(void);   //删除当前牌点,作弊用

    void ModifyCard(double);  //修改当前牌,作弊用

   int Addgamble(int);    //加赌注

};

主程序 

#include 

#include 

//fuctions 

void replay(char &ans);     //询问玩家是否再玩一局 

void hit(int &total);       //要一张牌 

void deal(int &player,int &cpu,int &playerturns,int &cputurns); //为计算机和玩家各发两张牌 

void BET(int &bet,int &money);  //下注 

void print(int wins,int lose,int draw,int money); //输出最后结果 

void rules(); //输出游戏规则 

void results(int player,int cpu,int bet,int &money,int &draw,int &win,int &lose); //判断一局的结果  

/*diyige*/ 

//wait.h begin  

#include  

#include  

void wait(int milli)  //暂停, milli 毫秒 

clock_t start; 

start=clock(); 

while( ( clock()-start ) < milli ); 

//wait.h end 

#include 

#include 

#include 

#include 

//========================================================= 

void pause() 

//暂停,任按一键继续 

        cout<<"任按一键继续"<

        getch(); 

//========================================================= 

int GetInt(int L=0,int H=100) 

        int ent; 

        cout<<"Please Enter A Number Between "<

        cin>>ent; 

        while((entH)) 

        { 

                cout<<"Error"<

                cout<<"Value must be between "<

                cin>>ent; 

        } 

        return(ent); 

//========================================================== 

void sign() 

//Displays ISAAC SHAFFER 

        cout<<"This Program Was Written By Isaac Shaffer"<

//============================================================= 

int random(long hi,long lo) 

//This Program Finds A Random Number Between Hi and Low 

        int ran; 

        srand((unsigned)time(NULL)); 

        ran=rand()%(hi-(lo-1))+lo; 

        return(ran); 

//utility.h end 

int main() 

    int player=0,cpu=0,win=0,lose=0,draw=0,playerturns=0,cputurns=0,money=0,bet; 

   char ans; 

   system("cls"); //执行系统命令,清屏 

   rules(); 

   cout<<"\t\t\t请问是否玩牌:

"; 

   cin>>ans; 

   if((ans == 'y')||(ans=='Y')) // 检查输入是否为Yes 

   { 

      cout<<"\t\t\t您的赌本为100美元"<

      money=100; 

      cout<<"\t\t\t"; 

      pause();  //暂停,任按一键继续 

   } 

   else 

   { 

      return(0); 

   } 

   do{ 

      system("cls"); 

      if(money<0) //看看有无赌本 

      { 

          cout<<"对不起,您没赌本了"<

         return(0); 

      } 

        BET(bet,money); //下注的程序 

        deal(player,cpu,playerturns,cputurns); //给玩家和计算机各发两张牌 

   do{    

           cout<<"\t\t\tWould You Like To Hit or Stay :

"; //询问是否要牌 

          cin>>ans; 

           if((ans=='h')||(ans=='H'))  //如果玩家要牌 

         { 

            playerturns++; //玩家手中的牌数增加一张 

            if(playerturns>5) //判断玩家手中的牌数是否超过5张 

            { 

                cout<<"\t\t\t你手中的牌不能超过5张牌";   //若超过5张不能继续要牌 

             

            } 

          

         } 

          if((playerturns<6)&&(ans=='h')) //判断是否符合玩家要牌的条件 

         { 

              cout<

         hit(player);  //调用发牌程序 

         } 

      }while((ans=='h')||(ans=='H'));  //继续询问玩家是否要牌 

   for(;(cpu<16)&&(cputurns<6);cputurns++)   //计算机开始要牌的条件 

   { 

      cout<

      cout<<"\t\t\t计算机要了一张牌"<

      hit(cpu); //调用发牌程序 

   } 

   cout<

   cout<

   cout<<"\t\t\t计算机的牌面为:

"<

   cout<<"\t\t\t您的牌面为:

"<

cout<

   results(player,cpu,bet,money,draw,win,lose); //判断输赢 

   replay(ans);     //询问是否继续玩牌 

   }while((ans=='y')||(ans=='Y')); 

   print(win,lose,draw,money);   //游戏结束,输出结果 

   cout<

   cout<<"\t\t\t\t"; 

   return(0); 

//--------------------------------------------------------------------------------------- 

void rules()     //游戏规则 

cout<<"\t\t WELCOME TO ISAAC SHAFFER'S BLACK JACK GAME"<

    cout<<"\t\t\t HERE ARE SOME SIMPLE RULES"<

   cout<<"\t\t\t1:

You Can only have a max of 5 cards."<

   cout<<"\t\t\t2:

If you bust you automatically lose."<

   cout<<"\t\t\t3:

If you win you win double what you bet."<

   cout<<"\t\t\t4:

The Dealer stops at or after 16."<

   cout<<"\t\t\t\t"; 

   pause(); 

//---------------------------------------------------------------------------------------- 

void BET(int &bet,int &money)    //接收玩家下注 

   system("cls"); 

   cout<<"\t\t\t您现有的 :

$"<

   cout<<"\t\t\t您要下的赌注是:

 "; 

   cin>>bet; 

       if(bet<0) 

        { 

        bet=bet*-1; 

        } 

   money=money-bet; 

//---------------------------------------------------------------------------------------- 

void deal(int &player,int &cpu,int &playerturns,int &cputurns) 

//为计算机和玩家各发两张牌 

   int playercard1,playercard2,cpucard1,cpucard2; 

   playerturns=2; 

   cputurns=2; 

   playercard1=random(13,1);   //取得13与1之间的随机数 

   cout<<"\n\t\t\tDEALING HAND"<

wait(350);      //等待350ms 

   playercard2=random(13,1); 

   wait(150);    

   cpucard1=random(13,1); 

    

   wait(350); 

   cpucard2=random(13,1); 

   if(playercard1>=10)    //若牌面的点数大于10,按10来计点数 

   { 

      playercard1=10; 

   } 

   if(playercard2>=10) 

   { 

      playercard2=10; 

   } 

   if(cpucard1>=10) 

   { 

      cpucard1=10; 

   } 

   if(cpucard2>=10) 

   { 

      cpucard2=10; 

   } 

player=playercard1+playercard2;  //玩家两张牌的总点数 

cpu=cpucard1+cpucard2;      //计算机两张牌的总点数 

cout<<"\t\t\t现有您的牌面总数是 :

"<

cout<<"["<

cout<<"["<

cout<

cout<<"\t\t\t计算机有一张"<

cout<

cout<<"[*] "<<" ["<

}     

//---------------------------------------------------------------------------------------- 

void hit(int &total)   //要一张牌 

//This fuction is to deal a card and add it to the total 

   int card; 

   card=random(13,1); 

   if(card>=10) 

   { 

      card=10; 

   } 

   total=total+card;   //牌点总数 

   cout<<"\t\t\t牌面是 :

"<

    cout<<"\t\t\t总的牌面是:

"<

//----------------------------------------------------------------- 

void results(int player,int cpu,int bet,int &money,int &draw,int &win,int &lose) 

//判断一局的结果  

if(cpu==player)   //平局 

   { 

   cout<<"\t\t\t平局"<

      draw++;     

   } 

   if(player>21)   //玩家超过了21点 

   { 

   cout<<"\t\t\t很遗憾,你输了"<

      lose++; 

   } 

   else 

   { 

       if(cpu

      { 

         cout<<"\n\t\t\t恭喜你,你赢了"; 

          money=money+(bet*2); 

           win++;    

      } 

   } 

   if(cpu>21)     //计算机超过了21点 

   { 

      cout<<"\t\t\t计算机输了"<

       

      if(player<21) 

      { 

         cout<<"\n\t\t\t恭喜你,你赢了"; 

         win++; 

         money=money+(bet*2); 

      } 

   } 

   else 

   { 

       if(cpu>player) 

      { 

         cout<<"\t\t\t很遗憾,你输了"<

          lose++; 

      } 

   } 

//----------------------------------------------------------------- 

void replay(char &ans)  //询问玩家是否再玩一局 

   cout<<"\n\t\t\t您是否想再玩一局:

"; 

   cin>>ans; 

//----------------------------------------------------------------- 

void print(int wins,int lose,int draw,int money) 

   cout<<"\t\t\t\t赢的次数 :

"<

   cout<<"\t\t\t\t输的次数 :

"<

   cout<<"\t\t\t\t平的次数 :

"<

   cout<<"\t\t\t\t您的赌本 :

"<

还有。

#include 

#include 

# define N x 

void main(void) 

   char str[20]; 

   char ch; 

   int i=0; 

   cout<<"输入密码:

"; 

   cout.flush(); 

   ch=getch(); 

   while(ch!

=x) 

   { 

      str[i++]=ch; 

      cout<<'*'; 

       cout.flush(); 

      ch=getch(); 

   } 

   str[i]=0; 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 工学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1