Pascal Programming Help!!!!!!!!!!

January 30, 2010 at 10:09:11
Specs: Windows XP
Hi
I'm doing my A Level Computing. We are doing
a program on a battleship game.
This is the program code below..
program battleshipgame2;
{$APPTYPE CONSOLE}
uses
SysUtils;
Var
Battleship:Array[0..9,0..9] of Char;
R,C:Integer;
row,col:Integer; AllTargets---:Boolean;
begin
for R:=0 to 9 do
begin
for C:=0 to 9
do Battleship [R,C]:= 'M';
end;
Battleship [2,2]:='S';
Battleship [5,5]:='S';
Battleship [7,7]:='S';
repeat
for R:=0 to 9 do
begin
for C:=0 to 9
do
if (Battleship [R,C]= 'U')
then Writeln (Battleship[row,col]);
end;
Writeln ('Please enter a row number');
Readln (row);
Writeln ('Please enter a column number');
Readln (col);
if (Battleship [row,col])= 'S'
then Writeln ('Target Hit')
else
begin;
AllTargets---:=False;
Writeln ('Miss, Try Again');
end;
Battleship [row,col]:= 'U';
for R:=0 to 9 do
begin
for C:=0 to 9
do
if
Battleship [2,2]='U'
Battleship [5,5]='U'
Battleship [7,7]='U';
Battleship [R,C]='U'
then
begin;
AllTargets---:=True;
Writeln ('All Battleships Sunk.. Game
Over..Congratulations.. You Win');
end;
end;
until AllTargets---=True;

Readln
end.
I want help in the how the program ends. The
program ends when all the battleships are
sunk or when all the elements in the array
have been used. Can you also please check if
the whole program works. I also want the
program to show the coordinates already used
by the person before they enter the next
numbers so that they dont enter the same
numbers.
Can u please help????? I am trying to this for
weeks now. Can u please do it in a simple
way as it is A-Level work.
Thanks.


See More: Pascal Programming Help!!!!!!!!!!

Report •


#1
January 30, 2010 at 14:02:00
i would recommend to start from scratch - with pseudocode or minimal rendition. (i use BASIC because that lang. is very simple and i've used it alot). I dispensed with the boolean
in favor of simply counting hits, but every programmer has their own ways. Not sure if you're allowed to use GOTOs
so i had to construct a funky do-loop to handle the INPUTs
I used do-loops as opposed to for-next because imo they're better suited to the job. I coded an "escape" for the user to exit the game, which i'm suprised is not a requirement for the problem (exit/quit/end option should always be coded for, else user is forced to use a control-break or other means)

'========== initializations
dim battleship(9,9)
for i:=0 to 9
for j:=0 to 9
battleship(i,j):="M"
next j
next i
battleship(2,2):="S"
battleship(5,5):="S"
battleship(7,7):="S"
N:=0
hits:=0
max:=3
print "enter negative number to quit"
'======= begin main loop
do while N<100
'------- display current board status
gosub board
'-------- get input
do
input "row: ",R
if R<0 then exit do
input "col: ",C
if C<0 then
R:=-1
exit do
end if
if battleship(R,C)="U" then
print R,C "already used, try another shot"
end if
end do
'-------- analyze input and respond
if R<0 then exit do
N:=N+1
if battleship(R,C)="S" then
print "nice shot"
hits:=hits+1
end if
battleship(R,C):="U"
if hits>=max then exit do
end if
end do
'--------- end: show the results
if hits>=max then print "you win!"
if N=100 then print "took you long enough!"
if hits<max then print "give up so easy? goodbye"
end

subroutine board():
'-------- display the board status
print "board:"
print "R C: 0123456789"
for i:=0 to 9
print i;" ";
for j:=0 to 9
x:=battleship(i,j)
if x<>"U" then x:=" "
print x;
next j
print ""
next i
return


Report •
Related Solutions


Ask Question