//PROGRAMA: JUEGO DE SERPIENTES Y ESCALERAS
//AUTORES:  MAURO ANDRADE LOPEZ
//          JOSE DE JESUS PADILLA BA¥UELAS
//FECHA:    12/01/2001
//ESCUELA:  C.B.T.i.s. No. 185
//MATERIA:  LENGUAJE DE PROGRAMACION II
//ESPECIALIDAD: TECNICO EN COMPUTACION
//PROFESOR: ING. VICTOR MANUEL REYES PEREZ
//LUGAR:    CARDENAS, S.L.P.

#include <conio.h>
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <alloc.h>
#include <time.h>
#include <string.h>
#include <ctype.h>
#include <dos.h>

void sonido(int frec,float durac);

void main()
{
  int ag,mg,k,a,b,l,n,m,o,p,q,g,z,j,aa,ab,ac,num,x[6],y[6],dato,njug,xi;
  int bandera,nalea;
  unsigned tam;
  float dura;
  void far *imagen[6];
  void far *imagdad[6];
  char h[3],tecla,cadena[10],ncad[2],cap,otro;
  randomize();
  do
  {
   clrscr();
   gotoxy(20,3);
   printf("¨CUANTOS JUGADORES DESEAS?\n");
   gotoxy(20,4);
   printf("2,3,4,5 ¢ 6");
   gotoxy(32,4);
   scanf("%d",&num);
   clrscr();
   ag=DETECT;
   initgraph (&ag,&mg,"C:\\TC\\BGI");
   k=4;
   switch(num)
   {
     case 2:
       setcolor(k);
       circle(120,401,5);
       setfillstyle(1,k);
       floodfill(120,401,k);
       setcolor(8);
       circle(100,401,5);
       setfillstyle(1,8);
       floodfill(100,401,8);
       break;
     case 3:
       setcolor(4);
       circle(120,401,5);
       setfillstyle(1,4);
       floodfill(120,401,4);
       setcolor(8);
       circle(100,401,5);
       setfillstyle(1,8);
       floodfill(100,401,8);
       setcolor(15);
       circle(80,401,5);
       setfillstyle(1,15);
       floodfill(80,401,15);
       break;
     case 4:
       setcolor(4);
       circle(120,401,5);
       setfillstyle(1,4);
       floodfill(120,401,4);
       setcolor(8);
       circle(100,401,5);
       setfillstyle(1,8);
       floodfill(100,401,8);
       setcolor(15);
       circle(80,401,5);
       setfillstyle(1,15);
       floodfill(80,401,15);
       setcolor(5);
       circle(60,401,5);
       setfillstyle(1,5);
       floodfill(60,401,5);
       break;
     case 5:
       setcolor(4);
       circle(120,401,5);
       setfillstyle(1,4);
       floodfill(120,401,4);
       setcolor(8);
       circle(100,401,5);
       setfillstyle(1,8);
       floodfill(100,401,8);
       setcolor(15);
       circle(80,401,5);
       setfillstyle(1,15);
       floodfill(80,401,15);
       setcolor(5);
       circle(60,401,5);
       setfillstyle(1,5);
       floodfill(60,401,5);
       setcolor(12);
       circle(40,401,5);
       setfillstyle(1,12);
       floodfill(40,401,12);
       break;
     case 6:
       setcolor(4);
       circle(120,401,5);
       setfillstyle(1,4);
       floodfill(120,401,4);
       setcolor(8);
       circle(100,401,5);
       setfillstyle(1,8);
       floodfill(100,401,8);
       setcolor(15);
       circle(80,401,5);
       setfillstyle(1,15);
       floodfill(80,401,15);
       setcolor(5);
       circle(60,401,5);
       setfillstyle(1,5);
       floodfill(60,401,5);
       setcolor(12);
       circle(40,401,5);
       setfillstyle(1,12);
       floodfill(40,401,12);
       setcolor(11);
       circle(20,401,5);
       setfillstyle(1,11);
       floodfill(20,401,11);
       break;
   }
   setfillstyle(1,7);
   bar(139,59,499,419);
   setbkcolor(1);
   a=59;
   b=419;
   for (k=139;k<499;k=k+36)
   {
     rectangle(k,a,k+36,b);
     setcolor(15);
   }
   m=139;
   n=499;
   for (l=59;l<419;l=l+36)
   {
     rectangle(m,l,n,l+36);
     setcolor(15);
   }
   //DATOS ADICIONALES
   outtextxy(15,460,"PARA EL DADO PRESIONAR D");
   outtextxy(320,460,"PARA MOVERTE UTILIZA LAS FLECHAS");
   rectangle(10,450,620,470);
   //DADOS
   outtextxy(76,50,"DADO");
   rectangle(73,59,109,95);

   setcolor(15);
   setfillstyle(1,15);

   //dado 1
   circle(91,77,2);
   floodfill(91,77,15);
   for(k=0;k<6;k++)
   {
    tam=imagesize(73,59,109,95);
    if ((imagdad[k]=farmalloc(tam))==NULL)
    {
     closegraph();
     printf("ERROR:no hay suficiente espacio en memoria");
     exit(1);
    }
   }

   getimage(73,59,109,95,imagdad[0]);
   putimage(73,59,imagdad[0],1);

   //dado 2
   rectangle(73,59,109,95);
   circle(82,68,2);
   floodfill(82,68,15);
   circle(100,86,2);
   floodfill(100,86,15);
   getimage(73,59,109,95,imagdad[1]);
   putimage(73,59,imagdad[1],1);

   //dado 3
   rectangle(73,59,109,95);
   circle(82,68,2);
   floodfill(82,68,15);
   circle(100,86,2);
   floodfill(100,86,15);
   circle(91,77,2);
   floodfill(91,77,15);
   getimage(73,59,109,95,imagdad[2]);
   putimage(73,59,imagdad[2],1);

   //dado 4
   rectangle(73,59,109,95);
   circle(82,68,2);
   floodfill(82,68,15);
   circle(100,86,2);
   floodfill(100,86,15);
   circle(82,86,2);
   floodfill(82,86,15);
   circle(100,68,2);
   floodfill(100,68,15);
   getimage(73,59,109,95,imagdad[3]);
   putimage(73,59,imagdad[3],1);

   //dado 5
   rectangle(73,59,109,95);
   circle(82,68,2);
   floodfill(82,68,15);
   circle(100,86,2);
   floodfill(100,86,15);
   circle(82,86,2);
   floodfill(82,86,15);
   circle(100,68,2);
   floodfill(100,68,15);
   circle(91,77,2);
   floodfill(91,77,15);
   getimage(73,59,109,95,imagdad[4]);
   putimage(73,59,imagdad[4],1);

   //dado 6
   rectangle(73,59,109,95);
   circle(82,68,2);
   floodfill(82,68,15);
   circle(100,86,2);
   floodfill(100,86,15);
   circle(82,86,2);
   floodfill(82,86,15);
   circle(100,68,2);
   floodfill(100,68,15);
   circle(82,77,2);
   floodfill(82,77,15);
   circle(100,77,2);
   floodfill(100,77,15);
   getimage(73,59,109,95,imagdad[5]);
   putimage(73,59,imagdad[5],1);

   //CUADRO COMENTARIOS
   outtextxy(277,20,"COMENTARIOS");
   rectangle(247,30,391,45);
   //ESCALERAS
   setcolor(9);
   line(187,77,259,149);
   line(199,77,271,149);
   k=196;
   o=86;
   for (l=1;l<8;l++)
   {
     line(k,o,k+12,o);
     k+=9;
     o+=9;
   }
   line(367,149,331,176);
   line(379,149,343,176);
   k=170;
   for (l=340;l<367;l+=12)
   {
     line(l,k,l+12,k);
     k-=9;
   }
   line(331,194,295,212);
   line(343,194,307,212);
   k=210;
   for (l=300;l<340;l+=14)
   {
     line (l,k,l+12,k);
     k-=7;
   }
   line(295,230,259,257);
   line(307,230,271,257);
   k=253;
   for (l=264;l<295;l+=12)
   {
     line(l,k,l+12,k);
     k-=9;
   }
   line(475,221,475,365);
   line(487,365,487,221);
   for (l=230;l<365;l+=9)
   {
     line(475,l,487,l);
   }
   line(223,329,223,401);
   line(235,329,235,401);
   l=223;
   for(k=338;k<400;k+=9)
     line(l,k,l+12,k);
   line(333,293,405,221);
   line(345,293,417,221);
   k=284;
   for(l=342;l<405;l+=9)
   {
     line(l,k,l+12,k);
     k-=9;
   }
   line(151,185,151,257);
   line(163,185,163,257);
   k=151;
   for(l=194;l<257;l+=9)
     line(k,l,k+12,l);
   line(403,293,439,329);
   line(415,293,451,329);
   k=302;
   for(l=412;l<439;l+=9)
   {
     line(l,k,l+12,k);
     k+=9;
   }
   line(259,185,223,221);
   line(271,185,235,221);
   k=212;
   for(l=232;l<259;l+=9)
   {
     line(l,k,l+12,k);
     k-=9;
   }
   line(403,77,439,113);
   line(415,77,451,113);
   k=86;
   for(l=412;l<439;l+=9)
   {
     line(l,k,l+12,k);
     k+=9;
   }
   //SERPIENTE 1 CABEZA 5,A
   setcolor(10);
   //CUERPO
   setcolor(4);
   l=77;
   for(k=229;k<=409;k+=36)
   {
     line(k,l,k,l+36);
     l+=36;
   }
   line(409,302,409,295);
   k=229;
   for(l=113;l<=257;l+=36)
   {
     line(k,l,k+36,l);
     k+=36;
   }

   setcolor(4);
   circle(409,311,9);
   //SERPIENTE (2)
   circle(301,383,9);
   setcolor(10);
   line(299,387,297,387);
   line(303,387,305,387);
   //CUERPO EN INGLES
   setcolor(4);
   l=293;
   for(k=229;k<=300;k+=36)
   {
     line(k,l,k,l+36);
     l+=36;
   }
   line(409,302,409,295);
   k=229;
   for(l=329;l<=365;l+=36)
   {
     line(k,l,k+36,l);
     k+=36;
   }
   line(301,365,301,374);
   //NEMBERS
   setcolor(0);
   for(l=10;l>=1;l-=2)
    for(a=10;a>=1;a--)
    {
     k=475-36*(a-1);
     o=411-36*(l-1);
     dato=10*(l-1)+a;
     itoa(dato,h,10);
     outtextxy(k,o,h);
    }
   for(l=9;l>=1;l-=2)
    for(a=10;a>=1;a--)
    {
     k=153+36*(a-1);
     o=411-36*(l-1);
     dato=10*(l-1)+a;
     itoa(dato,h,10);
     outtextxy(k,o,h);
    }
   //SERPIENTE (3)
   circle(175,221,9);
   //CUERPO
   setcolor(65);
   l=77;
   for(k=301;k>=193;k-=36)
   {
     line(k,l,k,l+36);
     l+=36;
   }
   setcolor(65);
   k=265;
   for(l=113;l<=185;l+=36)
   {
     line(k,l,k+36,l);
     k-=36;
   }
   line(193,221,184,221);
   line(301,365,301,374);
   //JUEGO
   x[0]=114;
   y[0]=395;
   x[1]=94;
   y[1]=395;
   x[2]=74;
   y[2]=395;
   x[3]=54;
   y[3]=395;
   x[4]=34;
   y[4]=395;
   x[5]=14;
   y[5]=395;
   njug=0;
   for(k=0;k<6;k++)
   {
    tam=imagesize(x[k],y[k],x[k]+12,y[k]+12);
    if ((imagen[k]=farmalloc(tam))==NULL)
    {
     closegraph();
     printf("ERROR:no hay suficiente espacio en memoria");
     exit(1);
    }
   }
   for(xi=0;xi<6;xi++)
     getimage(x[xi],y[xi],x[xi]+12,y[xi]+12,imagen[xi]);
   putimage(x[njug],y[njug],imagen[njug],1);
   bandera=0;
   if(bandera==0)
   {
      x[njug]=152;
      y[njug]=395;
   }
   do
   {
    strcpy(cadena,"");
    setfillstyle(0,1);
    bar(250,35,350,40);
    strcpy(cadena,"Jugador: ");
    itoa(njug+1,ncad,10);
    strcat(cadena,ncad);
    setcolor(15);
    outtextxy(250,35,cadena);
    putimage(x[njug],y[njug],imagen[njug],1);
    do
    {
     cap=toupper(getch());
    }while(cap!='D');
    for(k=0;k<6;k++)
    {
     nalea=random(6);
     putimage(73,59,imagdad[nalea],0);
     sonido(random(1000)+100,0.005);
     delay(40);
     nosound();
     putimage(73,59,imagdad[nalea],0);
    }

    do
    {
     tecla=getch();
     dura=0.005;
     for(k=600;k<700;k+=20) sonido(k,dura);
     putimage(x[njug],y[njug],imagen[njug],1);
     switch(tecla)
     {
      case 77:
       x[njug]+=36;
       if(x[njug]>476) x[njug]=476;
       break;
      case 75:
       x[njug]-=36;
       if(x[njug]<152) x[njug]=152;
       break;
      case 80:
       y[njug]+=36;
       if(y[njug]>395) y[njug]=395;
       break;
      case 72:
       y[njug]-=36;
       if(y[njug]<71) y[njug]=71;
       break;
     }
     putimage(x[njug],y[njug],imagen[njug],1);
    }while(tecla!=13);
    //SUBE Y BAJA

    //1
    if (x[njug]==224 && y[njug]==395)
    {
      putimage(x[njug],y[njug],imagen[njug],1);
      x[njug]=224; y[njug]=323;
      putimage(x[njug],y[njug],imagen[njug],1);
    }
    //2
    if (x[njug]==476 && y[njug]==359)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]=476; y[njug]=215;
     putimage(x[njug],y[njug],imagen[njug],1);
    }
    //3
    if (x[njug]==440 && y[njug]==323)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]=404; y[njug]=287;
     putimage(x[njug],y[njug],imagen[njug],1);
    }
    //4
    if (x[njug]==152 && y[njug]==251)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]=152; y[njug]=179;
     putimage(x[njug],y[njug],imagen[njug],1);
    }
    //5
    if (x[njug]==332 && y[njug]==287)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]= 404; y[njug]=215;
     putimage(x[njug],y[njug],imagen[njug],1);
    }
    //6
    if (x[njug]==224 && y[njug]==215)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]=260; y[njug]=179;
     putimage(x[njug],y[njug],imagen[njug],1);
    }
    //7
    if (x[njug]==260 && y[njug]==251)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]=296; y[njug]=215;
     putimage(x[njug],y[njug],imagen[njug],1);
    }
    //8
    if (x[njug]==296 && y[njug]==215)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]=332; y[njug]=179;
     putimage(x[njug],y[njug],imagen[njug],1);
    }
    //9
    if (x[njug]==332 && y[njug]==179)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]=368; y[njug]=143;
     putimage(x[njug],y[njug],imagen[njug],1);
    }
    //10
    if (x[njug]==440 && y[njug]==107)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]=404; y[njug]=71;
     putimage(x[njug],y[njug],imagen[njug],1);
    }
    //11
    if (x[njug]==260 && y[njug]==143)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]=188; y[njug]=71;
     putimage(x[njug],y[njug],imagen[njug],1);
    }

    //SERPIENTE 1
    if (x[njug]==224 && y[njug]==287)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]=296; y[njug]=395;
     putimage(x[njug],y[njug],imagen[njug],1);
    }
    //SERPIENTE 2
    if (x[njug]==296 && y[njug]==71)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]=152; y[njug]=215;
     putimage(x[njug],y[njug],imagen[njug],1);
    }
    //SERPIENTE 3
    if (x[njug]==224 && y[njug]==71)
    {
     putimage(x[njug],y[njug],imagen[njug],1);
     x[njug]=404; y[njug]=323;
     putimage(x[njug],y[njug],imagen[njug],1);
    }

    putimage(73,59,imagdad[nalea],1);
    if((x[njug]!=152) || (y[njug]!=71))
    {
     njug++;
     if(njug>=num) {njug=0; bandera=1;}
     putimage(x[njug],y[njug],imagen[njug],1);
     if(bandera==0)
     {
	x[njug]=152;
	y[njug]=395;
     }
    }
   }while((x[njug]!=152) || (y[njug]!=71));
   for(k=0;k<6;k++)
   {
    farfree(imagen[k]);
    farfree(imagdad[k]);
   }
   outtextxy(510,240,"Otro Juego <S/N>");
   sonido(600,1);
   otro=toupper(getch());
   closegraph();
 }while(otro!='N');
}

void sonido(int frec,float durac)
{
 sound(frec);
 delay((int) (durac*1000));
 nosound();
}