本文主要是介绍C语言发牌程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
模拟扑克牌的发牌程序,要求52张牌,随机的发到4人手上
因为是makefile这节课的作业所以我写成了几个文件~,一样的,注释还是有乱码
- //poker.h
- #include<stdio.h>
- #include<time.h>
- #include<stdlib.h>
- #define NUM 13
- #define COLOR 4
- #define TIMES 1000/*洗牌的次数*/
- #define PLAYER 4
- #define PNUM ((COLOR*NUM)/PLAYER)
- void shuffle(int poker[][NUM]); //洗牌
- void licensing(int poker[][NUM], int player[][PNUM]); //发牌
- //shuffle.c
- #include "poker.h"
- void swap(int *a, int *b)
- {
- int temp;
- temp = *a;
- *a = *b;
- *b = temp;
- }
- void shuffle(int poker[][NUM])
- {
- int rand_i, rand_ic, rand_jc, rand_j, i;
- srand(time(NULL));
- for(i = 0; i < TIMES; i++)
- {
- rand_i = rand() % NUM;
- rand_ic = rand() % NUM;
- rand_j = rand() % COLOR;
- rand_jc = rand() % COLOR;
- swap(&poker[rand_j][rand_i], &poker[rand_jc][rand_ic]);
- }
- }
- //licensing.c
- #include"poker.h"
- void licensing(int poker[][NUM], int player[][PNUM])
- {
- int i, j;
- for(i = 0; i < COLOR; i++)
- {
- for(j = 0; j < NUM; j++)
- {
- player[i][j] = poker[i][j];
- }
- }
- }
- //main.c
- #include"poker.h"
- void itos(int i)
- {
- int num = i%100;
- switch(i/100)
- {
- case 0:
- printf("♠"); break;
- case 1:
- printf("♥"); break;
- case 2:
- printf("♦"); break;
- case 3:
- printf("♣"); break;
- default:
- printf("/nERROR!");
- exit(-1);
- }
- if(num >=1 && num <=9 ) printf("%d ", num+1);
- else if(num == 0) printf("A ");
- else if(num == 10) printf("J ");
- else if(num == 11) printf("Q ");
- else printf("K ");
- }
- int main(void)
- {
- int poker[COLOR][NUM], player[PLAYER][PNUM];
- int i, j;
- for(i = 0; i < COLOR; i++)
- for(j = 0; j < NUM; j++)
- {
- poker[i][j] = i * 100 + j;
- /*牌的å˜å‚¨æ–¹å¼?为百ä½?花色,个ä½?å’Œå??ä½?为大å°?*/
- }
- shuffle(poker);
- licensing(poker, player);
- for(i = 0; i < PLAYER; i++)
- {
- printf("THE PLAYER %d's poker is:/n", i+1);
- for(j = 0; j < PNUM; j++)
- {
- itos(player[i][j]);
- }
- printf("/n");
- }
- return 0;
- }
这篇关于C语言发牌程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!