| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2956 人关注过本帖
标题:一个简单的发牌程序
只看楼主 加入收藏
莫丶
Rank: 2
来 自:济南大学
等 级:论坛游民
帖 子:48
专家分:45
注 册:2011-1-2
结帖率:100%
收藏
 问题点数:0 回复次数:7 
一个简单的发牌程序
#include "stdio.h"
#include "time.h"
#include "stdlib.h"

#define num_suits 4
#define num_ranks 13
#define true 1
#define false 0

void main()
{
    char in_hand[num_suits][num_ranks]={0};
    const char ranks_code[]={'2','3','4','5','6','7','8','9','10','j','q','k','a'};
    const char suits_code[]={'c','d','h','s'};
    int suits,ranks,num_cards;
    printf("Enter number of cards in hand:\n");
    scanf("%d",&num_cards);
    srand(time(0));
    printf("your hand:\n");
    while(num_cards>0)
    {
        if(num_cards>52)
        {
        printf("error\n");
        break;
        }
     suits=rand()%num_suits;
     ranks=rand()%num_ranks;
     if(!in_hand[suits][ranks])
     {
            in_hand[suits][ranks]=true;
        num_cards--;
        printf("%c  %c\n",suits_code[suits],ranks_code[ranks]);
    }
    }
}


搜索更多相关主题的帖子: false 
2011-04-09 10:51
ansic
Rank: 14Rank: 14Rank: 14Rank: 14
来 自:恍惚窈冥
等 级:城市猎人
帖 子:1543
专家分:5367
注 册:2011-2-15
收藏
得分:0 

善人者,不善人之师;不善人者,善人之资。不贵其师,不爱其资,虽智大迷。
2011-04-09 10:53
逍遥乖乖
Rank: 2
等 级:论坛游民
帖 子:44
专家分:56
注 册:2011-4-8
收藏
得分:0 
新手看不懂
2011-04-09 10:58
莫丶
Rank: 2
来 自:济南大学
等 级:论坛游民
帖 子:48
专家分:45
注 册:2011-1-2
收藏
得分:0 
回复 3楼 逍遥乖乖
呵呵我也是新手 琢磨了老一会才写出来的
2011-04-09 11:00
hnuhsg1226
Rank: 9Rank: 9Rank: 9
来 自:中国
等 级:蜘蛛侠
威 望:2
帖 子:314
专家分:1314
注 册:2011-3-27
收藏
得分:0 
呵呵,有想法就不错,顶个

我的地盘
2011-04-09 11:30
heimodao
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:88
专家分:172
注 册:2011-4-8
收藏
得分:0 
fabai.c:13:62: 警告:多字节字符常量
fabai.c: 在函数‘main’中:
fabai.c:13: 警告:隐式常量转换溢出
2011-04-09 12:02
νiЯūs
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-4-9
收藏
得分:0 
只能给一个人发牌?
程序代码:
//Poker.h
#ifndef __POKER_H__
#define __POKER_H__

typedef struct _poker
{
    char m;
    char f;
    char i;
}Poker,*pPoker,&HPoker;

//显示使用
static Poker NormalPoker[] =
{
    {'A','@', 0x00},
    {'A','*', 0x01},
    {'A','&', 0x02},
    {'A','#', 0x03},
    {'2','@', 0x04},
    {'2','*', 0x05},
    {'2','&', 0x06},
    {'2','#', 0x07},            

    {'3','@', 0x08},
    {'3','*', 0x09},
    {'3','&', 0x0a},
    {'3','#', 0x0b},            

    {'4','@', 0x0c},
    {'4','*', 0x0d},
    {'4','&', 0x0e},
    {'4','#', 0x0f},          

    {'5','@', 0x10},
    {'5','*', 0x11},
    {'5','&', 0x12},
    {'5','#', 0x13},     

    {'6','@', 0x14},
    {'6','*', 0x15},
    {'6','&', 0x16},
    {'6','#', 0x17},         

    {'7','@', 0x18},
    {'7','*', 0x19},
    {'7','&', 0x1a},
    {'7','#', 0x1b},      

    {'8','@', 0x1c},
    {'8','*', 0x1d},
    {'8','&', 0x1e},
    {'8','#', 0x1f},       

    {'9','@', 0x20},
    {'9','*', 0x21},
    {'9','&', 0x22},
    {'9','#', 0x23},  

    {'0','@', 0x24},
    {'0','*', 0x25},
    {'0','&', 0x26},
    {'0','#', 0x27},    

    {'J','@', 0x28},
    {'J','*', 0x29},

    {'J','&', 0x2a},

    {'J','#', 0x2b},       

    {'Q','@', 0x2c},
    {'Q','*', 0x2d},
    {'Q','&', 0x2e},
    {'Q','#', 0x2f},    

    {'K','@', 0x30},
    {'K','*', 0x31},
    {'K','&', 0x32},
    {'K','#', 0x33},     

    {'G','-', 0x34},
    {'G','+', 0x35}

};

#define AllPokerNum 54

class APoker
{
private:
     unsigned char arPoker[54];
     unsigned char currentlen;    

public:
    APoker();
    //洗牌
    void Shuffle();
    //发牌
    unsigned char GetOnePoker(); 

private:
    int Rand();
};

#endif
程序代码:
//poker.cpp
#include "Poker.h"
#include <stdlib.h>
#include <time.h>

APoker::APoker()
{
    srand(time(NULL));
    Shuffle();
}

void APoker::Shuffle()
{
    currentlen = AllPokerNum;
    for(unsigned char i= 0;i<AllPokerNum;i++)
    {
        arPoker[i] = i;
    }
}

unsigned char APoker::GetOnePoker()
{
    int r = Rand();
    unsigned char v = 0;
    if(r<0)return 100;
    v = arPoker[r];
    arPoker[r] = arPoker[--currentlen];
    return v;
}

int APoker::Rand()
{
    if(currentlen>0)
    {
        int r = rand();
        return r%currentlen;
    }
    return -1;
}
程序代码:
//main.cpp
#include <stdlib.h>
#include <stdio.h>
#include "Poker.h"

int main()
{
    APoker ap;
    for(int i=0;i<54;i++)
    {
        int a = ap.GetOnePoker();
        if(a<54)
        {
            Poker p =  NormalPoker[a];
            printf("-->value: [ %c %c ] ; index: %02d\n",p.m,p.f,p.i);
        }
    }   

    system("pause");
    return 0;
}
程序代码:
编译
g++ main.cpp Poker.cpp Poker.h
运行 a.exe

输出:
-->value: [ 8 @ ] ; index: 28
-->value: [ A * ] ; index: 01
-->value: [ 0 # ] ; index: 39
-->value: [ K * ] ; index: 49
-->value: [ 7 @ ] ; index: 24
-->value: [ 4 @ ] ; index: 12
-->value: [ 6 @ ] ; index: 20
-->value: [ 9 & ] ; index: 34
-->value: [ J @ ] ; index: 40
-->value: [ 2 * ] ; index: 05
-->value: [ 0 * ] ; index: 37
-->value: [ K # ] ; index: 51
-->value: [ J & ] ; index: 42
-->value: [ 8 # ] ; index: 31
-->value: [ 7 & ] ; index: 26
-->value: [ 2 # ] ; index: 07
-->value: [ Q * ] ; index: 45
-->value: [ 4 # ] ; index: 15
-->value: [ 2 & ] ; index: 06
-->value: [ 4 & ] ; index: 14
-->value: [ J # ] ; index: 43
-->value: [ 9 * ] ; index: 33
-->value: [ 7 * ] ; index: 25
-->value: [ 3 * ] ; index: 09
-->value: [ 4 * ] ; index: 13
-->value: [ 5 * ] ; index: 17
-->value: [ 5 & ] ; index: 18
-->value: [ 5 # ] ; index: 19
-->value: [ 3 & ] ; index: 10
-->value: [ 0 @ ] ; index: 36
-->value: [ 6 * ] ; index: 21
-->value: [ 6 & ] ; index: 22
-->value: [ 8 * ] ; index: 29
-->value: [ 9 # ] ; index: 35
-->value: [ 2 @ ] ; index: 04
-->value: [ 9 @ ] ; index: 32
-->value: [ G - ] ; index: 52
-->value: [ 6 # ] ; index: 23
-->value: [ 7 # ] ; index: 27
-->value: [ 8 & ] ; index: 30
-->value: [ K @ ] ; index: 48
-->value: [ K & ] ; index: 50
-->value: [ J * ] ; index: 41
-->value: [ Q & ] ; index: 46
-->value: [ 0 & ] ; index: 38
-->value: [ Q @ ] ; index: 44
-->value: [ A # ] ; index: 03
-->value: [ 3 # ] ; index: 11
-->value: [ Q # ] ; index: 47
-->value: [ A @ ] ; index: 00
-->value: [ G + ] ; index: 53
-->value: [ 5 @ ] ; index: 16
-->value: [ A & ] ; index: 02
-->value: [ 3 @ ] ; index: 08

2011-04-09 17:43
νiЯūs
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-4-9
收藏
得分:0 
c版。
程序代码:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>

typedef struct _poker
{
    char m;
    char f;
    char i;
}Poker,*pPoker;

//显示使用
static Poker NormalPoker[] =
{
    {'A','@', 0x00},
    {'A','*', 0x01},
    {'A','&', 0x02},
    {'A','#', 0x03},
    {'2','@', 0x04},
    {'2','*', 0x05},
    {'2','&', 0x06},
    {'2','#', 0x07},            

    {'3','@', 0x08},
    {'3','*', 0x09},
    {'3','&', 0x0a},
    {'3','#', 0x0b},            

    {'4','@', 0x0c},
    {'4','*', 0x0d},
    {'4','&', 0x0e},
    {'4','#', 0x0f},          

    {'5','@', 0x10},
    {'5','*', 0x11},
    {'5','&', 0x12},
    {'5','#', 0x13},     

    {'6','@', 0x14},
    {'6','*', 0x15},
    {'6','&', 0x16},
    {'6','#', 0x17},         

    {'7','@', 0x18},
    {'7','*', 0x19},
    {'7','&', 0x1a},
    {'7','#', 0x1b},      

    {'8','@', 0x1c},
    {'8','*', 0x1d},
    {'8','&', 0x1e},
    {'8','#', 0x1f},       

    {'9','@', 0x20},
    {'9','*', 0x21},
    {'9','&', 0x22},
    {'9','#', 0x23},  

    {'0','@', 0x24},
    {'0','*', 0x25},
    {'0','&', 0x26},
    {'0','#', 0x27},    

    {'J','@', 0x28},
    {'J','*', 0x29},

    {'J','&', 0x2a},

    {'J','#', 0x2b},       

    {'Q','@', 0x2c},
    {'Q','*', 0x2d},
    {'Q','&', 0x2e},
    {'Q','#', 0x2f},    

    {'K','@', 0x30},
    {'K','*', 0x31},
    {'K','&', 0x32},
    {'K','#', 0x33},     

    {'G','-', 0x34},
    {'G','+', 0x35}

};

#define AllPokerNum 54

typedef struct _APoker
{
    unsigned char p[AllPokerNum];
    unsigned char currentlen;
}APoker,*pAPoker;

static int Rand(pAPoker pa);

 //洗牌
void Shuffle(pAPoker pa);

 //发牌
unsigned char GetOnePoker(pAPoker pa);

int main(int argn,char * argv[])
{
    APoker ap;
    Shuffle(&ap);
    srand(time(NULL));
    int i=0;
    for(;i<54;i++)
    {
        int a = GetOnePoker(&ap);
        if(a<54)
        {
            Poker p =  NormalPoker[a];
            printf("-->value: [ %c %c ] ; index: %02d\n",p.m,p.f,p.i);
        }
    }
    system("PAUSE");
    return 0;
}

static int Rand(pAPoker pa)
{
    if(!pa||!pa->currentlen)return -1;
    int i = rand();
    return i%pa->currentlen;
}

void Shuffle(pAPoker pa)
{
    if(!pa)return;
    unsigned char i=0;
    pa->currentlen = AllPokerNum;
    for(; i<AllPokerNum; i++)pa->p[i] = i;
}

unsigned char GetOnePoker(pAPoker pa)
{
    if(!pa)return 100;
    int r = Rand(pa);
    unsigned char v = 0;
    if(r<0)return 100;
    v = pa->p[r];
    pa->p[r] = pa->p[--pa->currentlen];
    return v;
}
程序代码:
D:\temp\Test\>gcc test.c -Wall

D:\temp\Test\>a.exe
-->value: [ K # ] ; index: 51
-->value: [ 9 # ] ; index: 35
-->value: [ 5 * ] ; index: 17
-->value: [ K * ] ; index: 49
-->value: [ 9 & ] ; index: 34
-->value: [ 0 * ] ; index: 37
-->value: [ Q * ] ; index: 45
-->value: [ 3 @ ] ; index: 08
-->value: [ K & ] ; index: 50
-->value: [ 0 & ] ; index: 38
-->value: [ 8 @ ] ; index: 28
-->value: [ 8 * ] ; index: 29
-->value: [ 4 & ] ; index: 14
-->value: [ J # ] ; index: 43
-->value: [ 9 * ] ; index: 33
-->value: [ J & ] ; index: 42
-->value: [ J * ] ; index: 41
-->value: [ 7 @ ] ; index: 24
-->value: [ 7 # ] ; index: 27
-->value: [ J @ ] ; index: 40
-->value: [ 6 # ] ; index: 23
-->value: [ 5 # ] ; index: 19
-->value: [ 8 # ] ; index: 31
-->value: [ 4 * ] ; index: 13
-->value: [ 4 @ ] ; index: 12
-->value: [ Q @ ] ; index: 44
-->value: [ K @ ] ; index: 48
-->value: [ 2 * ] ; index: 05
-->value: [ 6 * ] ; index: 21
-->value: [ 0 @ ] ; index: 36
-->value: [ A # ] ; index: 03
-->value: [ G - ] ; index: 52
-->value: [ Q & ] ; index: 46
-->value: [ 2 & ] ; index: 06
-->value: [ 0 # ] ; index: 39
-->value: [ G + ] ; index: 53
-->value: [ Q # ] ; index: 47
-->value: [ 3 * ] ; index: 09
-->value: [ 7 * ] ; index: 25
-->value: [ A * ] ; index: 01
-->value: [ 7 & ] ; index: 26
-->value: [ 6 @ ] ; index: 20
-->value: [ 9 @ ] ; index: 32
-->value: [ 5 @ ] ; index: 16
-->value: [ 3 & ] ; index: 10
-->value: [ 5 & ] ; index: 18
-->value: [ A & ] ; index: 02
-->value: [ 2 # ] ; index: 07
-->value: [ 4 # ] ; index: 15
-->value: [ A @ ] ; index: 00
-->value: [ 8 & ] ; index: 30
-->value: [ 2 @ ] ; index: 04
-->value: [ 3 # ] ; index: 11
-->value: [ 6 & ] ; index: 22
Press any key to continue . . .

2011-04-09 18:10
快速回复:一个简单的发牌程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045957 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved