{Programa: maya.pas}
{Descripcion: Juego de llenar una malla con el color de los jugadores}
{Autor: Ing. Victor Manuel Reyes Perez}
{Lugar: Cardenas, S.L.P.}
program malla;
uses crt,graph;
var otro:char;

procedure grafico;
var adapgraf,modograf:integer;
begin
 adapgraf:=detect;
 initgraph(adapgraf,modograf,'c:\tp6');
end;

procedure fingrafico;
begin
 closegraph;
end;

procedure presenta;
var i,j:integer;
begin
 for i:=1 to 8 do
  for j:=1 to 6 do
  begin
   setfillstyle(random(7)+2,random(12)+1);
   bar((i-1)*80,(j-1)*80,(i-1)*80+80,(j-1)*80+80);
  end;
 settextstyle(4,0,7);
 setcolor(DARKGRAY);
 outtextxy(171,81,'M A Y A');
 setcolor(LIGHTGRAY);
 outtextxy(173,78,'M A Y A');
 setcolor(YELLOW);
 outtextxy(175,75,'M A Y A');
 setcolor(YELLOW);
 outtextxy(177,73,'M A Y A');
 settextstyle(5,0,1);
 setfillstyle(1,RED);
 bar(80,430,560,450);
 setcolor(YELLOW);
 outtextxy(85,422,'Victor Reyes  -  Cardenas, S.L.P. -  16/Dic/2001');
 readln;
end;

procedure tablero;
var cx,cy:integer;
begin
 setbkcolor(RED);
 cleardevice;
 cy:=30;
 setcolor(YELLOW);
 setfillstyle(1,YELLOW);
 repeat
  cx:=35;
  repeat
   fillellipse(cx,cy,1,1);
   cx:=cx+30;
  until cx>605;
  cy:=cy+30;
 until cy>450;
end;

procedure juego;
type reg=record
      xi,xd,ya,yb,n:integer;
     end;
var clrjug,clrjug1,clrjug2,numjug:integer;
    tecla:char;
    cx,cy,jug,poscur,color:integer;
    cas:array[1..19,1..14] of reg;
    c6,c7:string[6];
    op:boolean;
    ncj1,ncj2:integer;
begin
 for cx:=1 to 19 do
  for cy:=1 to 14 do
  with cas[cx][cy] do
  begin
    xi:=0;
    xd:=0;
    ya:=0;
    yb:=0;
    n:=0;
  end;
 numjug:=random(2)+1;
 clrjug1:=LIGHTGREEN;
 clrjug2:=LIGHTBLUE;
 if numjug=1 then jug:=1 else jug:=2;
 cx:=35;
 cy:=32;
 clrjug:=clrjug1;
 setlinestyle(0,0,3);
 setcolor(YELLOW);
 line(cx,cy,cx,cy+26);
 color:=RED;
 poscur:=1; {1=cursor vertical, -1=horizontal}
 ncj1:=0;
 ncj2:=0;
 settextstyle(0,0,1);
 repeat
  tecla:=readkey;
  if tecla=#0 then tecla:=readkey;
  if tecla<>#13 then
  begin
     setcolor(color);
     if poscur=1 then
      line(cx,cy,cx,cy+26)
     else
      line(cx,cy,cx+26,cy);
  end;
  sound(500);
  delay(35);
  nosound;
  if tecla=#32 then
  begin
   poscur:=-(poscur);
   if poscur=1 then begin cx:=cx-2; cy:=cy+2;
                          if cy>422 then cy:=422; end
               else begin cx:=cx+2; cy:=cy-2;
                          if cx>605 then cx:=577; end;
  end;
  if tecla=#13 then
  begin
   op:=false;
   if jug=1 then
    clrjug:=clrjug1
   else
    clrjug:=clrjug2;
   setcolor(clrjug);
   if poscur=1 then
   begin
    line(cx,cy,cx,cy+26);
    if((cx-35) div 30+1) < 19 then
     cas[(cx-35) div 30+1][(cy-32) div 30+1].xi:=1;
    if ((cx-35) div 30+1)>1 then
     cas[(cx-35) div 30][(cy-32) div 30+1].xd:=1;
    if (((cx-35) div 30 +1)< 20) then
    begin
     with cas[(cx-35) div 30+1][(cy-32) div 30+1] do
     begin
      if ((xi=1) and (xd=1) and (ya=1) and (yb=1) and (n=0)) then
      begin
       setfillstyle(1,clrjug);
       bar(cx+2,cy,cx+28,cy+26);
       cas[(cx-35) div 30+1][(cy-32) div 30+1].n:=jug;
       if jug=1 then ncj1:=ncj1+1 else ncj2:=ncj2+1;
       op:=true;
      end;
    end;
    end;
     if (((cx-35) div 30 )> 0) then
     begin
      with cas[(cx-35) div 30][(cy-32) div 30+1] do
      begin
       if ((xi=1) and (xd=1) and (ya=1) and (yb=1) and (n=0)) then
       begin
        setfillstyle(1,clrjug);
        bar(cx-30+2,cy,cx-30+28,cy+26);
        cas[(cx-35) div 30][(cy-32) div 30+1].n:=jug;
        if jug=1 then ncj1:=ncj1+1 else ncj2:=ncj2+1;
        op:=true;
       end;
      end;
     end;
   end
   else
   begin
    line(cx,cy,cx+26,cy);
    if ((cy-30) div 30+1)<14 then
    cas[(cx-37) div 30+1][(cy-30) div 30+1].ya:=1;
    if ((cy-30) div 30+1)>1 then
     cas[(cx-37) div 30+1][(cy-30) div 30].yb:=1;
    setfillstyle(1,4);
    bar(200,460,600,479);
    if (((cy-30) div 30+1) <15) then
    begin
     with cas[(cx-37) div 30+1][(cy-30) div 30+1] do
     begin
      if ((xi=1) and (xd=1) and (ya=1) and (yb=1) and (n=0)) then
      begin
       setfillstyle(1,clrjug);
       bar(cx,cy+2,cx+26,cy+28);
       cas[(cx-37) div 30+1][(cy-30) div 30+1].n:=jug;
       if jug=1 then ncj1:=ncj1+1 else ncj2:=ncj2+1;
       op:=true;
      end;
     end;
    end;
     if (((cy-30) div 30) > 0) then
     begin
      with cas[(cx-37) div 30+1][(cy-30) div 30] do
      begin
       if ((xi=1) and (xd=1) and (ya=1) and (yb=1) and (n=0)) then
       begin
        setfillstyle(1,clrjug);
        bar(cx,cy-30+2,cx+26,cy-30+28);
        cas[(cx-37) div 30+1][(cy-30) div 30].n:=jug;
        if jug=1 then ncj1:=ncj1+1 else ncj2:=ncj2+1;
        op:=true;
       end;
      end;
     end;
     begin
     end
   end;
   if op=false then
    if jug=1 then jug:=2 else jug:=1;
   str(ncj1,c6); str(ncj2,c7);
   setfillstyle(1,4);
   bar(200,460,439,479);
   outtextxy(250,460,'Jugador 1 = '+c6+'     vs      Jugador 2 = '+c7);
   color:=getpixel(cx,cy);
  end
  else
  begin
   color:=getpixel(cx,cy);
   setcolor(YELLOW);
   if poscur=1 then  {cursor vertical}
   begin
    case tecla of
     #80:      {Flecha abajo}
      begin
       cy:=cy+30;
       if cy>422 then cy:=32;
      end;
     #72:      {Flecha arriba}
      begin
       cy:=cy-30;
       if cy<32 then cy:=422;
      end;
     #77:      {Flecha derecha}
      begin
       cx:=cx+30;
       if cx>605 then cx:=35;
      end;
     #75:     {Flecha izquierda}
      begin
       cx:=cx-30;
       if cx<35 then cx:=605;
      end;
    end;
    color:=getpixel(cx,cy);
    line(cx,cy,cx,cy+26);
   end
   else  {cursor horizontal}
   begin
    case tecla of
     #80:      {Flecha abajo}
      begin
       cy:=cy+30;
       if cy>452 then cy:=30;
      end;
     #72:      {Flecha arriba}
      begin
       cy:=cy-30;
       if cy<30 then cy:=450;
      end;
     #77:      {Flecha derecha}
      begin
       cx:=cx+30;
       if cx>605 then cx:=37;
      end;
     #75:     {Flecha izquierda}
      begin
       cx:=cx-30;
       if cx<35 then cx:=577;
      end;
    end;
    color:=getpixel(cx,cy);
    line(cx,cy,cx+26,cy);
   end;
 end;
 until ((tecla=#27) or ((ncj1+ncj2)=256));
 str(ncj1,c6);
 str(ncj2,c7);
 setfillstyle(1,BLUE);
 bar(50,460,590,479);
 setcolor(YELLOW);
 outtextxy(140,470,'Aciertos Jugador 1: '+c6+'    Aciertos Jugador 2: '+c7);
 tecla:=readkey;
 setlinestyle(0,0,1);
end;

begin {principal}
 randomize;
 grafico;
 presenta;
 repeat
  tablero;
  juego;
  repeat
   setfillstyle(1,BLUE);
   bar(50,460,590,479);
   setcolor(YELLOW);
   outtextxy(230,470,'Desean jugar otra vez <S/N>?');
   otro:=upcase(readkey);
  until otro in ['S','N'];
 until otro<>'S';
 cleardevice;
 settextstyle(9,0,5);
 outtextxy(80,180,'I'+chr(39)+'ll see you later!');
 readln;
 fingrafico;
end.
