program pisqworx;

uses crt;

const XS=10; YS=10;
const MINLEN=5;
const MAXPLAYERS=5;
const LOOKS: string='OXLTH';

var plocha: array[1..YS, 1..XS] of char;
var win: boolean;
    curplayer: integer;
    i, j, k: integer;

procedure drawBoard;
begin
     clrscr;
     for i:=1 to XS do
         write(' ', chr(ord('A')+i-1));
     writeln;

     write('/');
     for i:=1 to XS*2-1 do
         write('-');
     writeln('\');

     for j:=1 to YS do begin
         write('|');
         for i:=1 to XS do begin
             write(plocha[j, i], '|');
         end;
         writeln(' ', chr(ord('A')+j-1));
     end;

     write('\');
     for i:=1 to XS*2-1 do
         write('-');
     writeln('/');
end;

procedure makeMove;
var move: string;
    x, y: integer;
label re;
begin
re:
     writeln('Please enter the move player ', LOOKS[curplayer], ' (first X, then Y)');
     readln(move);
     if (move='QUIT') then
        win:=true
     else begin
          x:=ord(move[1])-ord('A')+1;
          y:=ord(move[2])-ord('A')+1;
          if (x<1) OR (x>XS) OR (y<1) OR (y>YS) OR (plocha[y, x]<>' ') then begin
             writeln('Bad move, try again!');
             goto re;
          end else begin
              plocha[y, x]:=LOOKS[curplayer];
          end;
     end;
end;

function checkWin:char;
var f: boolean;
    c: char;
label stop;
begin
     {we only have to check directions
         ->      |     \           /
                 V       X       X
     }

     {1}
     for j:=1 to YS do begin
         for i:=1 to XS-MINLEN+1 do begin
             f:=true;
             c:=plocha[j, i];
             if (c=' ')
                then continue;
             for k:=1 to MINLEN-1 do begin
                 if (plocha[j, i+k]<>c)
                    then f:=false;
             end;

             if (f=true) then begin
                checkWin:=c;
                win:=true;
                goto stop;
             end;
         end;
     end;

     {2}
     for j:=1 to YS-MINLEN+1 do begin
         for i:=1 to XS do begin
             f:=true;
             c:=plocha[j, i];
             if (c=' ')
                then continue;
             for k:=1 to MINLEN-1 do begin
                 if (plocha[j+k, i]<>c)
                    then f:=false;
             end;

             if (f=true) then begin
                checkWin:=c;
                win:=true;
                goto stop;
             end;
         end;
     end;
     {3}
     for j:=1 to YS-MINLEN+1 do begin
         for i:=1 to XS-MINLEN+1 do begin
             f:=true;
             c:=plocha[j, i];
             if (c=' ')
                then continue;
             for k:=1 to MINLEN-1 do begin
                 if (plocha[j+k, i+k]<>c)
                    then f:=false;
             end;

             if (f=true) then begin
                checkWin:=c;
                win:=true;
                goto stop;
             end;
         end;
     end;

     {4}
     for j:=1 to YS-MINLEN+1 do begin
         for i:=MINLEN-1 to XS do begin
             f:=true;
             c:=plocha[j, i];
             if (c=' ')
                then continue;
             for k:=1 to MINLEN-1 do begin
                 if (plocha[j+k, i-k]<>c)
                    then f:=false;
             end;

             if (f=true) then begin
                checkWin:=c;
                win:=true;
                goto stop;
             end;
         end;
     end;
     checkWin:=' ';
stop:
end;

begin
     win:=false;
     curplayer:=1;

     for i:=1 to YS do begin
         for j:=1 to XS do begin
             plocha[i, j]:=' ';
         end;
     end;

     repeat
           drawBoard;
           makeMove;
           checkWin;
           curplayer:=curplayer+1;
           if (curplayer>MAXPLAYERS)
              then curplayer:=1;
     until win;
     drawBoard;
     if (checkWin<>' ') then
          writeln(checkWin, ' player won!')
     else writeln('Game was interrupted!');
     readkey;
end.
