首页 > 编程语言 > C语言实现随机发牌
2020
09-27

C语言实现随机发牌

本文实例为大家分享了C语言实现随机发牌的具体代码,供大家参考,具体内容如下

#include "stdafx.h"
#include<time.h>
#include<stdlib.h>

int card[54];//保存每张牌的数字符号
bool flag[54];//标记数组 false 代表这个单元编号的牌没抽过
void show1();
void mix1();
int main()
{
 srand(time(NULL));
 mix1();
 show1();
 int a;
 scanf_s("%d", &a);
 return 0;
}
void mix1() {
 int c = 0;
 for (int i = 0; i < 54; i++) {
  c++;
  int a = rand() % 54;
  if (flag[a] == false) {
   card[i] = a;
   flag[a] = true;
  }
  else
  {
   i--;
  }
 }
 printf("c=%d\n", c);
}
void show1() {
 char flowers[4][10] = { "\3","\4","\5","\6" };
 char number[13][3] = { "A","2","3","4","5","6","7","8","9","10","J","Q","K" };
 for (int i = 0; i < 54; i++) {
  if (card[i] == 52) {
   printf("大王 ");
  }
  else if (card[i] == 53) {
   printf("小王 ");
  }
  else {
   printf("%s %s ", flowers[card[i] / 13], number[card[i] % 13]);
  }
  if ((i + 1) % 17 == 0)
  {
   printf("\n");
  }
 }
}

小编再为大家分享一段:C语言扑克牌生成程序

// poker.c
// day05
//
// 一个扑克牌生成程序:运行如下:
// 请输入你要的张数:5
// 程序输出:H5、H6、H7、H8、H9(Spade(黑桃)、Club(梅花)、Heart(红桃)、Diamond(方块))
// 建议:char suit[4] = {'S','D','C','H'}
// char rank[13] = {'2','3','4','5','6','7','8','9','T','J','Q','K','A'};
// Created by apple on 13-6-6.
// Copyright (c) 2013年 apple. All rights reserved.
//
 
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
 char suit[4] = {'S','C','H','D'};
 char rank[13] = {'2','3','4','5','6','7','8','9','T','J','Q','K','A'};
 int num = 0,a = 0,b = 0;
 int temp[4][13] = {0};
 srand((unsigned)time(0));
 
 printf("请输入您要的张数:");
 scanf("%d",&num);
 
 a = rand() % 4;
 b = rand() % 13;
 do{
  if(temp[a][b]==1){
   a = rand() % 4;
   b = rand() % 13;
  }else{
   printf("%c%c\t",suit[a],rank[b]);
   temp[a][b] = 1;
   num--;
  }
 }while(num);
 
 
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自学编程网。

编程技巧