论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > C语言程序设计教程
Tag:新手,函数,指针,数据类型,对象,Turbo,入门,运算符,数组,结构,二级,,tc,游戏,试题,问答,编译,视频教程

扑克牌的发牌程序(用伪随机数实现)

文章类别:C语言程序设计 | 发表日期:2008-9-24 14:44:16

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
int *insertsort(int sort[],int k)     \*插入排序*\
{
  int i,j;
 
  for(i=k-2;i>=0;i--)
   {
     sort[k-1]=sort[i];
     j=i+1;
     while(sort[k-1]>sort[j])
       {
        sort[j-1]=sort[j];
        j++;
       }
      sort[j-1]=sort[k-1];
   }
 return sort;
}

void output(int card[],int n)         \*转换输出函数,将生成的随机数转换成扑克牌输出*\
{
 int i,j;

 textbackground(1);           
            
 for(i=0;i<n-1;i++) {
  
   switch(card[i]/26)
   {
     case 0:
        switch((card[i]+4)/2){
              case 2:
              case 3:
              case 4:
              case 5:               
              case 6:                
              case 7:                
              case 8:               
              case 9:               
              case 10:
                 printf("%c%d",3,(card[i]+4)/2);break;                             
              case 11:
                 printf("%cJ",3);break;
              case 12:
                 printf("%cQ",3);break;
              case 13:
                 printf("%cK",3);break;
              case 14:
                 printf("%cA",3);break;
        }
     case 1:
        switch((card[i]-26+4)/2){
              case 2:
              case 3:
              case 4:
              case 5:
              case 6:
              case 7:
              case 8:
              case 9:
              case 10:
                 printf("%c%d",4,(card[i]-26+4)/2);break;
              case 11:
                 printf("%cJ",4);break;
              case 12:
                 printf("%cQ",4);break;
              case 13:
                 printf("%cK",4);break;
              case 14:
                 printf("%cA",4);break;
        }       
     case 2:
        switch((card[i]-52+4)/2){
              case 2:
              case 3:
              case 4:
              case 5:
              case 6:
              case 7:
              case 8:
              case 9:
              case 10:
                 printf("%c%d",5,(card[i]-52+4)/2);break;
              case 11:
                 printf("%cJ",5);break;
              case 12:
                 printf("%cQ",5);break;
              case 13:
                 printf("%cK",5);break;
              case 14:
                 printf("%cA",5);break;
        }       
     case 3:
        switch((card[i]-78+4)/2){
              case 2:
              case 3:
              case 4:
              case 5:
              case 6:
              case 7:
              case 8:
              case 9:
              case 10:
                 printf("%c%d",6,(card[i]-78+4)/2);break;
              case 11:
                 printf("%cJ",6);break;
              case 12:
                 printf("%cQ",6);break;
              case 13:
                 printf("%cK",6);break;
              case 14:
                 printf("%cA",6);break;
        }         
     case 4:       
        if(card[i]==104||card[i]==105)
           printf("joker");
        if(card[i]==106||card[i]==107)
           printf("JOKER");
        break;
     default:
        break;
   }
  }
}
main()
{
   int i,j,flag,n;                                               \*remain数组用来放底牌*\
   int remain[9],cards1[26],cards2[26],cards3[26],cards4[26];  \*cards数组用来放发的牌,每人25张用大小为26的数组是为了方便排序*\
   int whole[100];           \*whole数组用来放出过的牌,方便电脑记忆*\

srand((unsigned)time(NULL));
for(i=0;i<25;i++)
  {      
        cards1[i]=rand()%108;
        flag=i;
       
     
        for(j=0;j<i;j++)
          {
           if(!(cards1[j]-cards1[i]))
           {
             i--;
 
             break;
           }
          }           
        if(i!=flag)            
          continue;                      
   }
 
for(i=0;i<25;i++)
      {
        cards2[i]=rand()%108;
        flag=i;
       
       for(n=0;n<25;n++)
         {
          if(!(cards1[n]-cards2[i]))
           {
              i--;
              break; 
           }
         }
        if(i!=flag)
          continue;
        for(j=0;j<i;j++)
          {
          
           if(!(cards2[j]-cards2[i]))
            {
              i--;
 
              break;
            }
          }           
       if(i!=flag)
          continue;                  
      }

for(i=0;i<25;i++)
      {
        cards3[i]=rand()%108;
        flag=i;
       
       for(n=0;n<25;n++)
         {
          if(!(cards1[n]-cards3[i])||!(cards2[n]-cards3[i]))
           {
              i--;
              break; 
           }
         }
        if(i!=flag)
          continue;
        for(j=0;j<i;j++)
          {          
           if(!(cards3[j]-cards3[i]))
            {
              i--;
 
              break;
            }
          }           
        if(i!=flag)
          continue;          
      }

for(i=0;i<25;i++)
      {
        cards4[i]=rand()%108;
        flag=i;
       
        for(n=0;n<25;n++)
         {
          if(!(cards1[n]-cards4[i])||!(cards2[n]-cards4[i])||!(cards3[n]-cards4[i]))
           {
              i--;
              break; 
           }
         }
        if(i!=flag)
          continue;
        for(j=0;j<i;j++)
          {          
           if(!(cards4[j]-cards4[i]))
            {
              i--;
 
              break;
            }
          }           
        if(i!=flag)
          continue;                
      }

for(j=0,i=0;j<25&&i<100;j++){
      whole[i]=cards1[j];
      i++;
      whole[i]=cards2[j];
      i++;
      whole[i]=cards3[j];
      i++;
      whole[i]=cards4[j];
      i++;  
   }
insertsort(whole,101);
  
for(n=0,i=0;n<8;n++,i++){  
   for(j=0;j<100;j++){
       if(i==whole[j])
       i++;                                   
   }
   remain[n]=i;                      
}

insertsort(cards1,26);
insertsort(cards2,26);
insertsort(cards3,26);
insertsort(cards4,26);
insertsort(remain,9);
printf("\nCards of four players are:\n\n");
printf("East player:\n   ");
output(cards1,26);printf("\n");
printf("South player:\n   ");
output(cards2,26);printf("\n");
printf("West player:\n   ");
output(cards3,26);printf("\n");
printf("North player:\n   ");
output(cards4,26);printf("\n\n");
printf("The left cards are:\n\n");
printf("   ");
output(remain,9);printf("\n");
getch();
}
 
   

上一篇:{实例}掷骰子 人气:5451
下一篇:{实例}柱状图表示学生成绩各分数段比率 人气:5848
视频教程列表
文章教程搜索
 
C语言程序设计推荐教程
C语言程序设计热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058