program NumberSquares(input,output);
(*
  NUMBER SQUARE GAME IN TURBO-PASCAL
  AS OF BASIC VER 4.0 - 12 NOV 79
  BY MARC I. LEAVEY, M.D.

  Adapted for the JOYCE PCW machine by W.Cirsovius
*)

type
  field        = array[1..4,1..4] of integer;
  Mstr         = string[80];
const
  Magic        : field = ((1,6,15,8),
                          (12,11,2,5),
                          (10,13,4,3),
                          (7,16,9,14));
  emptyloc     = 16;
var
  EndGame,
  TotalEnd     : boolean;
  Pleasure     : char;
  Board        : field;
  selrow,selcol: integer;

procedure InKey(var ch:char);
Begin
  repeat until keypressed;
  read(kbd,ch);
  if ch=^C then
  begin
    clrscr;
    writeln;
    writeln;
    writeln('** Abort **');
    halt;
  end;
  ch:=upcase(ch);
End;

procedure Greeting;
Begin
  clrscr;
  write(chr($1b),'r');
  writeln('N U M B E R   S Q U A R E S');
  write(chr($1b),'u');
  writeln;
  writeln('WELCOME TO THE WORLD OF CONFUSION.');
  writeln('THERE ARE TWO VERSIONS OF NUMBER SQUARES:');
  writeln(' 1 - SEQUENTIAL');
  writeln(' 2 - MAGIC SQUARE');
End;

procedure SetupBoard;
var
  row,col      : integer;

  procedure SetMagic;
  (* SET UP MAGIC SQUARE BOARD *)
  begin
    Board:=Magic;
    selrow:=4;
    selcol:=2;
  end;

  procedure SetSequential;
  (* SET UP SEQUENTIAL BOARD *)
  begin
    for row:=1 to 4 do
      for col:=1 to 4 do
        Board[row,col]:=pred(row)*4+col;
    selrow:=4;
    selcol:=4;
  end;

Begin (* SetupBoard *)
  repeat
    gotoxy(1,10);
    clreol;
    write('WHICH IS YOUR PLEASURE ');
    InKey(Pleasure);
  until Pleasure in ['1','2'];
  case Pleasure of
    '1' : SetSequential;
    '2' : SetMagic;
  end;
End;

procedure ScrambleBoard;
(* NOW SCRAMBLE THE BOARD TWO HUNDRED TIMES *)
var
  match         : boolean;
  Scramb,dir    : integer;

  procedure GoDown;
  begin
    Board[selrow,selcol]:=Board[pred(selrow),selcol];
    Board[pred(selrow),selcol]:=emptyloc;
    selrow:=pred(selrow);
  end;

  procedure GoUp;
  begin
    Board[selrow,selcol]:=Board[succ(selrow),selcol];
    Board[succ(selrow),selcol]:=emptyloc;
    selrow:=succ(selrow);
  end;

  procedure GoLeft;
  begin
    Board[selrow,selcol]:=Board[selrow,pred(selcol)];
    Board[selrow,pred(selcol)]:=emptyloc;
    selcol:=pred(selcol);
  end;

  procedure GoRight;
  begin
    Board[selrow,selcol]:=Board[selrow,succ(selcol)];
    Board[selrow,succ(selcol)]:=emptyloc;
    selcol:=succ(selcol);
  end;

Begin (* ScrambleBoard *)
  gotoxy(1,10);
  clreol;
  writeln('I AM NOW SCRAMBLING THE BOARD...');
  for Scramb:=1 to 200 do
  begin
    repeat
      dir:=succ(random(4));
      case dir of
        1 : match:=selrow=1;
        2 : match:=selrow=4;
        3 : match:=selcol=1;
        4 : match:=selcol=4;
      end;
    until not match;
    case dir of
      1 : GoDown;
      2 : GoUp;
      3 : GoLeft;
      4 : GoRight;
    end;
  end;
End;

procedure PrintBoard;
var
  i,j          : integer;
  function PrNum:Mstr;
  var
    piece      : integer;
    myNum      : Mstr;
  begin
    piece:=Board[i,j];
    j:=succ(j);
    if j=5 then
    begin
      i:=succ(i);
      j:=1;
    end;
    if piece=emptyloc then myNum:='  '
                      else str(piece:2,myNum);
    PrNum:=chr($85)+myNum;
  end;

Begin (* PrintBoard *)
  clrscr;
  i:=1;
  j:=1;
  writeln(chr($86),chr($8a),chr($8a),
          chr($8e),chr($8a),chr($8a),
          chr($8e),chr($8a),chr($8a),
          chr($8e),chr($8a),chr($8a),chr($8c));
  writeln(PrNum,PrNum,PrNum,PrNum,chr($85));
  writeln(chr($87),chr($8a),chr($8a),
          chr($8f),chr($8a),chr($8a),
          chr($8f),chr($8a),chr($8a),
          chr($8f),chr($8a),chr($8a),chr($8d));
  writeln(PrNum,PrNum,PrNum,PrNum,chr($85));
  writeln(chr($87),chr($8a),chr($8a),
          chr($8f),chr($8a),chr($8a),
          chr($8f),chr($8a),chr($8a),
          chr($8f),chr($8a),chr($8a),chr($8d));
  writeln(PrNum,PrNum,PrNum,PrNum,chr($85));
  writeln(chr($87),chr($8a),chr($8a),
          chr($8f),chr($8a),chr($8a),
          chr($8f),chr($8a),chr($8a),
          chr($8f),chr($8a),chr($8a),chr($8d));
  writeln(PrNum,PrNum,PrNum,PrNum,chr($85));
  writeln(chr($83),chr($8a),chr($8a),
          chr($8b),chr($8a),chr($8a),
          chr($8b),chr($8a),chr($8a),
          chr($8b),chr($8a),chr($8a),chr($89));
End;

procedure StatusMessage(msg:Mstr);
Begin
  gotoxy(1,15);
  clreol;
  write(msg);
End;

procedure TellInvalid(msg:Mstr);
Begin
  StatusMessage(msg);
  delay(2000);
End;

procedure InputMove;
var
  flag,
  loop         : boolean;
  myDig,
  MyRow,MyCol,
  CurRow,CurCol: integer;

  function FindNumber:boolean;
  (* Search for selected number - returns TRUE if match *)
  begin
    selrow:=0;
    selcol:=0;
    for CurRow:=1 to 4 do
    begin
      for CurCol:=1 to 4 do
      begin
        if Board[CurRow,CurCol]=myDig then
        begin
          selrow:=CurRow;
          selcol:=CurCol;
        end;
      end;
    end;
    if selrow=0 then TellInvalid('I CAN''T FIND THAT NUMBER');
    FindNumber:=selrow<>0;
  end;

  function TestValidMove:boolean;
  (* Search for empty location - returns TRUE if empty *)
  var
    empty      : boolean;
  begin
    MyRow:=0;
    MyCol:=0;
    empty:=false;
    for CurRow:=pred(selrow) to succ(selrow) do
      if CurRow in [1..4] then
        if Board[CurRow,selcol]=emptyloc then
      begin
        MyRow:=CurRow;
        MyCol:=selcol;
        empty:=true;
      end;
    if not empty then
    begin
      for CurCol:=pred(selcol) to succ(selcol) do
        if CurCol in [1..4] then
          if Board[selrow,CurCol]=emptyloc then
        begin
          MyRow:=selrow;
          MyCol:=CurCol;
          empty:=true;
        end;
    end;
    if not empty then TellInvalid('NOT A VALID MOVE');
    TestValidMove:=empty;
  end;

  procedure FindSequential;
  (* Test sequential solved *)
  var
    row,col,
    tile       : integer;
  begin
    tile:=0;
    loop:=true;
    for row:=1 to 4 do
      for col:=1 to 4 do
      begin
        if Board[row,col]<tile then loop:=false;
        if loop then tile:=Board[row,col];
      end;
      EndGame:=loop;
      if EndGame then StatusMessage('YOU GOT IT!');
  end;

  procedure FindMagic;
  (* Test magic solved *)
  begin
    loop:=true;
    for CurRow:=1 to 4 do
      for CurCol:=1 to 4 do
        if Board[CurRow,CurCol]<>Magic[CurRow,CurCol] then loop:=false;
    EndGame:=loop;
    if EndGame then StatusMessage('THAT IS THE CORRECT SOLUTION!');
  end;

  procedure Numin(var num:integer);
  var
    digit,
    endline    : boolean;
    Ms         : char;
    dig        : integer;

    procedure Digin;
    begin
      endline:=false;
      digit:=false;
      InKey(Ms);
      if (Ms=^M) then endline:=true
      else begin
             write(Ms);
             dig:=ord(Ms)-ord('0');
             if (dig in [0..9]) then digit:=true
             else TellInvalid('** Invalid input - retry **');
           end;
    end;

  begin (* Numin *)
    repeat
      StatusMessage('MOVE WHICH PIECE ');
      num:=0;
      repeat
        repeat
          Digin;
        until endline or digit;
        if digit then num:=10*num+dig;
      until endline;
      writeln;
    until num in [1..15];
  end;

Begin (* InputMove *)
  repeat
    Numin(myDig);
    flag:=FindNumber;
    if flag then flag:=TestValidMove;
  until flag;
  Board[MyRow,MyCol]:=myDig;
  Board[selrow,selcol]:=emptyloc;
  gotoxy((3*selcol)-1,2*selrow);
  write('  ');
  gotoxy((3*MyCol)-1,2*MyRow);
  write(myDig:2);
  case Pleasure of
    '1' : FindSequential;
    '2' : FindMagic;
  end;
End;

BEGIN (* MAIN *)
  EndGame:=false;
  TotalEnd:=false;
  repeat
    Greeting;
    SetupBoard;
    ScrambleBoard;
    PrintBoard;
    repeat
      InputMove;
    until EndGame;
    gotoxy(1,16);
    clreol;
    writeln('THAT IS THE CORRECT SOLUTION!');
    write('LIKE TO PLAY ANOTHER GAME ');
    InKey(Pleasure);
    TotalEnd:=Pleasure<>'Y';
  until TotalEnd;
END.

