Program Gen_Suduku_Square_Varients_V1_003b_By_Brad;

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;


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 PutStartScreen;
Begin
  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');
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);
          NoZero[1]:=False;
          If (Upcase(ch[1])<>'E') then Add[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-181439);
          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-181440);
          End;
        End;
  End;
  GotoXy(1,1);
  Writeln('Combination Counter');
  Writeln;
  Writeln('Done ',TotalvarientCounters,' of 362880 possible combinations');
End;


Begin
  Repeat
    Clrscr;
      PutStartScreen;
    ClrScr;
    Ch[1]:=Readkey;
    Case Ord(Ch[1]) of
      27 : Begin
             Writeln('BOO and Foohey');
             Endi:=True;
           End;
      13 : Begin
             Writeln('YAY');
             Endi:=False;
           End;
    End;
    If (Upcase(Ch[1])='E') then
    Begin
      OpenvarFile2;
      OpenVarFile3;
      Endi=False;
    End;
    If Endi=False then
    Begin
      Openvarfile1;
      TotalVarientCounters:=0;
      F:=987654321;
      NCounter:=123456789;
      Repeat       
        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);
             Inc(TotalvarientCounters);
           End;
        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>181439 then AddNewVarient(3); 
        End;
        Dec(F);
        Inc(NCounter);
	IF (TotalvarientCounters=362880) then Endi=True;
      Until Endi=True;
      Close(VarFile[1]);
      Close(VarFile[2]);
      Close(VarFile[3]);
    End
  Until Endi=True;
End.
