Battleships

by Lewis Wright: L6 Age ~16

Introduction

This is a most impressive implementation of a challenging programming task and shows what can be achieved during the first year of programming. A web preview is now available.

You start by positioning five ships on your 12 x 12 grid. Read the location instructions carefully or you might be cautioned, "Look Captain, the rules are fairly simple - X Y Dir - not side by side!". If you make too many mistakes, the program will exit. The computer at random puts five ships on its own grid. You select a coordinate to attack and are informed if it was a hit or a miss, then the computer attacks your navy. Check the code to confirm that the computer does not use the positions you have input to cheat! The aim is to hit all of the squares occupied by computer ships before the computer hits all of yours. There are instructions, which you can choose to skip.

We must admit to having tested this program, for your benefit of course! You might like to extend the validation and to make the program output summaries of the current position from time to time.

Extract of Gameplay

The following extract of computer output gives a flavour of the game.

The Computer is firing sir! They have pulverised position 7 11
They have HIT us captain! We are already hearing reports of virtual casualties.
They SUNK Our Submarine!
Here are the places where the computer has attacked
  123456789...
1 MOMOOOMOHOMO
2 OOOOOOOOHOOO
3 OOOOOMOOHOOO
4 OOMOOMOMHMOO
5 OOMOOOOOHOOO
6 MOOOOOOOMOOO
7 MOOHMOOOOMOO
8 MMMHMOOHHHMO
9 OOOMMOOOMOOO
. MMOOOOOOOMMO
. OMOMOOHHHMOO
. OOOOOOOOOMMO
Here are the places you have attacked thus far
  123456789...
1 OOOOOMOOOOOO
2 OOOOOOMOMMOO
3 OOOHMOOMHOOO
4 OOMHOOOOMOOO
5 MOOMOOMHHHHH
6 OMMHOOOOOOOO
7 OOMHOMOMOOOO
8 OOOOMMOMMOOO
9 OOOMMMHHHMOO
. OOMMOOMOOMOO
. OMOOOMOOOOOO
. OOOOMOOOOOOO
Input attack longitude (X) captain!
10
Input attack latitude (Y) captain!
3
Great Aiming Captain - We HIT them!
.
.
.
      

The Program

program Battleships;
{$APPTYPE CONSOLE}

{
    Copyright (c) 2010 Lewis Wright
    
    Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    use this file except in compliance with the License, as described at
    http://www.apache.org/licenses/ and http://www.pp4s.co.uk/licenses/
}

uses
  SysUtils,
  strutils;
var
  attacked, ships, compships, compattacked : array[0..13,0..13] of char;
  tempy,errorcheck11,intro, hitr,count5555,invalidcount, hitrx,hitry, longhitrx,
  longhitry,misscheck,tempx,attx,atty,cattx,catty, count3, count4, sunk, compsunk : integer;
  direction, dirr, firstfound : char;
  hitships, comphitships : array[1..5,1..6] of integer;
  strd, charx,chary : string;

procedure inputships(noofspaces : integer; identifier : char);
var
  valid : boolean;
  count, count999 : integer;
begin
  repeat
    valid := true;
    direction := ' ';
    read(tempx,tempy,strd);
    direction := strd[2];

    case direction of     
    'R' : if tempx + noofspaces - 1 > 12 then valid := false;
    'L' : if tempx - noofspaces + 1 < 1 then valid := false;
    'U' : if tempy - noofspaces + 1 < 1 then valid := false;
    'D' : if tempy + noofspaces - 1 > 12 then valid := false;
    end;
    if (direction <> 'R') AND(direction <> 'L') AND(direction <> 'D') AND(direction <> 'U') then
    begin
      valid := false;
    end;

    for count := 1 to noofspaces do
      begin
        if direction = 'R' then
          begin
             if ships[tempx+count,tempy] <> 'O' then
               begin
                 valid := false;
               end;
          end;
        if direction = 'L' then
          begin
            if ships[tempx-count,tempy] <> 'O' then
              begin
                valid := false;
              end;
          end;
        if direction = 'U' then
          begin
            if ships[tempx,tempy-count] <> 'O' then
              begin
                valid := false;
              end;
            end;
        if direction = 'D' then
          begin
            if ships[tempx,tempy+count] <> 'O' then
              begin
                valid := false;
              end;
          end;
       end;

       for count999 := 0 to noofspaces - 1 do
          begin
            case direction of
            'U' :
                begin
                  if (ships[tempx,tempy-count999+1] <> 'O') AND(ships[tempx,tempy-count999+1] <> identifier)
                      OR (ships[tempx,tempy-count999-1] <> 'O') AND (ships[tempx,tempy-count999-1] <> identifier)
                      OR(ships[tempx-1,tempy-count999] <> 'O') AND (ships[tempx-1,tempy-count999] <> identifier)
                      OR(ships[tempx+1,tempy-count999] <> 'O') AND (ships[tempx+1,tempy-count999] <> identifier)then
                    begin
                      valid := false;
                    end;
                end;
            'L' :
                begin
                  if (ships[tempx-count999,tempy+1] <> 'O') AND (ships[tempx-count999,tempy+1] <> identifier)
                     OR (ships[tempx-count999,tempy-1] <> 'O') AND (ships[tempx-count999,tempy-1] <> identifier)
                     OR (ships[tempx-1-count999,tempy] <> 'O') AND (ships[tempx-1-count999,tempy] <> identifier)
                     OR (ships[tempx+1-count999,tempy] <> 'O') AND (ships[tempx+1-count999,tempy] <> identifier)then
                    begin
                      valid := false;
                    end;
                end;
            'R' :
                begin
                  if (ships[tempx+count999,tempy+1] <> 'O') AND (ships[tempx+count999,tempy+1] <> identifier)
                     OR (ships[tempx+count999,tempy-1] <> 'O')AND(ships[tempx+count999,tempy-1] <> identifier)
                     OR (ships[tempx+count999-1,tempy] <> 'O') AND (ships[tempx+count999-1,tempy] <> identifier)
                     OR (ships[tempx+1+count999,tempy] <> 'O') AND (ships[tempx+1+count999,tempy] <> 'O') then
                    begin
                      valid := false;
                    end;
                end;
            'D' :
                begin
                  if (ships[tempx,tempy+count999+1] <> 'O') AND (ships[tempx,tempy+count999+1] <> identifier)
                     OR (ships[tempx,tempy+count999-1] <> 'O') AND (ships[tempx,tempy+count999-1] <> identifier)
                     OR (ships[tempx-1,tempy+count999] <> 'O') AND (ships[tempx-1,tempy+count999] <> identifier)
                     OR (ships[tempx+1,tempy+count999] <> 'O') AND (ships[tempx+1,tempy+count999] <> identifier) then
                    begin
                      valid := false;
                    end;
                end;
            end;
          end;
      if valid = false then
        begin
          if invalidcount = 3 then
            begin
              sleep(3000);
              writeln('"Know what Captain? I think I will take the lead on this one."');
              writeln;
              sleep(4000);
              writeln('"Too many lives at stake for messing about"');
              writeln;
              sleep(4000);
              writeln;
              writeln('"And as Captain I now have power over you - have fun in the brig *COLONEL*"');
              sleep(3000);
              readln;
              halt;
            end;

          if invalidcount = 2 then
            begin
              sleep(3000);
              writeln('"Are you sure you are alright Captain? Maybe you should take a walk..."');
              sleep(1000);
              writeln;
              writeln('*off the side of the boat*');
              writeln;
              inc(invalidcount);
            end;

          if invalidcount = 1 then
            begin
              sleep(3000);
              writeln('"Look Captain, the rules are fairly simple - X Y Dir - not side by side!"');
              writeln;
              sleep(1000);
              writeln('"TRY HARDER!"');
              writeln;
              inc(invalidcount);
            end;

          if invalidcount = 0 then
            begin
              sleep(3000);
              writeln('"Err... maybe you would like another go at that one Captain..." *SIGH*');
              writeln;
              inc(invalidcount);
            end;
          end;
  until valid = true;
  for count := 1 to noofspaces do
    begin
      ships[tempx,tempy] := identifier;
      case direction of
        'R' : inc(tempx);
        'L' : dec(tempx);
        'U' : dec(tempy);
        'D' : inc(tempy);
      end;
    end;
end;

procedure printships;
var
  count2: integer;
begin
  writeln('  123456789...');
  for count2 := 1 to 12 do
    begin
      if count2 < 10 then
      begin
        writeln(count2,' ',ships[1,count2],ships[2,count2],ships[3,count2],ships[4,count2],ships[5,count2],
        ships[6,count2],ships[7,count2],ships[8,count2],ships[9,count2],ships[10,count2],ships[11,count2],ships[12,count2]);
      end
      else
      begin
        writeln('. ',ships[1,count2],ships[2,count2],ships[3,count2],ships[4,count2],ships[5,count2],ships[6,count2],
        ships[7,count2],ships[8,count2],ships[9,count2],ships[10,count2],ships[11,count2],ships[12,count2]);
      end;
    end;
end;

procedure printcompships;
begin
  writeln('  123456789...');
  for count3 := 1 to 12 do
    begin
      if count3 < 10 then
        begin
          writeln(count3,' ',compships[1,count3],compships[2,count3],compships[3,count3],compships[4,count3],
          compships[5,count3],compships[6,count3],compships[7,count3],compships[8,count3],compships[9,count3],
          compships[10,count3],compships[11,count3],compships[12,count3])
        end
      else
        begin
          writeln('. ',compships[1,count3],compships[2,count3],compships[3,count3],compships[4,count3],
          compships[5,count3],compships[6,count3],compships[7,count3],compships[8,count3],compships[9,count3],
          compships[10,count3],compships[11,count3],compships[12,count3])
        end;
    end;
end;

procedure printattacked;
begin
  writeln('Here are the places you have attacked thus far');
  sleep(1000);
  writeln('  123456789...');
  for count3 := 1 to 12 do
    begin
      if count3 < 10 then
        begin
          writeln(count3,' ',attacked[1,count3],attacked[2,count3],attacked[3,count3],attacked[4,count3],
          attacked[5,count3],attacked[6,count3],attacked[7,count3],attacked[8,count3],attacked[9,count3],
          attacked[10,count3],attacked[11,count3],attacked[12,count3])
        end
      else      
        begin
          writeln('. ',attacked[1,count3],attacked[2,count3],attacked[3,count3],attacked[4,count3],
          attacked[5,count3],attacked[6,count3],attacked[7,count3],attacked[8,count3],attacked[9,count3],
          attacked[10,count3],attacked[11,count3],attacked[12,count3])
        end;
    end;
end;

procedure printcompattacked;
begin
  writeln('  123456789...');
  for count3 := 1 to 12 do
    begin
      if count3 < 10 then
        begin
          writeln(count3,' ',compattacked[1,count3],compattacked[2,count3],compattacked[3,count3],
          compattacked[4,count3],compattacked[5,count3],compattacked[6,count3],compattacked[7,count3],
          compattacked[8,count3],compattacked[9,count3],compattacked[10,count3],compattacked[11,count3],
          compattacked[12,count3])
        end
      else
        begin
          writeln('. ',compattacked[1,count3],compattacked[2,count3],compattacked[3,count3],
          compattacked[4,count3],compattacked[5,count3],compattacked[6,count3],compattacked[7,count3],
          compattacked[8,count3],compattacked[9,count3],compattacked[10,count3],compattacked[11,count3],
          compattacked[12,count3])
        end;
    end;
end;

procedure humanturn;
var
  hitcount, done : integer;
begin
  sleep(1000);
  printattacked;
  sleep(1000);
  writeln('Input attack longitude (X) captain!');
  readln(attx);
  writeln('Input attack latitude (Y) captain!');
  readln(atty);

  if compships[attX,attY] <> 'O' then
  begin
    writeln('Great Aiming Captain - We HIT them!');
    sleep(1000);
    attacked[attX,attY] := 'H';
    case compships[attX,attY] of
    'A' :
        begin
          hitcount := 0;
          done := 0;
          repeat
            inc(hitcount);
            if hitships[1,hitcount] = 0 then
              begin
                hitships[1,hitcount] := 1;
                done := 1;
              end;
            if hitships[1,hitcount+1] = -1 then        
              begin
                sleep(1000);
                writeln('We SUNK Their Aircraft Carrier! Good work men!');
                inc(sunk);
                done := 1;
              end;
          until done = 1;
        end;
    'B' :
        begin
          hitcount := 0;
          done := 0;
          repeat
            inc(hitcount);
            if hitships[2,hitcount] = 0 then
              begin
                hitships[2,hitcount] := 1;
                done := 1;
              end;
            if hitships[2,hitcount+1] = -1 then
              begin
                sleep(1000);
                writeln('We SUNK Their Battleship! Good work men!');
                inc(sunk);
                done := 1;
              end;
          until done = 1;
        end;
    'C' :
        begin
          hitcount := 0;
          done := 0;
          repeat
            inc(hitcount);
            if hitships[3,hitcount] = 0 then
              begin
                hitships[3,hitcount] := 1;
                done := 1;
              end;
            if hitships[3,hitcount+1] = -1 then
              begin
                sleep(1000);
                writeln('We SUNK Their Cruiser! Good work men!');
                inc(sunk);
                done := 1;
              end;
          until done = 1;
        end;
    'S' :
        begin
          hitcount := 0;
          done := 0;
          repeat
            inc(hitcount);
            if hitships[4,hitcount] = 0 then
              begin
                hitships[4,hitcount] := 1;
                done := 1;
              end;
            if hitships[4,hitcount+1] = -1 then
              begin
                sleep(1000);
                writeln('We SUNK Their Submarine! Good work men!');
                inc(sunk);
                done := 1;
              end;
          until done = 1;
        end;
    'D' :
        begin
          hitcount := 0;
          done := 0;
          repeat
            inc(hitcount);
            if hitships[5,hitcount] = 0 then
              begin
                hitships[5,hitcount] := 1;
                done := 1;
              end;
            if hitships[5,hitcount+1] = -1 then
              begin
                sleep(1000);
                writeln('We SUNK Their Destroyer! Good work men!');
                inc(sunk);
                done := 1;
              end;
          until done = 1;
        end;
    end;
  end
  else
    begin
      writeln('MISSED');
      sleep(1000);
      attacked[attX,attY] := 'M';
    end;
end;

procedure compturn;
var
  hitcount2, done2 : integer;
begin
  if hitr = 0 then
    begin
      randomize;
      repeat
        cattX := Random(12) + 1;
        cattY := Random(12) + 1;
      until (compattacked[cattX,cattY] = 'O') AND (compattacked[cattX-1,cattY] <> 'H') AND
        (compattacked[cattX,cattY-1] <> 'H') AND (compattacked[cattX+1,cattY] <> 'H') AND
        (compattacked[cattX,cattY+1] <> 'H');
    end
  else
    begin
      if hitr = 1 then
        begin
          longhitrx := hitrx;
          longhitry := hitry;
          if (compattacked[hitrx+1,hitry] = 'O') AND (hitrx + 1 < 13) then
            begin
              cattX := hitrx+1;
              cattY := hitry;
              firstfound := 'R';
            end
          else
            if (compattacked[hitrx-1,hitry] = 'O') AND (hitrx - 1 > 0) then 
              begin
                cattX := hitrx-1;
                cattY := hitry;
                firstfound := 'L';
              end
            else
              if (compattacked[hitrx,hitry+1] = 'O') AND (hitry + 1 < 13) then
                begin
                  cattX := hitrx;
                  cattY := hitry+1;
                  firstfound := 'D';
                end
              else
                if (compattacked[hitrx,hitry-1] = 'O') AND (hitry - 1 > 0) then
                  begin
                    cattX := hitrx;
                    cattY := hitry-1;
                    firstfound := 'U';
                  end;
        end
      else
        begin
          if dirr = 'R' then
            begin
              if (misscheck = 1) OR ((Firstfound = 'R') AND (cattX + 1 > 12)) OR((Firstfound = 'L')
                                                                         AND (cattX -1 < 0))  then
                begin
                  if firstfound = 'R' then
                    begin
                      cattY := longhitrY;
                      cattX := longhitrX - 1;
                      firstfound := 'L';
                    end
                  else
                    begin
                      cattY := longhitrY;
                      cattX := longhitrX + 1;
                      firstfound := 'R';
                    end;
                end
              else
                begin
                  if firstfound = 'R' then
                    begin
                      cattY := longhitrY;
                      cattX := hitrX + 1;
                    end
                  else
                    begin
                      cattY := longhitrY;
                      cattX := hitrX - 1;
                    end;
                end;
            end
          else
            begin
              if (misscheck = 1) OR ( (Firstfound = 'U') AND (cattY-1 < 1) ) OR ((Firstfound = 'D') AND (cattY +1 > 12))  then
                begin
                  if firstfound = 'D' then
                    begin
                      cattX := longhitrX;
                      cattY := longhitrY - 1;
                      firstfound := 'U';
                    end
                  else
                    begin
                      cattX := longhitrX;
                      cattY := longhitrY + 1;
                      firstfound := 'D';
                    end;
                 end
              else
                begin
                  if firstfound = 'D' then
                    begin
                      cattX := longhitrX;
                      cattY := hitrY + 1;
                    end
                  else
                    begin
                      cattX := longhitrX;
                      cattY := hitrY - 1;
                    end;
                end;
            end;
          end;
    end;
    writeln('The Computer is firing sir! They have pulverised position ',cattX,' ',cattY);
    sleep(1000);
  if ships[cattX,cattY] <> 'O' then
  begin
    writeln('They have HIT us captain! We are already hearing reports of virtual casualties.');
    sleep(1000);
    misscheck := 0;
    compattacked[cattX,cattY] := 'H';

    if hitry = cattY then
    begin
      dirr := 'R';
    end;
    if hitrx = cattX then
    begin
      dirr := 'D';
    end;

    case ships[cattX,cattY] of
    'A' :
        begin
          hitcount2 := 0;
          done2 := 0;
          repeat
            inc(hitcount2);
            if comphitships[1,hitcount2]= 0 then
              begin
                comphitships[1,hitcount2]:= 1;
                done2:= 1;
                hitrx := cattX;
                hitry := cattY;
                inc(hitr);
             end;
           if comphitships[1,hitcount2+1]= -1 then
             begin
               sleep(1000);
               writeln('They SUNK our aircraft carrier!');
               inc(compsunk);
               done2:= 1;
               hitrx := 0;
               hitry := 0;
               hitr := 0;
             end;
          until done2 = 1;
        end;
    'B' :
        begin
          hitcount2 := 0;
          done2 := 0;
          repeat
            inc(hitcount2);
            if comphitships[2,hitcount2] = 0 then
              begin
                comphitships[2,hitcount2] := 1;
                done2 := 1;
                hitrx := cattX;
                hitry := cattY;
                inc(hitr);
              end;
            if comphitships[2,hitcount2+1] = -1 then
              begin
                sleep(1000);
                writeln('They SUNK Our Battleship!');
                inc(compsunk);
                done2 := 1;
                hitrx := 0;
                hitry := 0;
                hitr := 0;
              end;
          until done2 = 1;
        end;
    'C' :
        begin
          hitcount2 := 0;
          done2 := 0;
          repeat
            inc(hitcount2);
            if comphitships[3,hitcount2] = 0 then
              begin
                comphitships[3,hitcount2] := 1;
                done2 := 1;
                hitrx := cattX;
                hitry := cattY;
                inc(hitr);
              end;
           if comphitships[3,hitcount2+1] = -1 then
              begin
                sleep(1000);
                writeln('They SUNK Our Cruiser!');
                inc(compsunk);
                done2 := 1;
                hitrx := 0;
                hitry := 0;
                hitr := 0;
              end;
          until done2 = 1;
        end;
    'S' :
        begin
          hitcount2 := 0;
          done2 := 0;
          repeat
            inc(hitcount2);
            if comphitships[4,hitcount2] = 0 then
              begin
                comphitships[4,hitcount2] := 1;
                done2 := 1;
                hitrx := cattX;
                hitry := cattY;
                inc(hitr);
             end;
            if comphitships[4,hitcount2+1] = -1 then
              begin
                sleep(1000);
                writeln('They SUNK Our Submarine!');
                inc(compsunk);
                done2 := 1;
                hitrx := 0;
                hitry := 0;
                hitr := 0;
              end;
          until done2 = 1;
        end;    
    'D' :
        begin
          hitcount2 := 0;
          done2 := 0;
          repeat
            inc(hitcount2);
            if comphitships[5,hitcount2] = 0 then
              begin
                comphitships[5,hitcount2] := 1;
                done2 := 1;
                hitrx := cattX;
                hitry := cattY;
                inc(hitr);
              end;
            if comphitships[5,hitcount2+1] = -1 then
              begin
                sleep(1000);
                writeln('They SUNK Our Destroyer!');
                inc(compsunk);
                done2 := 1;
                hitrx := 0;
                hitry := 0;
                hitr := 0;
              end;
          until done2 = 1;
        end;
    end;
 end
 else
    begin
      writeln('MISSED');
      sleep(1000);
      compattacked[cattX,cattY] := 'M';
      misscheck := 1;
    end;
end;

procedure cominputships(spaces : integer; id : char);
var
  dfind : integer;
  direction2 : char;
  valid2 : boolean;
  count64, count88: integer;
begin
  repeat
    valid2 := true;
    tempx := random(12) + 1;
    tempy := random(12) + 1;
    dfind := random(4) + 1;

    case dfind of
    1 : direction2 := 'R';
    2 : direction2 := 'U';
    3 : direction2 := 'L';
    4 : direction2 := 'D';
    end;
    case direction2 of
       'R' : if tempx + spaces - 1 > 12 then valid2 := false;
       'L' : if tempx - spaces + 1 < 1 then valid2 := false;
       'U' : if tempy - spaces + 1 < 1 then valid2 := false;
       'D' : if tempy + spaces - 1 > 12 then valid2 := false;
    end;

    for count64 := 1 to spaces do
    begin
       if direction2 = 'R' then
       begin
        if compships[tempx+count64,tempy] <> 'O'
          then
            begin
              valid2 := false;
            end;
       end;
       if direction2 = 'L' then
       begin
        if compships[tempx-count64,tempy] <> 'O'
          then
            begin
              valid2 := false;
            end;
       end;
       if direction2 = 'U' then
       begin
        if compships[tempx,tempy-count64] <> 'O'
          then
            begin
              valid2 := false;
            end;
       end;
       if direction2 = 'D' then
       begin
         if compships[tempx,tempy+count64] <> 'O' then
            begin
              valid2 := false;
            end;
         end;
       end;

       for count88 := 0 to spaces - 1  do
          begin
            case direction2 of
            'U' :
                begin
                 if ((compships[tempx,(tempy-count88)+1] <> 'O') AND(compships[tempx,(tempy-count88)+1] <> id))
                     OR ((compships[tempx,(tempy-count88)-1] <> 'O') AND (compships[tempx,(tempy-count88)-1] <> id))
                     OR((compships[tempx-1,tempy-count88] <> 'O') AND (compships[tempx-1,tempy-count88] <> id)) OR
                     ((compships[tempx+1,tempy-count88] <> 'O') AND (compships[tempx+1,tempy-count88] <> id))then
                    begin
                      valid2 := false;
                    end;
                end;
            'L' :
                begin
                 if ((compships[tempx-count88,tempy+1] <> 'O') AND (compships[tempx-count88,tempy+1] <> id))
                     OR ((compships[tempx-count88,tempy-1] <> 'O') AND (compships[tempx-count88,tempy-1] <> id))
                     OR ((compships[(tempx-1)-count88,tempy] <> 'O') AND (compships[(tempx-1)-count88,tempy] <> id))
                     OR ((compships[(tempx+1)-count88,tempy] <> 'O') AND (compships[(tempx+1)-count88,tempy] <> id))then
                    begin
                      valid2 := false;
                    end;
                end;
            'R' :
                begin
                 if ((compships[tempx+count88,tempy+1] <> 'O') AND (compships[tempx+count88,tempy+1] <> id))
                     OR ((compships[tempx+count88,tempy-1] <> 'O')AND(compships[tempx+count88,tempy-1] <> id))
                     OR ((compships[(tempx+count88)-1,tempy] <> 'O') AND (compships[(tempx+count88)-1,tempy] <> id))
                     OR ((compships[(tempx+1)+count88,tempy] <> 'O') AND (compships[(tempx+1)+count88,tempy] <> 'O')) then
                    begin
                      valid2 := false;
                    end;
                end;
            'D' :
                begin
                 if ((compships[tempx,(tempy+count88)+1] <> 'O') AND (compships[tempx,(tempy+count88)+1] <> id))
                     OR ((compships[tempx,(tempy+count88)-1] <> 'O') AND (compships[tempx,(tempy+count88)-1] <> id))
                     OR ((compships[tempx-1,tempy+count88] <> 'O') AND (compships[tempx-1,tempy+count88] <> id))
                     OR ((compships[tempx+1,tempy+count88] <> 'O') AND (compships[tempx+1,tempy+count88] <> id)) then
                    begin
                      valid2 := false;
                    end;
                end;
            end;
          end;
  until valid2 = true;
  for count64 := 1 to spaces do
    begin
      compships[tempx,tempy] := id;
      case direction2 of
       'R' : inc(tempx);
       'L' : dec(tempx);
       'U' : dec(tempy);
       'D' : inc(tempy);
      end;
    end;
end;

begin
  randomize;
  hitr := 0;
  misscheck := 0;
  for count3 := 0 to 13 do
    begin
      for count4 := 0 to 13 do
        begin
          ships[count3,count4] := 'O';
          compships[count3,count4] := 'O';
          attacked[count3,count4] := 'O';
          compattacked[count3,count4] := 'O';
        end;
    end;
  writeln('PRESS 1 to PLAY INTRO OR 0 to SKIP');
  readln(intro);

  if intro = 1 then
  begin
    writeln('The computer - the immortal foe');
    for count5555 := 1 to 50 do
    begin
      sleep(100);
      writeln;
    end;
    writeln('Year after year you moaned, cried and fumed as your computer crashed');
    for count5555 := 1 to 50 do
    begin
      sleep(100);
      writeln;
    end;
    writeln('Then one day, you said "NO MORE" and lead your virtual fleet into an epic battle!');
    for count5555 := 1 to 65 do
    begin
      sleep(100);
      writeln;
    end;
    writeln('You will be assisted by your old friend Colonel Peter Umbridge');
    writeln;
    sleep(3000);
    writeln('"Good Afternoon Captain!"');
    writeln;
    sleep(3000);
    writeln('"The grid err... I mean sea is 12 by 12"');
    writeln;
    sleep(3000);
    writeln('"We can bomb any metre block with our suprisingly accurate V Bomb - that''s V for Virtual"');
    writeln;
    sleep(3000);
    writeln('"You will now position our glorious navy"');
    writeln;
    sleep(3000);
    writeln('"Actually only 5 ships - lots of the men are *sick*"');
    writeln;
    sleep(3000);
  end;
  writeln('"Please submit coordinates in the following way..."');
  writeln;
  sleep(3000);
  writeln('"HorizontalStartPoint VerticalSP ShipDirection - for example 4 7 R/L/D/U"');
  writeln;
  sleep(3000);
  writeln('"Remember not to put ships side by side captain - they can only touch corner to corner"');
  writeln;
  sleep(3000);
  printships;
  writeln('"Please input aircraft carrier captain, 5 spaces are needed"');
  inputships(5,'A');
  invalidcount := 0;
  printships;
  writeln('"Please input battleship captain, 4 spaces needed"');
  inputships(4,'B');
  invalidcount := 0;
  printships;
  hitships[2,5] := -1;
  comphitships[3,6] := -1;
  comphitships[4,6] := -1;
  comphitships[5,6] := -1;
  comphitships[1,6] := -1;
  comphitships[2,6] := -1;
  comphitships[2,5] := -1;
  comphitships[3,5] := -1;
  comphitships[3,4] := -1;
  comphitships[4,4] := -1;
  comphitships[4,5] := -1;
  comphitships[5,5] := -1;
  comphitships[5,4] := -1;
  comphitships[5,3] := -1;
  printships;
  writeln('Please input cruiser captain, 3 spaces needed');
  inputships(3,'C');
  invalidcount := 0;
  hitships[3,4] := -1;
  hitships[3,5] := -1;
  printships;
  writeln('Please input submarine captain, 3 spaces needed');
  inputships(3,'S');
  invalidcount := 0;
  hitships[4,4] := -1;
  hitships[4,5] := -1;
  printships;
  writeln('Please input destroyer captain, 2 spaces needed');
  inputships(2,'D');
  invalidcount := 0;
  hitships[5,3] := -1;
  hitships[5,4] := -1;
  hitships[5,5] := -1;
  hitships[1,6] := -1;
  hitships[2,6] := -1;
  hitships[3,6] := -1;
  hitships[4,6] := -1;
  hitships[5,6] := -1;
  printships;

  cominputships(5,'A');
  cominputships(4,'B');
  cominputships(3,'C');
  cominputships(3,'S');
  cominputships(2,'D');

  repeat
     humanturn;
     compturn;
     writeln('Here are the places where the computer has attacked');
     sleep(1000);
     printcompattacked;
  until (sunk = 5) OR (compsunk = 5);
  if sunk = 5 then
    begin
       writeln;
       writeln('Your men fought the good fight');
       sleep(1000);
       writeln;
       writeln('In the end you come out victorious');
       sleep(1000);
       writeln;
       writeln('But with ',compsunk,' ships lost and countless wives widowed...');
       sleep(1000);
       writeln;
       writeln('Who really won?');
       sleep(3000);
       writeln;
       writeln('You did...');
     end;
  if compsunk = 5 then
    begin
      writeln('YOU LOSE - PREPARE FOR SEVERAL EONS CAPTIVITY IN THE MEMORY UNIT!');
    end;                                                              
  readln;
end.

Remarks

Can you think of a program like this that you could write?

Programming - a skill for life!

Student programs to inspire you!