现在的位置: 首页 > 综合 > 正文

C语言发牌程序

2013年12月11日 ⁄ 综合 ⁄ 共 2298字 ⁄ 字号 评论关闭

模拟扑克牌的发牌程序,要求52张牌,随机的发到4人手上

因为是makefile这节课的作业所以我写成了几个文件~,一样的,注释还是有乱码

Code:
  1. //poker.h   
  2. #include<stdio.h>   
  3. #include<time.h>   
  4. #include<stdlib.h>   
  5. #define NUM 13   
  6. #define COLOR 4   
  7. #define TIMES 1000/*洗牌的次数*/   
  8. #define PLAYER 4   
  9. #define PNUM ((COLOR*NUM)/PLAYER)   
  10.   
  11. void shuffle(int poker[][NUM]);  //洗牌 
  12. void licensing(int poker[][NUM], int player[][PNUM]); //发牌  
Code:
  1. //shuffle.c   
  2. #include "poker.h"   
  3.   
  4. void swap(int *a, int *b)   
  5. {   
  6.     int temp;   
  7.   
  8.     temp = *a;   
  9.     *a = *b;   
  10.     *b = temp;   
  11. }   
  12.   
  13. void shuffle(int poker[][NUM])   
  14. {   
  15.     int rand_i, rand_ic, rand_jc,  rand_j, i;   
  16.     srand(time(NULL));   
  17.   
  18.     for(i = 0; i < TIMES; i++)   
  19.     {   
  20.         rand_i = rand() % NUM;   
  21.         rand_ic = rand() % NUM;   
  22.         rand_j = rand() % COLOR;   
  23.         rand_jc = rand() % COLOR;   
  24.   
  25.         swap(&poker[rand_j][rand_i], &poker[rand_jc][rand_ic]);   
  26.   
  27.     }   
  28. }   
Code:
  1. //licensing.c
  2. #include"poker.h"   
  3.   
  4. void licensing(int poker[][NUM], int player[][PNUM])   
  5. {   
  6.     int i, j;   
  7.   
  8.     for(i = 0; i < COLOR; i++)   
  9.     {   
  10.         for(j = 0; j < NUM; j++)   
  11.         {   
  12.             player[i][j] = poker[i][j];   
  13.         }   
  14.     }   
  15. }   
Code:
  1. //main.c   
  2. #include"poker.h"   
  3.   
  4. void itos(int i)   
  5. {   
  6.     int num = i%100;   
  7.     switch(i/100)   
  8.     {   
  9.         case 0:   
  10.             printf("♠"); break;   
  11.         case 1:   
  12.             printf("♥"); break;   
  13.         case 2:   
  14.             printf("♦"); break;   
  15.         case 3:   
  16.             printf("♣"); break;   
  17.         default:   
  18.             printf("/nERROR!");   
  19.             exit(-1);   
  20.     }   
  21.   
  22.     if(num >=1 && num <=9 ) printf("%d  ", num+1);   
  23.     else if(num == 0) printf("A  ");   
  24.     else if(num == 10) printf("J  ");   
  25.     else if(num == 11) printf("Q  ");   
  26.     else printf("K  ");   
  27. }   
  28.   
  29. int main(void)   
  30. {   
  31.     int poker[COLOR][NUM], player[PLAYER][PNUM];   
  32.     int i, j;   
  33.        
  34.     for(i = 0; i < COLOR; i++)   
  35.         for(j = 0; j < NUM; j++)   
  36.         {   
  37.             poker[i][j] = i * 100 + j;   
  38.             /*牌的存储方å¼?为百ä½?花色,个ä½?å’Œå??ä½?为大å°?*/  
  39.         }   
  40.   
  41.     shuffle(poker);   
  42.     licensing(poker, player);   
  43.   
  44.     for(i = 0; i < PLAYER; i++)   
  45.     {   
  46.         printf("THE PLAYER %d's poker is:/n", i+1);   
  47.   
  48.         for(j = 0; j < PNUM; j++)   
  49.         {   
  50.             itos(player[i][j]);   
  51.         }   
  52.   
  53.         printf("/n");   
  54.     }   
  55.   
  56.     return 0;   
  57. }   

 

抱歉!评论已关闭.