{$M 8192,0,0}
Program Gen_Suduku_Square_Varients_V1_002_By_Brad;

{Uses WinCrt;}
Uses Crt,Dos;

Type Counter = LongInt;

Var
  ADD                   : Array[1..2] of Boolean;
  F,Ncounter,
  TotalVarientCounters  : Counter;
  CH                    : Array[1..3] of Char;
  StrVarient            : Array [1..2] of String[9];
  NumberString          : Array [1..2] of String[9];
  NoZero                : Array [1..2] of Boolean;
  I                     : Integer;
  Endi                  : Boolean;
  Keypress              : Text;
  Varfile               : Array[1..3] of Text;
  ProgramName,
  CmdLine,S             : String;

Procedure OpenVarfile1;
Begin
  Assign(Varfile[1],'SuGenUp.txt');
  Rewrite(Varfile[1]);
End;

Procedure OpenVarfile2;
Begin
  Assign(Varfile[2],'SuGenDow.txt');
  Rewrite(Varfile[2]);
End;

Procedure OpenVarfile3;
Begin
  Assign(Varfile[3],'SuGenMid.txt');
  Rewrite(Varfile[3]);
End;

Procedure OpenKeypress;
Begin
  Assign(KeyPress,'Char.txt');
  Reset(Keypress);
    Readln(Keypress,Ch[1]);
  Close(Keypress);
End;

Procedure Writexy(x,y : Integer; z :String);
Begin
  Gotoxy(x,y);
  Write(z);
End;


Procedure AddNewVarient( AddType : Integer);
Var
  Line1 : String[3];
  Line2 : String[3];
  Line3 : String[3];
Begin
  Case AddType of
    1 : Begin
          Line1:=StrVarient[1][1]+StrVarient[1][2]+StrVarient[1][3];
          Line2:=StrVarient[1][4]+StrVarient[1][5]+StrVarient[1][6];
          Line3:=StrVarient[1][7]+StrVarient[1][8]+StrVarient[1][9];
          Writeln(Varfile[1],Line1);
          Writeln(VarFile[1],Line2);
          Writeln(Varfile[1],Line3);
          Writeln(Varfile[1],'-----',TotalVarientCounters);
          Inc(TotalvarientCounters);
          NoZero[1]:=False;
        End;
    2 : Begin
          Line1:=StrVarient[2][1]+StrVarient[2][2]+StrVarient[2][3];
          Line2:=StrVarient[2][4]+StrVarient[2][5]+StrVarient[2][6];
          Line3:=StrVarient[2][7]+StrVarient[2][8]+StrVarient[2][9];
          Writeln(Varfile[2],Line1);
          Writeln(VarFile[2],Line2);
          Writeln(Varfile[2],Line3);
          Writeln(Varfile[2],'-----',TotalVarientCounters);
          NoZero[2]:=False;
        End;
    3 : Begin
          IF Add[1]=True then
          Begin
            Add[1]:=False;
            Line1:=StrVarient[1][1]+StrVarient[1][2]+StrVarient[1][3];
            Line2:=StrVarient[1][4]+StrVarient[1][5]+StrVarient[1][6];
            Line3:=StrVarient[1][7]+StrVarient[1][8]+StrVarient[1][9];
            Writeln(Varfile[3],Line1);
            Writeln(VarFile[3],Line2);
            Writeln(Varfile[3],Line3);
            Writeln(Varfile[3],'-----',TotalVarientCounters);
          End;
          IF Add[2]=True then
          Begin
            Add[2]:=False;
            Line1:=StrVarient[2][1]+StrVarient[2][2]+StrVarient[2][3];
            Line2:=StrVarient[2][4]+StrVarient[2][5]+StrVarient[2][6];
            Line3:=StrVarient[2][7]+StrVarient[2][8]+StrVarient[2][9];
            Writeln(Varfile[3],Line1);
            Writeln(VarFile[3],Line2);
            Writeln(Varfile[3],Line3);
            Writeln(Varfile[3],'-----',TotalVarientCounters);
          End;
        End;
  End;
  GotoXy(1,1);
  Writeln('Combination Counter');
  Writeln;
  Writeln('Done ',TotalvarientCounters,' of 362880 possible combinations');
End;


Begin
  Repeat
    Clrscr;
    TextColor(8);
    Writeln('Suduku 3 by 3 square Varient Finder');
    Writeln('______ _ __ _ ______ _______ ______');
    Writeln;
    TextColor(7);
    Writeln('                             By Bradley Jones');
    Writeln;
    Writeln('This Program Finds all Possible combinations of a single 3 by 3 Quadrant - Joy');
    Writeln;
    Writeln('Its method is to count from 123456789 to 987654321 excluding all numbers: ');
    Writeln('           - With a Zero');
    Writeln('           - or with a repeating numeral');
    Writeln;
    Writeln;
    Writeln;
    Write('Press ');
    TextColor(5);
    Write('ANY ');
    TextColor(7);
    Write('key to generate Suduku 3 by 3 square combination varient file');
    Writeln;
    Write('Or Pres ');
    TextColor(5);
    Write('ESC ');
    TextColor(7);
    Write(' Key to Exit');
    Writeln;
    Write('Or ');
    TextColor(5);
    Write('E ');
    TextColor(7);
    Write('for Experimental Up and Down Combo Files');
    Ch[1]:=Readkey;
    ClrScr;
    Case Ord(Ch[1]) of
      27 : Begin
             Writeln('BOO and Foohey');
             Endi:=True;
           End;
      13 : Begin
             Writeln('YAY');
             Endi:=False
           End;
      69,
      101: Begin
             Endi:=False;
             If F=123456789 then
             Begin
               Writeln('Combinations have been generated within the following text file (SuGenUP.txt)');
               Writeln('Would You Like 9 copys of this File? Y/N');
               Repeat
                 Ch[2]:=Readkey;
                 Case Ord(Ch[2]) of
                   121,
                   89  : Begin
                           SwapVectors;
                             For I:=1 to 9 do
                             Begin
                               Str(I,S);
                               CmdLine := '/C Copy SuGenUP.txt '+'CFile'+S+'.Sku';
                               Exec(GetEnv('COMSPEC'), CmdLine);
                             End;
                           SwapVectors;
                           If DosError <> 0 then Writeln('Could not execute COMMAND.COM')
                           Else WriteLn('Exec successful Files Copied ','Child process exit code = ',DosExitCode);
                           Readln;
                         End;
                    110,
                    78 : Begin
                           Writeln('Ok Then');
                         End
                   Else
                   Begin
                     Writexy(10,15,'Press Y or N onley');
                   End;
                 End;
               Until (Upcase(ch[2])='Y') or (Upcase(ch[2])='N');
             End;
          End;
    End;
    If Endi=False then
    Begin
      Openvarfile1;
      If Upcase(Ch[1])='E' then
      Begin
       OpenvarFile2;
       OpenVarFile3;
      End;
      TotalVarientCounters:=0;
      F:=987654322;
      NCounter:=123456788;
      Repeat
        Dec(F);
        Inc(NCounter);
        Str(NCounter,NumberString[1]);
        Str(F,NumberString[2]);
        StrVarient[1]:=NumberString[1];
        StrVarient[2]:=NumberString[2];
        NoZero[1]:=True;
        NoZero[2]:=True;
        For I:=1 to 9 do
        Begin
          If (StrVarient[1][I]<>'0') and (NoZero[1]=True) then NoZero[1]:=True Else NoZero[1]:=False;
          If (StrVarient[2][I]<>'0') and (NoZero[2]=True) then NoZero[2]:=True Else NoZero[2]:=False;
        End;
        IF (NoZero[1]=True) and
           (StrVarient[1][1]<>StrVarient[1][2]) and
           (StrVarient[1][1]<>StrVarient[1][3]) and
           (StrVarient[1][1]<>StrVarient[1][4]) and
           (StrVarient[1][1]<>StrVarient[1][5]) and
           (StrVarient[1][1]<>StrVarient[1][6]) and
           (StrVarient[1][1]<>StrVarient[1][7]) and
           (StrVarient[1][1]<>StrVarient[1][8]) and
           (StrVarient[1][1]<>StrVarient[1][9]) and
           (StrVarient[1][2]<>StrVarient[1][3]) and
           (StrVarient[1][2]<>StrVarient[1][4]) and
           (StrVarient[1][2]<>StrVarient[1][5]) and
           (StrVarient[1][2]<>StrVarient[1][6]) and
           (StrVarient[1][2]<>StrVarient[1][7]) and
           (StrVarient[1][2]<>StrVarient[1][8]) and
           (StrVarient[1][2]<>StrVarient[1][9]) and
           (StrVarient[1][3]<>StrVarient[1][4]) and
           (StrVarient[1][3]<>StrVarient[1][5]) and
           (StrVarient[1][3]<>StrVarient[1][6]) and
           (StrVarient[1][3]<>StrVarient[1][7]) and
           (StrVarient[1][3]<>StrVarient[1][8]) and
           (StrVarient[1][3]<>StrVarient[1][9]) and
           (StrVarient[1][4]<>StrVarient[1][5]) and
           (StrVarient[1][4]<>StrVarient[1][6]) and
           (StrVarient[1][4]<>StrVarient[1][7]) and
           (StrVarient[1][4]<>StrVarient[1][8]) and
           (StrVarient[1][4]<>StrVarient[1][9]) and
           (StrVarient[1][5]<>StrVarient[1][6]) and
           (StrVarient[1][5]<>StrVarient[1][7]) and
           (StrVarient[1][5]<>StrVarient[1][8]) and
           (StrVarient[1][5]<>StrVarient[1][9]) and
           (StrVarient[1][6]<>StrVarient[1][7]) and
           (StrVarient[1][6]<>StrVarient[1][8]) and
           (StrVarient[1][6]<>StrVarient[1][9]) and
           (StrVarient[1][7]<>StrVarient[1][8]) and
           (StrVarient[1][7]<>StrVarient[1][9]) and
           (StrVarient[1][8]<>StrVarient[1][9]) then
           Begin
             Add[1]:=True;
             AddNewVarient(1);
           End;
        If (Upcase(ch[1])<>'E') and (Add[1]=True) then Add[1]:=False;
        If (UPCase(CH[1])='E') and (NoZero[2]=True) and
           (StrVarient[2][1]<>StrVarient[2][2]) and
           (StrVarient[2][1]<>StrVarient[2][3]) and
           (StrVarient[2][1]<>StrVarient[2][4]) and
           (StrVarient[2][1]<>StrVarient[2][5]) and
           (StrVarient[2][1]<>StrVarient[2][6]) and
           (StrVarient[2][1]<>StrVarient[2][7]) and
           (StrVarient[2][1]<>StrVarient[2][8]) and
           (StrVarient[2][1]<>StrVarient[2][9]) and
           (StrVarient[2][2]<>StrVarient[2][3]) and
           (StrVarient[2][2]<>StrVarient[2][4]) and
           (StrVarient[2][2]<>StrVarient[2][5]) and
           (StrVarient[2][2]<>StrVarient[2][6]) and
           (StrVarient[2][2]<>StrVarient[2][7]) and
           (StrVarient[2][2]<>StrVarient[2][8]) and
           (StrVarient[2][2]<>StrVarient[2][9]) and
           (StrVarient[2][3]<>StrVarient[2][4]) and
           (StrVarient[2][3]<>StrVarient[2][5]) and
           (StrVarient[2][3]<>StrVarient[2][6]) and
           (StrVarient[2][3]<>StrVarient[2][7]) and
           (StrVarient[2][3]<>StrVarient[2][8]) and
           (StrVarient[2][3]<>StrVarient[2][9]) and
           (StrVarient[2][4]<>StrVarient[2][5]) and
           (StrVarient[2][4]<>StrVarient[2][6]) and
           (StrVarient[2][4]<>StrVarient[2][7]) and
           (StrVarient[2][4]<>StrVarient[2][8]) and
           (StrVarient[2][4]<>StrVarient[2][9]) and
           (StrVarient[2][5]<>StrVarient[2][6]) and
           (StrVarient[2][5]<>StrVarient[2][7]) and
           (StrVarient[2][5]<>StrVarient[2][8]) and
           (StrVarient[2][5]<>StrVarient[2][9]) and
           (StrVarient[2][6]<>StrVarient[2][7]) and
           (StrVarient[2][6]<>StrVarient[2][8]) and
           (StrVarient[2][6]<>StrVarient[2][9]) and
           (StrVarient[2][7]<>StrVarient[2][8]) and
           (StrVarient[2][7]<>StrVarient[2][9]) and
           (StrVarient[2][8]<>StrVarient[2][9]) then
        Begin
          Add[2]:=True;
          AddNewVarient(2);
          If TotalVarientCounters>181440 then AddNewVarient(3); {its adding early more combos on one side}
        End;
      Until (F=123456789);
      Close(VarFile[1]);
      Close(VarFile[2]);
      Close(VarFile[3]);
    End;
  Until (Endi=True) or (NCounter=987654321);
End.
