c语言编程做个自动发牌程序 具体题目如图
展开全部
#include "stdio.h"
#include "time.h"
#define poke_NUM (52)
int poke[poke_NUM];
//黑 A- K红 A - K 梅 A-K 方 A-K
#define TYPE(p) (p / 13)
#define VALUE(p) (p % 13)
#define SWAP(a, b) {int tmp = a; a = b; b = tmp;}
void init_poke() {
int i;
for (i = 0; i < poke_NUM; i++)
poke[i] = i;
}
void sort_poke(int* d, int n) {
int i, j;
for (i = 0; i < n-1; i++) {
int typeA = TYPE(d[i]);
int valueA = VALUE(d[i]);
for (j = i; j < n; j++) {
int typeB = TYPE(d[j]);
int valueB = VALUE(d[j]);
if (typeA > typeB ||
(typeA == typeB && valueA > valueB)) {
SWAP(d[i], d[j]);
typeA = typeB;
valueA = valueB;
}
}
}
}
void rand_poke() {
int i;
srand(time(NULL));
i = poke_NUM;
while (i-- > 0) {
int a, b;
a = rand() % poke_NUM;
do {
b = rand() % poke_NUM;
} while (b == a);
SWAP(poke[a], poke[b]);
}
for (i = 0; i < 4; i++) {
sort_poke(&poke[i*13], 13);
}
}
void print_poke() {
int i;
char name[4][2] = {"A", "B", "C", "D"};
char typename[4][4] = {"黑", "红", "梅", "方"};
char valuename[5][4] = {"A", "10", "J", "Q", "K"};
int lasttype = -1;
for (i = 0; i < poke_NUM; i++) {
int type = TYPE(poke[i]);
int value = VALUE(poke[i]);
if (i % 13 == 0) {
printf("\nPlayer %s:", name[i/13]);
}
if (type != lasttype) {
printf("\n %s - ", typename[type]);
lasttype = type;
}
if (value == 0) printf("%s ", valuename[0]);
else if (value <= 8) printf("%c ", '1' + value);
else printf("%s ", valuename[value - 8]);
}
}
int main() {
init_poke();
rand_poke();
print_poke();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询