program miny;
{mini by ZK 2008-12-08}

uses crt,strings;

var polesloupec:array [0..10] of string;
var poleradek:array [0..10] of string;

var inradek:integer;
    insloupec:char;
    inobtiznost:char;
    inukon:char;

    radekc:integer;
    sloupecc:integer;
    rnda,rndb,rndc:integer;
    bunka:string;
    bunkanew:char;

    showall:string;
    x,y:integer;
    pocetmin:integer;
    pokusc:integer;
    ctrlp:integer;

    ch:char;


    procedure hlavicka;
        begin
        textcolor(15);
        writeln('');
        writeln(' --==========================================--');
        textcolor(14);
        writeln('');
        writeln('       Skvela a zabavna hra LOVENI MIN');
        writeln('');
        textcolor(7);
        textcolor(15);
        writeln(' --==========================================--');
        writeln('');
        textcolor(7);
        end;

    procedure paticka;
        begin
        textcolor(15);
        writeln('');
        writeln(' --==========================================--');
        textcolor(14);
        writeln('');
        writeln('    Mejte se dobre a priste zase NA VIDENOU');
        writeln('');
        textcolor(7);
        textcolor(15);
        writeln(' --==========================================--');
        {
        writeln('');
        }
        writeln('');
        textcolor(7);
        textcolor(13);
        writeln('              ZK (c) 2008-12-27');
        textcolor(7);

        end;

procedure hrainic;
begin
randomize;
clrscr;
hlavicka;

writeln('');
writeln(' Vytejte v loveni min, zvolte si obtiznost');
writeln('');
writeln(' 0 - bezna obtiznost + zobrazeni min');
writeln(' 1 - bezna obtiznost');
writeln(' 2 - profik');
writeln('');

write(' Zvolte si obtiznost: '); {readln(inobtiznost);}


                  repeat
                      gotoxy(23,15);
                      inobtiznost:=readkey;
                      inradek:=ord(inobtiznost)-48;
                      {write(ch);}
                      {writeln(inobtiznost,' - ',inradek);}
                  until (inradek = 0) or (inradek = 1) or (inradek = 2);

                  write(inobtiznost);



writeln;


insloupec:=' ';
{ plneni pole }


    for radekc:=1 to 9 do
        begin

            for sloupecc:=1 to 9 do
                begin
                   poleradek[radekc,sloupecc]:='0';
                end;


                    rnda:=random(9);
                    poleradek[radekc,rnda+1]:='M';

                    repeat
                      rndb:=random(9);

                    until rnda <> rndb;
                    poleradek[radekc,rndb+1]:='M';

                    if inobtiznost = '2' then
                        begin
                          repeat
                          rndc:=random(9);
                          until (rnda <> rndc) and (rndb <> rndc);
                          poleradek[radekc,rndc+1]:='M';
                        end;

                    {writeln(rnda, rndb, rndc);}


        end;


end;
begin
{START HRY}
hrainic;
repeat

clrscr;

if insloupec <> ' ' then
    begin

        sloupecc:=(ord(insloupec))-97+1;

        x:=sloupecc;
        y:=inradek;

        bunka:=poleradek[y,x];

        {
        writeln('V bunce je: ',bunka);
        }

     if inukon = 'o' then
        begin
        {oznacovani min}
        if bunka = '0' then poleradek[y,x]:='!';
        if bunka = 'M' then poleradek[y,x]:='m';
        if bunka = 'm' then poleradek[y,x]:='M';
        if bunka = '!' then poleradek[y,x]:='0';
        pokusc:=pokusc-1;
        end
      else
        begin
        {odkryvani min}

        if (bunka = 'M') or (bunka = 'm') then
           begin
               showall:='ano';
           end
        else
           begin
               if (bunka = '0') or (bunka = '!') then
                 begin
                   bunkanew:=' ';
                 end;

               pocetmin:=0;

               if(poleradek[y-1,x-1]) = 'M' then pocetmin:=pocetmin+1;
               if(poleradek[y-0,x-1]) = 'M' then pocetmin:=pocetmin+1;
               if(poleradek[y+1,x-1]) = 'M' then pocetmin:=pocetmin+1;
               if(poleradek[y+1,x-0]) = 'M' then pocetmin:=pocetmin+1;
               if(poleradek[y+1,x+1]) = 'M' then pocetmin:=pocetmin+1;
               if(poleradek[y+0,x+1]) = 'M' then pocetmin:=pocetmin+1;
               if(poleradek[y-1,x+1]) = 'M' then pocetmin:=pocetmin+1;
               if(poleradek[y-1,x-0]) = 'M' then pocetmin:=pocetmin+1;

               if(poleradek[y-1,x-1]) = 'm' then pocetmin:=pocetmin+1;
               if(poleradek[y-0,x-1]) = 'm' then pocetmin:=pocetmin+1;
               if(poleradek[y+1,x-1]) = 'm' then pocetmin:=pocetmin+1;
               if(poleradek[y+1,x-0]) = 'm' then pocetmin:=pocetmin+1;
               if(poleradek[y+1,x+1]) = 'm' then pocetmin:=pocetmin+1;
               if(poleradek[y+0,x+1]) = 'm' then pocetmin:=pocetmin+1;
               if(poleradek[y-1,x+1]) = 'm' then pocetmin:=pocetmin+1;
               if(poleradek[y-1,x-0]) = 'm' then pocetmin:=pocetmin+1;

               {
               writeln('Pocet min na ',sloupecc,inradek,': ',pocetmin);
               }

               bunkanew:=chr(pocetmin+48);
               if pocetmin = 0 then
                   begin
                     poleradek[y,x]:=' ';
                   end
                   else
                   begin
                     poleradek[y,x]:=bunkanew;
                   end;
           end;
    end;


    if pocetmin = 0 then
        begin
        for ctrlp:=0 to 100 do
           begin

            for radekc:=1 to 9 do
                begin
                    for sloupecc:=1 to 9 do
                       begin
                           x:=sloupecc;
                           y:=radekc;

                           bunka:=poleradek[y,x];


                           {gotoxy(2,2);writeln('V bunce je: ',bunka);}


                           if (bunka <> 'M') and (bunka <> 'm') then
                               begin


                               if(poleradek[y-1,x-1]) = ' ' then pocetmin:=pocetmin+1;
                               if(poleradek[y-0,x-1]) = ' ' then pocetmin:=pocetmin+1;
                               if(poleradek[y+1,x-1]) = ' ' then pocetmin:=pocetmin+1;
                               if(poleradek[y+1,x-0]) = ' ' then pocetmin:=pocetmin+1;
                               if(poleradek[y+1,x+1]) = ' ' then pocetmin:=pocetmin+1;
                               if(poleradek[y+0,x+1]) = ' ' then pocetmin:=pocetmin+1;
                               if(poleradek[y-1,x+1]) = ' ' then pocetmin:=pocetmin+1;
                               if(poleradek[y-1,x-0]) = ' ' then pocetmin:=pocetmin+1;

                               if pocetmin <> 0 then
                                   begin
                                   pocetmin:=0;
                                   if(poleradek[y-1,x-1]) = 'M' then pocetmin:=pocetmin+1;
                                   if(poleradek[y-0,x-1]) = 'M' then pocetmin:=pocetmin+1;
                                   if(poleradek[y+1,x-1]) = 'M' then pocetmin:=pocetmin+1;
                                   if(poleradek[y+1,x-0]) = 'M' then pocetmin:=pocetmin+1;
                                   if(poleradek[y+1,x+1]) = 'M' then pocetmin:=pocetmin+1;
                                   if(poleradek[y+0,x+1]) = 'M' then pocetmin:=pocetmin+1;
                                   if(poleradek[y-1,x+1]) = 'M' then pocetmin:=pocetmin+1;
                                   if(poleradek[y-1,x-0]) = 'M' then pocetmin:=pocetmin+1;


                                   if(poleradek[y-1,x-1]) = 'm' then pocetmin:=pocetmin+1;
                                   if(poleradek[y-0,x-1]) = 'm' then pocetmin:=pocetmin+1;
                                   if(poleradek[y+1,x-1]) = 'm' then pocetmin:=pocetmin+1;
                                   if(poleradek[y+1,x-0]) = 'm' then pocetmin:=pocetmin+1;
                                   if(poleradek[y+1,x+1]) = 'm' then pocetmin:=pocetmin+1;
                                   if(poleradek[y+0,x+1]) = 'm' then pocetmin:=pocetmin+1;
                                   if(poleradek[y-1,x+1]) = 'm' then pocetmin:=pocetmin+1;
                                   if(poleradek[y-1,x-0]) = 'm' then pocetmin:=pocetmin+1;

                                   if pocetmin = 0 then
                                       begin
                                       poleradek[y,x]:=' ';
                                       end
                                     else
                                       begin
                                       bunkanew:=chr(pocetmin+48);
                                       poleradek[y,x]:=bunkanew;
                                       pocetmin:=0;
                                       end;
                                   end;
                               end;
                       end;
                end;
           end;
        end;
    end;


    writeln('');
    writeln('');
    writeln('');
    textcolor(10);
    writeln('        A   B   C   D   E   F   G   H   I');
    textcolor(7);
    textcolor(2);
    writeln('      +---+---+---+---+---+---+---+---+---+');
    textcolor(7);
    for radekc:=1 to 9 do
        begin

            textcolor(10);
            if radekc < 10 then
                begin
                  write('    ',(radekc));
                end
              else
                begin
                  write('   ',(radekc));
                end;
            textcolor(7);
            textcolor(2);
            write(' | ');
            textcolor(7);

                for sloupecc:=1 to 9 do
                    begin

                    bunka:=poleradek[radekc,sloupecc];
                    if (bunka = 'M') or (bunka = 'm') then
                        begin
                          if showall = 'ano' then
                             begin
                               textcolor(12);
                               write('*');
                               textcolor(7);
                             end
                           else
                             begin
                                if bunka = 'M' then
                                    begin
                                    if inobtiznost = '0' then
                                        begin
                                        write('*');
                                        end
                                      else
                                        begin
                                        write('X');
                                        end;
                                    end
                                  else
                                    begin
                                    if inobtiznost = '0' then
                                        begin
                                        textcolor(12);
                                        write('m');
                                        textcolor(7)
                                        end
                                      else
                                        begin
                                        textcolor(12);
                                        write('!');
                                        textcolor(7);
                                        end;
                                    end;
                             end;
                        end
                      else
                        begin
                          if bunka = '0' then
                              begin
                                write('X');
                              end
                            else
                              begin
                                if bunka = '!' then
                                    begin
                                    textcolor(12);
                                    write(bunka);
                                    textcolor(7);
                                    end
                                  else
                                    begin
                                    textcolor(13);
                                    write(bunka);
                                    textcolor(7);
                                    end;
                              end;
                        end;
                    textcolor(2);
                    write(' | ');
                    textcolor(7);
                    end;
                writeln('');
                textcolor(2);
                writeln('      +---+---+---+---+---+---+---+---+---+');
                textcolor(7);
        end;




textcolor(3);
gotoxy(1,1);
writeln('################################################################################');

textcolor(3);
for radekc:=1 to 24 do
    begin
    gotoxy(1,radekc);
    write('#');
    gotoxy(47,radekc);
    write('#');
    gotoxy(80,radekc);
    write('#');
    end;

gotoxy(1,25);
write('################################################################################');
textcolor(7);



if insloupec <> ' ' then
    begin

    gotoxy(50,6);
    write('Zbyva najit min: ');

    pocetmin:=0;
    for radekc:=1 to 9 do
        begin
            for sloupecc:=1 to 9 do
                begin
                   bunka:=poleradek[radekc,sloupecc];

                   if (bunka = '!') or (bunka = 'm') then
                       begin
                       pocetmin:=pocetmin+1;
                       end;
                end;
        end;

    textcolor(11);
    if inobtiznost = '2' then
        begin
        pocetmin:=27-pocetmin;
        if pocetmin < 1 then textcolor(12);
        write(pocetmin);
        write(' z 27');
        end
      else
        begin
        pocetmin:=18-pocetmin;
        if pocetmin < 1 then textcolor(12);
        write(pocetmin);
        write(' z 18');
        end;
    textcolor(7);

    gotoxy(50,8);
    write('Predchozi krok: ');
    textcolor(13);
    write(insloupec);
    write(inradek);
    textcolor(7);

    gotoxy(50,9);
    write('Probehlo pokusu: ');
    textcolor(13);
    write(pokusc);
    textcolor(7);

    end;

{test vytezstvi}
gotoxy(50,13);
if showall = 'ano' then
    begin
    textcolor(12);
    writeln('     !!! BOUCHLO TO !!!');
    textcolor(7);
    end
  else
    begin

    {Test vypisu hlasky VYHRA}
    pocetmin:=0;
    for radekc:=1 to 9 do
        begin
            for sloupecc:=1 to 9 do
                begin
                   bunka:=poleradek[radekc,sloupecc];

                   if bunka = '0' then
                       begin
                       pocetmin:=pocetmin+1;
                       end;
                end;
        end;

    {pocetmin:=0;}

    if pocetmin = 0 then
        begin
            showall:='ano';
            textcolor(27);
            writeln('        !!! VYHRA !!!');
            textcolor(7);
            end
          else
            begin
            textcolor(27);
            if insloupec = ' ' then
                begin
                textcolor(27);
                writeln('      !!! ZACINAME !!!');
                textcolor(7);
                end
              else
                begin
                textcolor(10);
                writeln('     !!! DOBRA VOLBA !!!');
                end;
                textcolor(7);
            end;
        end;


gotoxy(58,3);
textcolor(14);
write(' LOVENI MIN');
gotoxy(58,4);
write('------------');
textcolor(7);

gotoxy(49,16);
textcolor(3);
write('##############################');
textcolor(7);

gotoxy(49,18);
    textcolor(15);
    write(' Vase volba: ');
    textcolor(7);

    if showall <> 'ano' then
        begin
        gotoxy(51,20);write(' U - ukaz');
        gotoxy(51,21);write(' O - oznac/odznac');
        end;
        gotoxy(51,22);write(' R - restart (S/R)');
        gotoxy(51,23);write(' K - konec (K/Q)');

    repeat
        gotoxy(62,18);
        inukon:=readkey;
        {inradek:=ord(insloupec);}
        {writeln(insloupec,' - ',inradek);}

        if inukon = 'k' then inukon:= 'q';
        if inukon = 's' then inukon:= 'r';

        if showall = 'ano' then
            begin
            if inukon = 'u' then inukon:= ' ';
            if inukon = 'o' then inukon:= ' ';
            end;

    until (inukon = 'q') or (inukon = 'r') or (inukon = 'o') or (inukon = 'u');
    {write(inukon);}

    if inukon = 'q' then write('KONEC');
    if inukon = 'r' then write('RESTART');
    if inukon = 'o' then write('OZNAC/ODZNAC');
    if inukon = 'u' then write('UKAZ');


if inukon <> 'q' then
    begin
    if inukon = 'r' then
        begin
        hrainic;
        end
      else
        begin

        textcolor(0);
        gotoxy(51,20);write('                   ');
        gotoxy(51,21);write('                   ');
        gotoxy(51,22);write('                   ');
        gotoxy(51,23);write('                   ');
        textcolor(7);

        textcolor(15);
        gotoxy(49,20);write(' Sloupec: ');
        textcolor(7);

        repeat
            gotoxy(59,20);
            insloupec:=readkey;
            inradek:=ord(insloupec);
            {writeln(insloupec,' - ',inradek);}
        until ((inradek > 96) and (inradek < 106 ));
        write(insloupec);


        textcolor(15);
        gotoxy(49,21);
        write(' Radek: ');
        textcolor(7);

        repeat
            gotoxy(57,21);
            ch:=readkey;
            inradek:=ord(ch)-48;
            {write(ch);}
            {writeln(ch,' - ',inradek);}
        until (inradek = 1) or (inradek = 2) or (inradek = 3) or (inradek = 4) or (inradek = 5) or (inradek = 6) or (inradek = 7) or (inradek = 8) or (inradek = 9);
        write(ch);

        end;

end;

showall:='ne';
pokusc:=pokusc+1;
until inukon = 'q';


writeln('');
clrscr;
paticka;

repeat; until keypressed;

end.

