C语言斗地主随机发牌系统

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 54

void swapped(char *a, char *b)
{
    char k;
    k  = *a;
    *a = *b;
    *b = k;
}

void bubble(char a[ ], char n)
{
    for(int i = 0; i < n; i++)
        if(a[i] > a[i + 1]){
            swapped(&a[i],&a[i + 1]);
        }
}

void bubble_sort(char a[ ],int n)
{
    while (n > 1){
        n--;
        bubble(a, n);
    }

}
//定义扑克牌数组
char pokers[54] = {
                             '3','3','3','3',//ASCII: 51 //3-9对应他们各自的牌
                             '4','4','4','4',
                             '5','5','5','5',
                             '6','6','6','6',
                             '7','7','7','7',
                             '8','8','8','8',
                             '9','9','9','9',//ASCII: 57
                             ':',':',':',':', //ASCII: 58 //对应牌:"10"
                             'J','J','J','J', //ASCII:74 //对应牌:"J"
                             'K','K','K','K',//ASCII:75 //对应牌:"Q"
                             'L','L','L','L',//ASCII:76 //对应牌:"K"
                             'M','M','M','M',//ASCII:77 //对应牌:"A"
                             'N','N','N','N',//ASCII:78 //对应牌:"2"
                             'w', //ASCII:119 //对应牌:"W"(大王)
                             'W' //ASCII:87 //对应牌:"w"(小王)
                             };

//设定随机函数
//功能: 随机输出一个min~max(包括min,max)之间的数
int random_value(int min,int max)
{
    return (rand()%(max+1-min) + min);
}

//展示玩家手牌
//思路: 不打印"Z"
//用ASCII代码表示大小
void show_pokers(char arr[])
{
    for(int i = 0; i < 20; i++){
        if( arr[i] != '~'){
            if( arr[i] == ':')
                printf("10");
            else if( arr[i] == 'K')
                printf("Q");
            else if( arr[i] == 'L')
                printf("K");
            else if( arr[i] == 'M')
                printf("A");
            else if( arr[i] == 'N')
                printf("2");
            else if( arr[i] == 'W')
                printf("w");
            else if( arr[i] == 'w')
                printf("W");
            else
                printf("%c", arr[i]);
        }

        if( (i == 16 && arr[17] == '~') || i == 19)
            break;
        else
            printf(" ");
    }
    printf("\n");
}

int main()
{
    srand((unsigned)time(NULL));

    int rand_storage[54];

    int temp,i,j;
    int temp_2 = 1;
    rand_storage[0] = -1;
    for(i = 0; i <SIZE; i++){
        temp = random_value(0,53);
        for(j = 0; j < i ; j++){
            if(rand_storage[j] == temp){
                temp_2 = 0;
                break;
            }
        }
        if(temp_2){
            rand_storage[i] = temp;
        }
        else{
            i--;
        }
        temp_2 = 1;
    }

/*
    for(i = 0; i < size;i++){
        printf("%d ", rand_storage[i]);
    }
*/

    char player_1[20];
    char player_2[20];
    char player_3[20];

    for(i = 0; i < 17; i++){
        player_1[i] = pokers[ rand_storage[i] ];
    }

    for(i = 17; i < 34; i++){
        player_2[i-17] = pokers[ rand_storage[i] ];
    }

    for(i = 34; i < 51; i++){
        player_3[i-34] = pokers[ rand_storage[i] ];
    }

    for(i = 17; i < 20; i++){
        player_1[i] = '~' ;
        player_2[i] = '~' ;
        player_3[i] = '~' ;
    }

    printf("底牌:");
    for(i = 51; i < 54; i++){
        if( pokers[ rand_storage[i] ] != '~'){
            if( pokers[ rand_storage[i] ] == ':')
                printf("10");
            else if( pokers[ rand_storage[i] ] == 'K')
                printf("Q");
            else if( pokers[ rand_storage[i] ] == 'L')
                printf("K");
            else if( pokers[ rand_storage[i] ] == 'M')
                printf("A");
            else if( pokers[ rand_storage[i] ] == 'N')
                printf("2");
            else if( pokers[ rand_storage[i] ] == 'w')
                printf("W");
            else if( pokers[ rand_storage[i] ] == 'W')
                printf("w");
            else
                printf("%c", pokers[ rand_storage[i] ]);
        }
        printf(" ");
    }

    printf("\n");

    bubble_sort(player_1,17);
    bubble_sort(player_2,17);
    bubble_sort(player_3,17);

    printf("player_1: ");
    show_pokers(player_1);
    printf("player_2: ");
    show_pokers(player_2);
    printf("player_3: ");
    show_pokers(player_3);

    printf("\n");

    int n;
    printf("输入地主玩家号: " );
    scanf("%d", &n);
    if( n == 1){
        for(i = 51; i < 54; i++){
            player_1[i-34] = pokers[ rand_storage[i] ];
        }
    }
    else if(n == 2){
        for(i = 51; i < 54; i++){
            player_2[i-34] = pokers[ rand_storage[i] ];
        }
    }
    else if(n == 3){
        for(i = 51; i < 54; i++){
            player_3[i-34] = pokers[ rand_storage[i] ];
        }
    }

    bubble_sort(player_1,20);
    bubble_sort(player_2,20);
    bubble_sort(player_3,20);


    printf("player_1: ");
    show_pokers(player_1);
    printf("player_2: ");
    show_pokers(player_2);
    printf("player_3: ");
    show_pokers(player_3);

    return 0;
}
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值