#include <iostream.h>
#include <constrea.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>

#define RAND_MAX 3

int pacx=7; int pacy=7; int tecla=0;
int fanta1x=10;int fanta1y=10;
int fanta2x=10;int fanta2y=10;
int fanta3x=10;int fanta3y=10;
int fanta4x=10;int fanta4y=10;
int movimiento=0;
void apertura();
void salida();
void dibu();
void dibu1();
void dibu2();
void dibu3();
void combinado_largo_vidas();
void combinado_largo();
void cortito_come();
dibuja_pantalla();
int puntos=0;

 
char matriz[20][26]={
  
'','.','.','.','.','','�','.','.','.','.','.','.','�','.','.','.','.','.','.','�','.','.','.','.','.'//0
  
'','.','.','.','.','.','�','.','.','�','.','.','.','�','.','.','.','�','.','.','�','.','.','.','.','.',
  
'','.','�','�','.','.','�','.','.','�','.','.','.','.','.','.','','�','.','.','�','.','.','�','.','.',
  
'.','.','�','�','.','.','.','.','.','�','�','�','�','�','�','�','�','�','.','.','�','.','.','�','.','.'//4
  
'.','.','�','�','.','.','.','.','.','.','.','�','.','.','.','.','.','.','.','.','�','.','.','�','.','.',
  
'.','.','�','�','�','�','�','.','.','�','�','�','.','.','.','�','�','�','.','.','�','�','�','�','.','.'//6
  
'.','.','�','.','.','.','.','.','.','�','.','.','.','.','.','.','.','�','.','.','.','.','.','.','.','.',
  
'�','�','�','.','.','.','.','.','.','�','','.','.','.','.','.','.','�','.','.','.','.','.','.','.','.',
  
'.','.','�','�','�','�','.','.','.','�','�','�','�','�','�','�','�','�','.','�','�','�','�','�','�','�'//9
  
'.','.','.','.','.','�','.','.','.','.','.','.','.','.','�','.','.','.','.','�','','.','.','.','.','.',
  
'.','.','�','�','�','�','.','.','.','.','�','.','.','�','�','�','�','.','.','�','�','�','�','�','.','.'//11
  
'.','.','�','.','.','.','.','.','.','.','�','.','.','.','.','.','�','.','.','.','.','.','.','�','.','.',
  
'.','.','�','.','.','.','.','.','.','.','�','.','.','.','.','.','�','.','.','�','�','�','�','�','.','.',
  
'.','.','�','.','�','�','�','.','.','.','�','�','�','�','�','�','�','.','.','�','.','.','.','.','.','.'//14
  
'.','.','�','.','�','.','�','.','.','.','.','.','.','�','.','.','.','.','.','�','.','.','.','.','.','.',
  
'.','.','�','�','�','.','�','.','.','.','.','�','�','�','�','�','.','.','.','�','.','.','�','�','.','.',
  
'.','.','.','.','.','.','�','�','�','.','.','�','.','.','.','�','.','.','�','�','.','.','.','�','.',''//17
  
'.','.','.','.','.','.','','.','�','.','.','�','.','�','.','�','.','.','�','.','.','.','.','�','.','',
  
'.','.','�','.','.','�','�','�','�','.','.','.','.','�','.','.','.','.','�','�','�','�','�','�','.',''//19
  
'.','.','�','.','.','.','.','.','.','.','.','.','.','�','.','.','.','.','.','.','.','.','.','.','.','.',
       } ;
 
char matriz_seguridad[20][26];
 
main ()
  {
   
apertura();
   
textcolor(6);
   
textbackground(1);
   
clrscr();
   
for(int j=0;j<20;j++)
    {
  
cout<<endl;
  
gotoxy(26,j+3);
  
for(int i=0;i<26;i++)
   {
    
matriz_seguridad[j][i]=matriz[j][i];
    
cprintf("%c",matriz[j][i]);
    } 
//for i
   
//for j
   
char aux;
   
textcolor(6);
   
textbackground(1);
   
clrscr();
   
dibuja_pantalla();
   
combinado_largo();
   
while (tecla!=27)
    {
     
if (kbhit())
     
tecla=getch();
     
switch (tecla)
      {
  
case 77:   // derecha
     
if (matriz[pacy][pacx+1]=='.' || matriz[pacy][pacx+1]==' ' || matriz[pacy][pacx+1]=='' || matriz[pacy][pacx+1]=='')
      {
         
if (pacx<25)
          {
       
matriz_seguridad[pacy][pacx+1]=' ';
       
cortito_come();
       
puntos++;

       
if (matriz[pacy][pacx+1]=='') // come frutilla
       
{
           
cortito_come();
           
puntos=puntos+99;
        }
       
if (matriz[pacy][pacx+1]=='') // se muere
       
{
           
combinado_largo_vidas;
           
salida();
           
return 0;
         }
        
pacx++;
        
movimiento=6;
        
dibuja_pantalla();
      }
       }
      
break;
  
case 75// izquierda
     
if (matriz[pacy][pacx-1]=='.' || matriz[pacy][pacx-1]==' ' || matriz[pacy][pacx-1]=='' || matriz[pacy][pacx-1]=='')
      {
        
if (pacx>0)
         {
           
matriz_seguridad[pacy][pacx-1]=' ';
           
cortito_come();
           
puntos++;

           
if (matriz[pacy][pacx-1]=='')
            {
        
cortito_come();
        
puntos=puntos+99;
            }
           
if (matriz[pacy][pacx-1]=='') // se muere
           
{
         
combinado_largo_vidas();
         
salida();
         
return 0;
            }
           
pacx--;
           
movimiento=4;
           
dibuja_pantalla();
         }
      }
     
break;
  
case 72:
     
if (matriz[pacy-1][pacx]=='.' || matriz[pacy-1][pacx]==' ' || matriz[pacy-1][pacx]=='' || matriz[pacy-1][pacx]=='')
      {
        
if (pacy>0)
         {
           
matriz_seguridad[pacy-1][pacx]=' ';
           
cortito_come();
           
puntos++;
           
if (matriz[pacy-1][pacx]=='')
            {
         
cortito_come();
         
puntos=puntos+99;
            }
           
if (matriz[pacy-1][pacx]=='') // se muere
           
{
          
combinado_largo_vidas();
          
salida();
          
return 0;
            }
           
pacy--;
           
movimiento=8;
           
dibuja_pantalla();
         }
      }
     
break;
  
case 80:
     
if (matriz[pacy+1][pacx]=='.' || matriz[pacy+1][pacx]==' ' || matriz[pacy+1][pacx]=='' || matriz[pacy+1][pacx]=='')
      {
        
if (pacy<19)
         {
        
matriz_seguridad[pacy+1][pacx]=' ';
        
cortito_come();
        
puntos++;
        
if (matriz[pacy+1][pacx]=='')
         {
           
cortito_come();
           
puntos=puntos+99;
         }
        
if (matriz[pacy+1][pacx]=='') // se muere
        
{
          
combinado_largo_vidas();
          
salida();
          
return 0;
         }
        
pacy++;
        
movimiento=2;
        
dibuja_pantalla();
            }
         }
     
break;
        }
  
tecla=' ';
    }

   
getch();
   
return 0;
}


void apertura()
{
  
char texto[25]=">>>>>> PACMAN <<<<<<";
  
clrscr();
  
for (int j=0;j<50;j++)
   {
    
for (int i=0;i<25;i++)
     {
  
textcolor(i+j);
  
gotoxy(30+i, 10);
  
if (texto[i]=='\0')
      
break;
  
else
      
cprintf("%c",texto[i]);
  
delay (5);
       }
// i
    
} // j
//funcion


dibuja_pantalla()
{
 
gotoxy(70,1);
 
cprintf("%i",puntos);
 
for(int j=0;j<20;j++)
  {
    
for(int i=0;i<26;i++)
  
matriz[j][i]=matriz_seguridad[j][i];
   }
// for

switch(movimiento)
   {
  
case 8:
     
matriz[pacy][pacx]='V';
     
break;
  
case 6:
     
matriz[pacy][pacx]='<';
     
break;

  
case 2:
     
matriz[pacy][pacx]='^';
     
break;
  
case 4:
     
matriz[pacy][pacx]='>';
     
break;
    }

 
switch(rand()%4)
 {
 
case 0:
  
if(fanta1x<25)
      
fanta1x++;
  
if(fanta2x>1)
      
fanta2x--;
  
if(fanta3y<19)
      
fanta3y++;
  
if(fanta4y>1)
      
fanta4y--;
  
break;
 
case 1:
  
if(fanta2x<25)
      
fanta2x++;
  
if(fanta3x>1)
      
fanta3x--;
  
if(fanta4y<19)
      
fanta4y++;
  
if(fanta1y>1)
      
fanta1y--;
     
break;
 
case 2:
  
if(fanta3x<25)
      
fanta3x++;
  
if(fanta4x>1)
      
fanta4x--;
  
if(fanta1y<19)
      
fanta1y++;
  
if(fanta2y>1)
      
fanta2y--;

  
break;
 
case 3:
  
if(fanta4x<25)
      
fanta4x++;
  
if(fanta1x>1)
      
fanta1x--;
  
if(fanta2y<19)
      
fanta2y++;
  
if(fanta3y>1)
      
fanta3y--;
  
break;
 }
 
matriz[fanta1y][fanta1x]=''// fantasmita
 
matriz[fanta2y][fanta2x]='';
 
matriz[fanta3y][fanta3x]='';
 
matriz[fanta4y][fanta4x]='';

textcolor(6);

 
for(j=0;j<20;j++)
  {
    
cout <<endl;
    
for(int i=0;i<26;i++)
     {
      
gotoxy(i+26,j+3);
      
if (matriz[j][i]=='V' || matriz[j][i]=='<' ||matriz[j][i]=='>' ||matriz[j][i]=='^')
  
textcolor(10);
      
else
       if
(matriz[j][i]=='')
  
textcolor(9);
      
else
       if
(matriz[j][i]=='')
  
textcolor(4);
      
else
       if
(matriz[j][i]=='.')
  
textcolor(15);
      
else
  
textcolor(6);
  
cprintf("%c",matriz[j][i]);
      }
// for i
  
// for j
  
return 0;
}
void combinado_largo()
{
  
sound (100);
  
delay (300);
  
sound (300);
  
delay (200);
  
sound (500);
  
delay (300);
  
nosound();
}
void cortito_come()
{
  
sound (400);
  
delay (30);
  
nosound();
}

void  combinado_largo_vidas()
{
    
sound (500);
    
delay (200);
    
sound (300);
    
delay (250);
    
sound (100);
    
delay (500);
    
nosound();
}

void salida()
{
 
window(1,1,80,25);
 
textcolor(WHITE);
 
textbackground(BLUE);
 
clrscr();

 
window(10,5,70,20);
 
textcolor(WHITE);
 
textbackground(BLACK);
 
clrscr();
 
for (int i=0;i<3;i++)
  {
    
dibu1();
    
delay(500);
    
dibu2();
    
delay(500);
    
dibu3();
    
delay(500);
   }
}


void dibu1()
{
 
dibu();
 
gotoxy(10,7);
 
cprintf("                 \\ - /");
}

void dibu()
{
 
gotoxy(30,2);
 
cprintf("            Gracias por jugar");
 
gotoxy(30,3);
 
cprintf("            al Pacman Ver.001");
 
gotoxy(10,5);
 
cprintf("                 -/--\\");
 
gotoxy(10,6);
 
cprintf("                ( . . )");
 
gotoxy(10,8);
 
cprintf("                / ~!~\\ ");
 
gotoxy(10,9);
 
cprintf("              /    &   \\");
 
gotoxy(10,10);
 
cprintf("          -------------------");
}

void dibu2()
{
  
dibu();
  
gotoxy(10,7);
  
cprintf("                 \\ o /");
}

void dibu3()
{
 
dibu();
 
gotoxy(10,7);
 
cprintf("                 \\ = /");

}