学习c时写的出牌游戏

主要学习c在DOS下如何驱动鼠标.
 #include<graphics.h>
 #include<stdio.h>
 #include<time.h>
 #include<dos.h>
 #define CARDCOLOR 5
 #define XP 500 /*显示牌的地方,可通过该值来改变*/
 #define YP 400
 #define NUMBER 17 /*没人牌的个数*/
 #define BKCOLOR 1

static int xnow=300,ynow=250,button=0;/*定义全局变量,保存鼠标信息*/
static cursoron[16][16],pattrib[16][16];/*定义全局变量,保存经过的各点信息*/
static int calbutton=0,outnumber=0;
/***************************************
定义54张牌
****************************************/
struct card
{
  int figure;
  int symbol;
  int flag[5];
}cards[54],outcards[17],player1[17],outcomputer[17];

/***************************************
初始化54张牌
****************************************/
void initcard()
{
 register int i=0;
	for(i=0;i<52;i++)
           {
             cards[i].figure=(i+1)%13;
	     cards[i].symbol=(i)/13 + 3;
             cards[i].flag[0]=0;
             cards[i].flag[1]=0;
	     cards[i].flag[2]=0;
	     }
  cards[52].figure=14;
  cards[53].figure=14;
  cards[52].symbol=1;
  cards[53].symbol=2;
}
/***************************************
洗牌,利用发生器产生随机数,做250次对换
****************************************/
void washcard()
 {
    register int i;
    struct card temp;
    int j=0,k=0;
    srand(time(NULL));
    	for(i=0;i<500;i++)
         {
           j=rand() % 54;
           k=rand() % 54;
	   temp=cards[j];
           cards[j]=cards[k];
           cards[k]=temp;
          }
 }
/*************************************
 牌的框架
 **************************************/
 void box(int xposition,int yposition,int n)
 {
 		int i=0;
			 for(i=0;i<n;i++)
 	        {
			setcolor(CARDCOLOR);
			bar3d(xposition-25*i,yposition-100,xposition-
                              25*(i+1),yposition,1,1);

		      }
	}


/***************************************
显示54张牌
****************************************/

void showcard(struct card *p,int n,int x, int y)
 {
   int i=0,m;
   char buff[3];
   m=n;
   box(x,y,m);
   	for(i=0;i<n;i++,p++)
           {

	     sprintf(buff,"%c%d",p->symbol,p->figure);
	     outtextxy(x-22-25 * i,y-90,buff);

	    sprintf(buff,"%d",p->flag[0]);
	     outtextxy(x-22-25 * i,y-80,buff);

             sprintf(buff,"%d",p->flag[1]);
	     outtextxy(x-22-25 * i,y-70,buff);

             sprintf(buff,"%d",p->flag[2]);
	     outtextxy(x-22-25 * i,y-60,buff);
           }
}
/***********************************************
对54张牌进行从小到大排列,并将没张牌的信息保存
***********************************************/
  void sortcard(struct card p[])
 {
   register int i=0,j=0;
   int flag=0;
   struct card t;
	for(i=0; i< 17;i++)

	  {   flag=0;
	       for(j=0;j<17-i-1;j++)
              {
		if(p[j].figure >= p[j+1].figure)
                  {
		      t=p[j];
                      p[j]=p[j+1];
		      p[j+1]=t;
		     flag=1;
                   }
               }
	    if(flag==0) break;
             }
       for(i=17; i<33;i++)

	  {   flag=0;
	       for(j=0;j<33-i-1;j++)
              {
		if(p[j].figure >= p[j+1].figure)
                  {
		      t=p[j];
                      p[j]=p[j+1];
		      p[j+1]=t;
		     flag=1;
                   }
               }
	    if(flag==0) break;
             }
        for(i=34; i<50;i++)

	  {   flag=0;
	       for(j=0;j<50-i-1;j++)
              {
		if(p[j].figure >= p[j+1].figure)
                  {
		      t=p[j];
                      p[j]=p[j+1];
		      p[j+1]=t;
		     flag=1;
                   }
               }
	    if(flag==0) break;
             }
  }

/***************************************
将每张牌所能组成的一条龙张数表示出来
****************************************/
int longer(struct card p[],int m,int number)
/*返回第一张牌的最长接龙,m表示第M张牌,m表示总共的张数*/
{
   int mark=1,j=0;
   	for(j=m;j<number;j++)
            {

              if((p[j].figure == p[j+1].figure-1) && (j+1)<number)
                 mark=mark+1;
              else
                   {if((p[j].figure == p[j+1].figure) && (j+1)<number) continue;
                    else break;
                   }

             }
     return mark;
}
/***************************************
对牌进行扫描,将一组牌所能组成的
一条龙张数表示出来
****************************************/
void inforcard(struct card p[],int num)
  {
    register int i=0;
    int len=0,j=0,tem=0;
    	for(i=0;i< num;i++)
            {
               if( p[i].figure == p[i+1].figure && p[i+1].figure == p[i+2].figure
                       && p[i+2].figure == p[i+3].figure && i+3<num)
                  {
                    p[i].flag[0]=4;
                    p[i+1].flag[0]=4;
                    p[i+2].flag[0]=4;
                    p[i+3].flag[0]=4;
                    i=i+3;
                   }
                 else
                     if( p[i].figure == p[i+1].figure && p[i+1].figure == p[i+2].figure
                             && i+2<num)
                        {
                         p[i].flag[0]=3;
                         p[i+1].flag[0]=3;
                         p[i+2].flag[0]=3;
                         i=i+2;
                        }
                      else
                           if( p[i].figure == p[i+1].figure && i+1<num)
                           {
                            p[i].flag[0]=2;
                            p[i+1].flag[0]=2;
                            i=i+1;
                           }
               }
           for(i=0;i<num;i++)
            {
               tem=0;
               len=longer(cards,i,num); /*牌的长度*/
               j=i;
               while(j<i+len)
                 {
		   p[i+tem].flag[1]=len;
		   if(p[i+tem].figure!=p[i+tem+1].figure) j=j+1;
                   tem++;
                  }

	       i=i+tem-1;    /*i会再加1*/
             }
  }
/*************************************
 初始为图形模式
 **************************************/
  initgraphs()
 {
  int gd=DETECT,gm=12;
  registerbgidriver(EGAVGA_driver);
  initgraph(&gd,&gm,"\\tc");
 }

/*************************************
 将数组转换为而进制
 **************************************/

void change(long q[])
{
   int i=0,j=0;
	for(i=0;i<16;i++)
           {
             for(j=0;j<16;j++)
               {
                 cursoron[i][j]=q[i]%2;
                 q[i]=q[i]/2;

                 }
             }
 }
/*************************************
 鼠标的位图
 **************************************/
void initcursor()
{


   long oncursor[16]=
   {

    0xC000,0xA000,0x9000,0x8800,
    0x8400,0x8200,0x8100,0x8080,
    0x8040,0x89E0,0x9100,0xA880,
    0xC440,0x8220,0x01F0,0x0000
   };

  change(oncursor);
}
/*****************************************
 用来画鼠标,并保存鼠标所在地方的点的信息
 *****************************************/

void on_mouse(xnow,ynow)
{
  int i=0,j=0;
   	for(i=0;i<16;i++)

               for(j=0;j<16;j++)
                    {
		      pattrib[i][j]=getpixel(xnow+i,ynow+j);
                      if(cursoron[i][j]==1)
		      putpixel(xnow+i,ynow+j,10);
                    }
}
/*************************************
 将原来的鼠标隐藏,还原原来的点的值
 **************************************/
void off_mouse(xnow,ynow)
 {
   int i=0,j=0;
     	for(i=0;i<16;i++)
            for(j=0;j<16;j++)

		putpixel(xnow+i,ynow+j,pattrib[i][j]);

 }

/*************************************
 鼠标复位
 **************************************/
 char InitMs()
{
  union REGS Inr, Outr;
  Inr.x.ax=0;
  int86(0x33,&Inr,&Outr);
  if(Outr.x.ax==0) return 0;
  else return 1;
}


/*************************************
 判断鼠标是否移动
 **************************************/

int movemouse()
{
  int x0,y0,but0;
  union REGS rin;
  rin.x.ax=3;
  int86(0x33,&rin,&rin);
  x0=rin.x.cx;
  y0=rin.x.dx;
  but0=rin.x.bx;
  if(xnow!=x0 || ynow!=y0 || but0!=button)  return 1;
  else return 0;
}

/*************************************
 读取鼠标新的信息
 **************************************/
int MouseRead()
{
   union REGS rin;
  rin.x.ax=3;
  int86(0x33,&rin,&rin);
  button=rin.x.bx;
  xnow=rin.x.cx;
  ynow=rin.x.dx;
  return -1;
}

/***************************************
  函数用来清除原来的牌,并在新的位置显示
******************************************/
 void clear(int x,int y,int flag) /*FLAG为1时,牌向上突出*/
 {
   int i=0,j=0;
   unsigned attrib[100][25];

     if(flag==0)
        {
         for(i=0;i<100;i++)
	   for(j=0;j<25;j++)
             {
	       attrib[i][j]=getpixel(x+j,y-40+i);
               putpixel(x+j,y-40+i,BKCOLOR);
              }
          for(i=0;i<100;i++)
	    for(j=0;j<25;j++)
	       putpixel(x+j,y+i,attrib[i][j]);

         }
      else
         {
           for(i=0;i<100;i++)
	   for(j=0;j<25;j++)
             {
	       attrib[i][j]=getpixel(x+j,y+i);
               putpixel(x+j,y+i,BKCOLOR);
              }

           for(i=0;i<100;i++)
	    for(j=0;j<25;j++)
	       putpixel(x+j,y-40+i,attrib[i][j]);

	  }
 }
 /***************************************
  函数用来清除原来的牌
******************************************/
 void card_cls(int x1,int y1,int x2,int y2)
  {
   int i=0,j=0;
   		for(i=x1;i<=x2;i++)
	  	    for(j=y1;j<=y2;j++)
             putpixel(i,j,BKCOLOR);

  }
/***************************************
  函数用来初始化PLAYER1的牌
******************************************/
void swplayer1()
{
  int i=0;
   	for(i=0;i<17;i++)
              player1[i]=cards[i];
}
/***************************************
  函数用来让电脑出牌
******************************************/
computer_out(int numb)
 {
   int i=0;
   switch(numb)
      {

          case 1:
		  for(i=0;i<17;i++)
                     {
			if(cards[17+i].flag[1]==1 &&
			   cards[17+i].figure > outcards[0].figure)
			{ outcomputer[0] = cards[17+i];

			  showcard(outcomputer,1,20,YP-250);
                          break;
                         }
                       }
	}
}
/***************************************
  函数用来更新PLAYER1的牌
******************************************/
void reduplayer1()
{
  int i=0,j=0;
   	for(i=0;i<17;i++)
            {
             player1[i].flag[2]=0;
             if(player1[i].figure!=-1)
	        player1[j++]=player1[i];

            }


}
/***************************************
  函数用来出牌
******************************************/
int outcard(int num)
 {
	 int i=0,cflag=0;

 	 		for(i=0;i<num;i++)
				{ if(player1[i].flag[2]%2 == 1 && player1[i].figure!=-1)
				    {
             outcards[cflag++] = player1[i];
             player1[i].figure=-1;
             player1[i].flag[2]=0;
            }
				}
 	 switch (cflag)
 	 {
		case 1:	card_cls(XP-400,YP-250,XP-50,YP-150);
                        showcard(outcards,1,XP-50,YP-150);
			computer_out(1);
			break;
		case 2:
                              if(outcards[0].figure== outcards[1].figure)
			         { card_cls(XP-400,YP-250,XP-50,YP-150);
                                   showcard(outcards,2,XP-50,YP-150);
                                   break;
                                 }
			     else { outtextxy(100,100,"error");break;}
                 case 3:
                             if(outcards[0].figure== outcards[1].figure &&
                                    outcards[1].figure== outcards[2].figure)
			         { card_cls(XP-400,YP-250,XP-50,YP-150);
                                   showcard(outcards,3,XP-50,YP-150);
                                   break;
                                 }
			     else { outtextxy(100,100,"error");break;}
		case 4:
			     if((outcards[0].figure== outcards[1].figure &&
			     	  outcards[1].figure== outcards[2].figure)     ||
			     	  (outcards[1].figure== outcards[2].figure &&
			     	   outcards[2].figure== outcards[3].figure )     ||
			     	  (outcards[0].figure== outcards[1].figure &&
			     	   outcards[1].figure== outcards[2].figure &&
			     	   outcards[2].figure== outcards[3].figure))
			         { card_cls(XP-400,YP-250,XP-50,YP-150);
                                    showcard(outcards,4,XP-50,YP-150);
                                     break;
                                  }
			     else { outtextxy(100,100,"error");break;}
	         case 5:
                             if(outcards[0].figure== outcards[1].figure-1 &&
			     	 outcards[1].figure== outcards[2].figure-1 &&
			     	 outcards[2].figure== outcards[3].figure-1 &&
                                 outcards[3].figure== outcards[4].figure-1 )
			         { card_cls(XP-400,YP-250,XP-50,YP-150);
                                    showcard(outcards,5,XP-50,YP-150);
                                     break;
                                  }
			     else { outtextxy(100,100,"error");break;}
                 case 6:
                             if((outcards[0].figure== outcards[1].figure-1 &&
			     	 outcards[1].figure== outcards[2].figure-1 &&
			     	 outcards[2].figure== outcards[3].figure-1 &&
                                 outcards[3].figure== outcards[4].figure-1 &&
                                 outcards[4].figure== outcards[5].figure-1)  ||
				 (outcards[0].figure== outcards[1].figure &&
			     	  outcards[2].figure== outcards[3].figure &&
			     	  outcards[4].figure== outcards[5].figure))

			         { card_cls(XP-400,YP-250,XP-50,YP-150);
                                    showcard(outcards,6,XP-50,YP-150);
                                     break;
                                  }
			     else { outtextxy(100,100,"error");break;}
                  case 7:
                             if(outcards[0].figure== outcards[1].figure-1 &&
			     	 outcards[1].figure== outcards[2].figure-1 &&
			     	 outcards[2].figure== outcards[3].figure-1 &&
                                 outcards[3].figure== outcards[4].figure-1 &&
                                 outcards[4].figure== outcards[5].figure-1 &&
                                 outcards[5].figure== outcards[6].figure-1 )

			         { card_cls(XP-400,YP-250,XP-50,YP-150);
                                    showcard(outcards,7,XP-50,YP-150);
                                     break;
                                  }
			     else { outtextxy(100,100,"error");break;}
                    case 8:
                             if(( outcards[0].figure== outcards[1].figure-1 &&
			     	 outcards[1].figure== outcards[2].figure-1 &&
			     	 outcards[2].figure== outcards[3].figure-1 &&
                                 outcards[3].figure== outcards[4].figure-1 &&
                                 outcards[4].figure== outcards[5].figure-1 &&
                                 outcards[5].figure== outcards[6].figure-1 &&
                                 outcards[6].figure== outcards[7].figure-1)||
                                 (outcards[0].figure== outcards[1].figure &&
			     	  outcards[2].figure== outcards[3].figure &&
			     	  outcards[4].figure== outcards[5].figure &&
				  outcards[6].figure== outcards[7].figure )
                                  )

			         { card_cls(XP-400,YP-250,XP-50,YP-150);
                                    showcard(outcards,8,XP-50,YP-150);
                                     break;
                                  }
			     else { outtextxy(100,100,"error");break;}
                   case 9:
                             if (outcards[0].figure== outcards[1].figure-1 &&
			     	 outcards[1].figure== outcards[2].figure-1 &&
			     	 outcards[2].figure== outcards[3].figure-1 &&
                                 outcards[3].figure== outcards[4].figure-1 &&
                                 outcards[4].figure== outcards[5].figure-1 &&
                                 outcards[5].figure== outcards[6].figure-1 &&
                                 outcards[6].figure== outcards[7].figure-1 &&
                                 outcards[7].figure== outcards[8].figure-1
                                 )

			         { card_cls(XP-400,YP-250,XP-50,YP-150);
                                    showcard(outcards,9,XP-50,YP-150);
                                     break;
                                  }
			     else { outtextxy(100,100,"error");break;}

 	 }
	reduplayer1();
	card_cls(XP-425,YP-150,XP,YP);
        outnumber=outnumber+cflag;
        showcard(player1,17-outnumber,XP,YP);
 	return cflag;
 }


/************************************************
  处理左右键的点击
  ***********************************************/
void dealclick()
 {
   char buff[10];

   int x=0,k=0,tem=XP - 25*NUMBER;
   
   if(getpixel(xnow,ynow) !=BKCOLOR && (button==1 || button==2))
    {
      off_mouse(xnow,ynow);
      x=(xnow-tem)/25*25+tem;
      k=16-(xnow-tem)/25;
      if(button==1)
        {
      	 player1[k].flag[2]=player1[k].flag[2]+1;
	 clear(x,YP-100,player1[k].flag[2]%2);
        }
      else
      	  if(button==2)
      	{
          outcard(17-outnumber);
          
      	}
      on_mouse(xnow,ynow);
    }

  }

main()
{
  initgraphs();
  setbkcolor(BKCOLOR);
  initcard();
  washcard();
  sortcard(cards);
  inforcard(cards,17);
  showcard(cards,17,XP,YP);
  InitMs();
  initcursor();
  swplayer1();
  on_mouse(xnow,ynow);
 do{
   if(movemouse())
   {
    off_mouse(xnow,ynow);
    MouseRead();
    printf("\r%4d   %4d   %4d",xnow,ynow,button);
    on_mouse(xnow,ynow);
   }
   dealclick();
  }while(!kbhit());

}

你可能感兴趣的:(游戏,C++,c,C#,XP)