用c++写一个简单的钓鱼(集卡)程序

用c++写一个简单的钓鱼(集卡)程序

因为我们C++的老师要求我们写一个面向对象的C++程序,要求是扑克牌游戏:
1、分花色,牌值表示能显示一副完整的牌
2、洗牌,分牌
3、出牌(游戏规则自己定,可以是争上游,斗地主或抽乌龟等等)
但是因为本人只会玩钓鱼和斗地主,加上技术不佳,所以就只写了这个钓鱼游戏,规则十分简单,这个游戏还是我在小学一二年纪玩的,哈哈。下面就来分享一下。
这个钓鱼游戏的玩法是这样的:我们取一副扑克牌,去掉大小王,只留下A~K的部分,也就是52张普通牌,并且玩的时候只需要关注牌值,花色不做设计。游戏支持两个人玩,首先我们将52张牌随机打乱,然后两个人依次抽牌按顺序排好,当出现相同牌的时候,将两张相同牌之间的牌都收入该次出牌玩家手中。这样知道最后将牌堆的牌抽空的时候,统计谁收集的牌多,牌多的玩家获胜。
源代码:

#include <iostream>
using namespace std;
#include<stdlib.h>
#include<time.h>
#include<stdio.h>
char initial[52] = {'A','A','A','A','2','2','2','2','3','3','3','3',
'4','4','4','4','5','5','5','5','6','6','6','6','7','7','7','7','8',
'8','8','8','9','9','9','9','1','1','1','1','J','J','J','J','Q',
'Q','Q','Q','K','K','K','K' };             //这里用“1”代替10,因为char类型的ASCALL码表里没有10,
                                           //如想要表示10,可定义一个字符指针数组,每一个牌面定为字符串便可
class Pile          //牌堆类:包含牌堆的初始化和洗牌
{
public:
    friend class StartGame;
    Pile(char initial[52]);         //洗牌构造函数
private:
    char cards[52];         //牌数组
};
Pile::Pile(char initial[52])
{
    char cardstemp[52] = { '\0'};
    char check;
    for (int i = 0; i < 52; i++)
    {
        cards[i] = initial[i];
    }
    cout << "pile initial..." << endl;
    for (int i = 0; i < 52; i++)
    {
        cout << cards[i] << " ";
    }
    cout<<endl;
    srand((unsigned int)time(NULL));
    for (int i = 0; i < 52; i++)
    {
        int postn;
        postn= rand() % 52;
        while (cardstemp[postn] != 0)
        {
            postn++;
            if(postn>=52)
            {
                postn = 0;
            }
        }
        cardstemp[postn] = cards[i];
    }
    for(int i=0;i<52;i++)
    {
        cards[i] = cardstemp[i];
    }
    cout << "是否查看当前牌堆:按C查看,按N跳过" << endl;       //提供确认洗牌结果的接口
    cin>>check;
    if (check=='c'||check=='C')
    {
        for(int i=0;i<52;i++)
        {
            cout<<cards[i]<<"  ";
        }
    }
    cout<<endl;
}

class StartGame         //游戏运行类;包含了游戏终止判断和游戏运行
{
public:
    void GameCircal(StartGame &s);     //游戏终止判断
    int GameRun();                     //游戏单次运行函数
};
int StartGame::GameRun()
{
    char checkcontinue;         //输入继续游戏检验位
    int count=0 ,earncards=0, playerorder=0;        //单次游戏结束判断位,玩家卡牌计数位,玩家顺序确定位
    int postn=0,pilepostn=0,playerpostn=0,p1postn=0,p2postn=0;      //出牌堆定位,牌堆定位,玩家定位,玩家存牌定位
    char *p1,*p2;                       //先后手
    char player1[20],player2[20];       //玩家信息记录
    char cardsshow[16]={'\0'};          //出牌数组
    char p1cards[52]={'\0'},p2cards[52]={'\0'};     //存牌数组
    Pile pile(initial);                             //单次洗牌
    cout<<"please enter 1P's name"<<endl;       //信息录入,确定先后手
    cin>>player1;
    cout<<"please enter 2P's name"<<endl;
    cin>>player2;
    srand(time(NULL));
    playerorder=rand()%2;
    if(playerorder==0)
    {
        p1=player1;
        p2=player2;
        cout<<player1<<"  先手"<<endl;
    }
    else
    {
        p1=player2;
        p2=player1;
        cout<<player2<<"  先手"<<endl;
    }
    while(count<52)     //单次运行
    {
         int temppostn=0,flag=0;        //重牌定位,重牌标志位
         if(playerpostn%2==0)           //抽牌
         {
             cout<<endl<<p1<<"  抽牌:按Enter确认"<<endl;
             fflush(stdin);
             getchar();
         }
         else
         {
             cout<<endl<<p2<<"  抽牌:按Enter确认"<<endl;
             fflush(stdin);
             getchar();
         }
         cardsshow[postn]=pile.cards[pilepostn];
         cout<<"当前牌序:"<<endl;
         for(int i=0;i<=postn;i++)          //显示出牌堆
         {
             cout<<cardsshow[i]<<"  ";
         }
         cout<<endl;
         for(int i=0;i<postn;i++)           //判断有无重牌
         {
             if(cardsshow[i]==cardsshow[postn])
             {
                 temppostn=i;
                 flag=1;
                 cout<<endl<<"^-^ 你钓到"<<cardsshow[i]<<"啦 ^-^ "<<endl;
                 break;
             }
         }
         if(flag==1&&playerpostn%2==0)      //重牌存入玩家牌堆
         {
             int temp=temppostn;
             for(int i=0;i<(postn-temppostn+1);i++)
             {
                 p1cards[p1postn]=cardsshow[temp];
                 temp++;
                 p1postn++;
             }
         }
         if(flag==1&&playerpostn%2!=0)
         {
             int temp=temppostn;
             for(int i=0;i<(postn-temppostn+1);i++)
             {
                 p2cards[p2postn]=cardsshow[temp];
                 temp++;
                 p2postn++;
             }
         }
         if(flag==1)            //重新定位出牌堆活动位
         {
             postn=temppostn;
         }
         if(flag==0)
         {
             postn++;
         }
         pilepostn++;
         playerpostn++;
         count++;
    }
    cout<<endl<<"游戏结束  "<<"玩家挣取牌数:"<<endl;      //单轮结算
    cout<<p1<<":"<<endl;
    for(int i=0;i<52;i++)
    {
         if(p1cards[i]>0)
         {
             cout<<p1cards[i]<<"  ";
             earncards++;
         }
    }
    cout<<endl<<"赢牌数:"<<earncards<<endl;
    cout<<p2<<":"<<endl;
    earncards=0;
    for(int i=0;i<52;i++)
    {
         if(p2cards[i]>0)
         {
             cout<<p2cards[i]<<"  ";
             earncards++;
         }
    }
    cout<<endl<<"赢牌数:"<<earncards<<endl;
    cout<<"是否继续游戏:按1继续,按2结束"<<endl;
    do{
    cin>>checkcontinue;
    if(checkcontinue=='1')              //继续游戏判断,返回值给GameCircal
    {
        return 1;
    }
    if(checkcontinue=='2')
    {
        return 0;
    }
    }while((checkcontinue!='1')&&(checkcontinue!='2'));
}
void StartGame::GameCircal(StartGame &s)        //游戏继续判断
{
    int i=0;
    do
    {
        i=s.GameRun();
    }while(i);
}
int main()
{
    StartGame startgame;
    startgame.GameCircal(startgame);
    cout<<endl<<"感谢参与"<<endl;
    return 0;
}

以上源程序可在code-blocks里正常运行,他只是实现了一部分的老师要求;关于牌值花色的设计见网上资料:https://wenku.baidu.com/view/485a3d1114791711cc79174a.html
还有分牌功能就由聪明的你自行设计了。
后面如果会写斗地主的话,我会将这些功能都加上,感谢阅读 ^ - ^

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值