program fleet;                                  {0 - пустой}
uses crt;
const
    SHIP_NUM = 4;
var                                             {1 - недоступный невидимый}
    field:array[1..25,1..14] of shortint;       {2 - недоступный видимый}
    i,j,pl:shortint;                            {3 - корабль}
    dist:array[1..4,1..2] of shortint;          {4 - подбитый}
    ships:array[1..SHIP_NUM] of shortint;       {5 - выстрел}
    ships_alive:array[1..SHIP_NUM,1..2] of shortint;
    IsHumanTurn:boolean;
    
function placeShip(x,y,size,dis,pl:shortint):boolean;
var i:shortint;
    res:boolean;
    nx,ny:shortint;
    d1,d2:shortint;
begin
    res:=TRUE;
    for i:=1 to size do begin
        nx:=x+dist[dis,1]*(i-1)+2+11*(pl-1);
        ny:=y+dist[dis,2]*(i-1)+2;
        if field[nx,ny] > 0 then begin
                res:= FALSE; break; end;
    end;
    if res then begin
        for i:=1 to size do begin
            nx:=x+dist[dis,1]*(i-1)+2+11*(pl-1);
            ny:=y+dist[dis,2]*(i-1)+2;
            d1:=dis-1; if d1=0 then d1:=4;
            d2:=dis+1; if d2=5 then d2:=1;
            field[nx+dist[d1,1],ny+dist[d1,2]]:=1;
            field[nx+dist[d2,1],ny+dist[d2,2]]:=1;
            field[nx,ny]:=3;
        end;

        nx:=x-dist[dis,1]+2+11*(pl-1);
        ny:=y-dist[dis,2]+2;
        field[nx+dist[d1,1],ny+dist[d1,2]]:=1;
        field[nx+dist[d2,1],ny+dist[d2,2]]:=1;
        field[nx,ny]:=1;
        
        nx:=x+dist[dis,1]*size+2+11*(pl-1);
        ny:=y+dist[dis,2]*size+2;
        field[nx+dist[d1,1],ny+dist[d1,2]]:=1;
        field[nx+dist[d2,1],ny+dist[d2,2]]:=1;
        field[nx,ny]:=1;
        
    end;
    placeShip:=res;
end;

procedure refreshVisible;
var i,j:shortint;
begin
    for i:=3 to 23 do
        for j:=3 to 12 do
            if field[i,j]=4 then begin
                field[i+1,j+1]:=2;
                field[i+1,j-1]:=2;
                field[i-1,j+1]:=2;
                field[i-1,j-1]:=2;
            end;
end;

function makeShot(x,y,pl:shortint):boolean;
var nx,ny:shortint;
begin
    nx:=x+2+11*(2-pl);
    ny:=y+2;
    if (field[nx,ny] = 3) OR (field[nx,ny] = 4) then begin field[nx,ny]:=4; makeShot:=TRUE; refreshVisible end
        else begin field[nx,ny]:=5; makeShot:=FALSE end;
end;

procedure drawField;
var i,j:shortint;
begin
    clrscr;
    for j:=1 to 10 do begin
        for i:=1 to 10 do
            case field[i+2,j+2] of
                3: write('0');
                4: write('X');
                5: write('*');
                else write(' ');                
            end;
        
        write('|'); if j<10 then write(' ');
        write(j,'|');
        for i:=1 to 10 do
            case field[i+13,j+2] of
                3: write(' ');
                4: write('X');
                5: write('*');
                else write(' ');
            end;
        writeln;
    end;
    writeln('1234567890    1234567890');
end;

function makeHumanTurn:boolean;
var x,y:shortint;
begin
    drawfield;
    write('Coordinats:'); read(x,y);
    makeHumanTurn:=makeShot(x,y,1);
end;

function testVictory:boolean;
var i,j,pl:shortint;
    res1,res2:boolean;
begin
    res1:=TRUE;res2:=TRUE;
    for pl:=1 to 2 do
        for i:=(3+11*(pl-1)) to (12+11*(pl-1)) do
            for j:=3 to 11 do
                if field[i,j]=3 then if pl=1 then res2:=FALSE
                                            else res1:=FALSE;
    if res1 then writeln('Player 1 vin');   
    if res2 then writeln('Player 2 vin');
    testVictory:= res1 OR res2;
end;

function makeAiTurn:boolean;
var x,y:shortint;
begin
    repeat begin x:=RANDOM(10)+1; y:=RANDOM(10)+1 end;
        until NOT ( (field[x+2,y+2]=4) OR  (field[x+2,y+2]=5) OR  (field[x+2,y+2]=2));
    makeAiTurn:= NOT makeShot(x,y,2);
end;
 
begin
    randomize;
{-------------------------------INITIALIZING------------------------------------------}
    for i:=1 to 25 do
        for j:=1 to 2 do begin
                field[i,j]:=1;
                field[i,12+j]:=1;
        end;
    
    for i:=1 to 2 do
        for j:=3 to 12 do begin
            field[i,j]:=1;
            field[23+i,j]:=1;
            field[13,j]:=1;
        end;

    dist[1,1]:= 0;dist[1,2]:= 1;                {вверх}
    dist[2,1]:= 1;dist[2,2]:= 0;                {вправо}
    dist[3,1]:= 0;dist[3,2]:=-1;                {влево}
    dist[4,1]:=-1;dist[4,2]:= 0;                {вниз}
    
    ships[1]:=4;
    ships[2]:=3;
    ships[3]:=2;
    ships[4]:=1;
    
    IsHumanTurn:=TRUE;
    
    for i:=1 to SHIP_NUM do begin
        ships_alive[i,1]:=ships[i];ships_alive[i,2]:=ships[i];
    end;
{-------------------------SHIP PLACEMENT--------------------------------------------}
    for pl:=1 to 2 do
        for i:=SHIP_NUM downto 1 do
            for j:=1 to ships[i] do
                repeat until placeShip(RANDOM(10)+1,RANDOM(10)+1,i,RANDOM(4)+1,pl);
{-----------------------------------------------------------------------------------}    
    {placeShip(1,1,4,3,1);}
    
    repeat if IsHumanTurn then IsHumanTurn:=makeHumanTurn
                          else IsHumanTurn:=makeAiTurn;
        until testVictory;readkey;
    
{    for j:=14 downto 1 do begin
        for i:=1 to 25 do
            write(field[i,j]);
        writeln;
    end;}
end.