program ahorcado;
uses crt;
const pal:array[1..30] of string[20]=('INFORMATICA','ABRACADABRA',
          'COMPUTACION','HERRAMIENTAS','COMERCIALIZACION',
          'NATURALIDAD','MANTENIMIENTO','SECUENCIACION',
          'DEMOSTRACION','BASIFICACION','TOTALITARISMO',
          'IDIOSINCRACIA','SUSTENTACION','NEGATIVIDAD',
          'DURABILIDAD','FRATERNALMENTE','FAVORABLEMENTE',
          'CONDUCTISMO','HIPOCONDRIACO','INDIGENISTA',
          'EFECTIVAMENTE','GARIGOLEADO','JUSTIFICACION',
          'KALEIDOSCOPIO','LONGITUDINAL','OPERATIVAMENTE',
          'PALEONTOLOGIA','QUIMIOTERAPIA','RINOCERONTE',
          'VULNERABILIDAD');

var sel,rsp:string[20];
    op,nop,c,np,tam,k:integer;
    letra,mas:char;
    ac:boolean;

procedure clr(f,p:integer);
begin
 textbackground(f);
 textcolor(p);
end;

procedure son(frec,dura:integer);
begin
 sound(frec);
 delay(dura);
 nosound;
end;

procedure pau(n:integer);
begin
 delay(n);
end;

procedure marco(x1,y1,x2,y2:integer;cr:char);
begin
 for k:=x1 to x2 do
 begin
  gotoxy(k,y1);
  write(cr);
  gotoxy(k,y2);
  write(cr);
 end;
 for k:=y1+1 to y2-1 do
 begin
  gotoxy(x1,k);
  write(cr);
  gotoxy(x2,k);
  write(cr);
 end;
end;

procedure cadalzo;
begin
 for k:=0 to 9 do begin gotoxy(60+k,6); write(chr(178)); end;
 for k:=1 to 10 do begin gotoxy(60,6+k); write(chr(178)); end;
 for k:=0 to 13 do begin gotoxy(58+k,17); write(chr(178)); end;
 gotoxy(65,7); write(chr(179));
end;

procedure figura(n:integer);
begin
 case n of
  1: begin gotoxy(65,8);  write(chr(2));
           gotoxy(55,20); write('Primero la cabeza');
     end;
  2: begin gotoxy(65,9); write(chr(178));
           gotoxy(65,10); write(chr(178)); gotoxy(55,20);
           write('Luego el cuerpo');
     end;
  3: begin gotoxy(64,11); write(chr(178));
           gotoxy(64,12); write(chr(178)); gotoxy(55,20);
           write('Luego pierna derecha');
     end;
  4: begin gotoxy(66,11); write(chr(178));
           gotoxy(66,12); write(chr(178)); gotoxy(55,20);
           write('Luego pierna izquierda');
     end;
  5: begin gotoxy(64,9); write(chr(205));
           gotoxy(63,9); write(chr(205)); gotoxy(55,20);
           write('Luego brazo derecho');
     end;
  6: begin gotoxy(66,9); write(chr(205));
           gotoxy(67,9); write(chr(205)); gotoxy(55,20);
           write('Luego brazo izquierdo');
     end;
  7: begin gotoxy(63,13); write(chr(223)); gotoxy(55,20);
           write('Luego pie derecho');
     end;
  8: begin gotoxy(67,13); write(chr(223)); gotoxy(55,20);
           write('Luego pie izquierdo');
     end;
  9: begin gotoxy(62,9); write(chr(223)); gotoxy(55,20);
           write('Luego mano derecha');
     end;
  10: begin gotoxy(68,9); write(chr(223)); gotoxy(55,20);
           write('Luego mano izquierda');
      end;
 end;
end;

procedure alegre;
begin
 son(300,700); pau(150); son(320,300); pau(150); son(360,300);
 pau(150); son(360,300); pau(150); son(320,300); pau(150);
 son(300,300); pau(150); son(270,300); pau(150); son(240,300);
 pau(150); son(240,300); pau(150); son(270,300); pau(150);
 son(300,300); pau(150); son(300,700); pau(150); son(270,700);
 pau(150);  son(300,700); pau(150); son(320,300); pau(150); son(360,300);
 pau(150); son(360,300); pau(150); son(320,300); pau(150);
 son(300,300); pau(150); son(270,300); pau(150); son(240,300);
 pau(150); son(240,300); pau(150); son(270,300); pau(150);
 son(300,300); pau(150); son(270,700); pau(150); son(240,700);
end;

procedure triste;
begin
 son(240,700); pau(200); son(240,300); pau(200); son(240,300);
 pau(200); son(240,500); pau(200); son(290,500); pau(150);
 son(270,300); pau(150); son(270,300); pau(150); son(240,300);
 pau(150); son(240,300); pau(150); son(230,300); pau(150);
 son(240,700);
end;

begin {PROGRAMA PRINCIPAL}
 randomize;
 repeat
  clr(4,14);
  clrscr;
  marco(1,1,80,24,chr(178));
  clr(4,15);
  marco(2,3,50,23,chr(177));
  marco(51,3,79,23,chr(177));
  clr(0,14);
  marco(2,2,79,2,chr(176));
  gotoxy(30,2);
  write('E L   A H O R C A D O');
  clr(4,14);
  np:=random(30)+1;
  sel:=pal[np];
  tam:=length(sel);
  rsp:='';
  for k:=1 to tam do rsp:=rsp+'_';
  gotoxy(20,5);
  clr(0,10);
  write(rsp);
  cadalzo;
  c:=0;
  gotoxy(18,10);
  clr(0,15);
  write('TECLEA UNA LETRA: ');
  gotoxy(18,13);
  write('LETRAS SELECCIONADAS');
  nop:=0;
  op:=0;
  repeat
   clr(0,14);
   gotoxy(36,10);
   readln(letra);
   son(600,10);
   gotoxy(20+nop,14);
   letra:=upcase(letra);
   write(letra);
   ac:=false;
   for k:=1 to tam do
   begin
    if (letra=sel[k]) and (rsp[k]='_') then
    begin
     ac:=true;
     rsp[k]:=letra;
     inc(c);
    end;
   end;
   gotoxy(20,5);
   clr(0,10);
   write(rsp);
   inc(nop);
   if ac=false then
   begin
    inc(op);
    clr(0,10+BLINK);
    figura(op);
    delay(1500); gotoxy(55,20); clr(4,10); write(' ':22);
   end;
  until (c=tam) or (op=10);
  clr(0,11+BLINK);
  if c=tam then
  begin
   gotoxy(16,18);
   write('­ TE CREES MUY LISTO !');
   alegre;
  end
  else
  begin
   gotoxy(13,18);
   write('LA PALABRA ERA: ',sel);
   triste;
   pau(400);
   triste;
  end;
  gotoxy(12,21);
  clr(0,15+BLINK);
  write('Quieres jugar de nuevo <S/N>? ');
  mas:=upcase(readkey);
  son(500,20);
 until mas<>'S';
end.
