: uh...can you help put that in lay man's terms or possibly help by showing what the whole thing is supposed to look like...we don't even know where to start or anything
:
I assume you are familiar with the game Battleship, and know the rules. If you don't then familiarizing yourself with that is a very good start. Here is part of the code you requested. It still needs some parts, like the human player placing his ships.
unit BSUtils;
interface
const
pfWater = 1;
pfMineSweep = 2;
pfFrigate = 3;
pfCruiser = 4;
pfBattleShip = 5;
ShipNames: array[1..5] of string = ('Water', 'MineSweep', 'Frigate',
'Cruiser', 'BattleShip');
FieldSize = 16;
type
TShip = record
Size: integer;
NumberOfHits: integer;
ID: integer;
end;
TPlayingField = array[1..FieldSize, 1..FieldSize] of integer;
TShips = array[1..11] of TShip;
TPlayerData = record
Ships: TShips;
Field: TPlayingField;
end;
var
Human: TPlayerData;
Computer: TPlayerData;
procedure Initialize;
{ Initializes the game to 0 }
procedure SetShip(var Player: TPlayerData; X, Y, Ship: integer; IsVert: boolean);
{ Sets the ship indexed "Ship" for the person indicated by "Player" at
location "X, Y" with vertical direction if "IsVert" is true }
procedure ComputerSetShips;
{ Sets all ships for the computer }
procedure WriteDisplay;
{ Shows default display: Human field Computer Field }
procedure GetPlayerShot;
{ Allows human to take 1 shot }
procedure GetComputerShot;
{ Allows computer to take 1 shot }
function IsWinner: integer;
{ Returns 1 if human wins
Returns 0 if neither wins
Returns -1 if computer wins }
implementation
procedure CreateShip(AID, ASize: integer; var Ship: TShip);
begin
with Ship do
begin
Size := ASize;
ID := AID;
NumberOfHits := 0;
end;
end;
procedure SetShip(var Player: TPlayerData; X, Y, Ship: integer; IsVert: boolean);
var
i: integer;
begin
with Player do
begin
if IsVert then
for i := Y to Y+Ships[Ship].Size-1 do
Field[X, I] := Ship
else
for i := X to X+Ships[Ship].Size-1 do
Field[I, Y] := Ship;
end;
end;
procedure SetBoard(var Player: TPlayerData);
var
i, x, y: integer;
begin
with Player do
begin
for i := 1 to 4 do
CreateShip(pfMineSweep, 2, Ships[i]);
for i := 5 to 8 do
CreateShip(pfFrigate, 3, Ships[i]);
for i := 9 to 10 do
CreateShip(pfCruiser, 4, Ships[i]);
CreateShip(pfBattleShip, 5, Ships[11]);
for x := 1 to FieldSize do
for y := 1 to FieldSize do
Field[x, y] := pfWater;
end;
end;
procedure ComputerSetShips;
function VIsClear(X, Y, Size: integer): boolean;
var
i: integer;
b: boolean;
begin
b := true;
for i := Y to Y+Size-1 do
begin
if Computer.Field[X, i] <> pfWater then
b := false;
end;
VIsClear := b;
end;
function HIsClear(X, Y, Size: integer): boolean;
var
i: integer;
b: boolean;
begin
b := true;
for i := X to X+Size-1 do
begin
if Computer.Field[i, Y] <> pfWater then
b := false;
end;
HIsClear := b;
end;
var
i: integer;
X, MaxX: integer;
Y, MaxY: integer;
D: boolean;
begin
for i := 1 to 11 do
begin
X := Round(Random*100);
D := X > 49;
if D then
begin
MaxX := FieldSize;
MaxY := FieldSize - Computer.Ships[i].Size-1;
repeat
X := Round(Random*MaxX)+1;
Y := Round(Random*MaxY)+1;
until VIsClear(X, Y, Computer.Ships[i].Size);
end else begin
MaxX := FieldSize - Computer.Ships[i].Size-1;
MaxY := FieldSize;
repeat
X := Round(Random*MaxX)+1;
Y := Round(Random*MaxY)+1;
until HIsClear(X, Y, Computer.Ships[i].Size);
end;
SetShip(Computer, X, Y, i, D);
end;
end;
procedure Initialize;
begin
Randomize;
SetBoard(Human);
SetBoard(Computer);
end;
procedure DisplayField(Player: TPlayerData; OffSetX, OffSetY: integer);
var
x, y: integer;
begin
for x := 1 to FieldSize do
for y := 1 to FieldSize do
begin
GotoXY(x+OffSetX, y+OffSetY);
case Player.Field[x, y] of
-20..-2: Write('#');
-1: Write('+');
0..20: Write('_');
end;
end;
end;
procedure DetermineShip(var Player: TPlayerData; X, Y: integer);
var
i: integer;
begin
with Player do
begin
Field[X, Y] := -Field[X, Y];
if -Field[X, Y] = -pfWater then Exit;
with Ships[Abs(Field[X, Y])] do
begin
inc(NumberOfHits);
if NumberOfHits = Size then
begin
writeln(ShipNames[ID], ' has been sunk!');
write('Press enter to continue...'); readln;
end;
end;
end;
end;
procedure GetPlayerShot;
var
X, Y: integer;
begin
repeat
GotoXY(1, FieldSize+4);
write('Give horizontal location (1-', FieldSize, '): '); readln(X);
write('Give vertical location (1-', FieldSize, '): '); readln(Y);
until (X in [1..FieldSize]) and (Y in [1..FieldSize]) and
(Computer.Field[x, y] > 0);
DetermineShip(Computer, X, Y);
DisplayField(Computer, 20, 1);
end;
procedure GetComputerShot;
var
X, Y: integer;
begin
repeat
X := Round(Random*FieldSize)+1;
Y := Round(Random*FieldSize)+1;
until (X in [1..FieldSize]) and (Y in [1..FieldSize]) and
(Human.Field[x, y] > 0);
write('Give horizontal location (1-', FieldSize, '): ', X);
write('Give vertical location (1-', FieldSize, '): ', Y);
DetermineShip(Human, X, Y);
DisplayField(Human, 0, 1);
end;
procedure WriteDisplay;
begin
GotoXY(1,1); write('Your board:');
DisplayField(Human, 0, 1);
GotoXY(20,1); write('My board:');
DisplayField(Computer, 20, 1);
end;
function IsWinner: integer;
var
i: integer;
begin
IsWinner := 1; { Assume Human has won }
for i := 1 to 11 do
if Computer.Ships[i].Size <> Computer.Ships[i].NumberOfHits then
IsWinner := -1; { If still computer ships left, assume Computer has won }
if IsWinner = 1 then Exit;
for i := 1 to 11 do
if Human.Ships[i].Size <> Human.Ships[i].NumberOfHits then
IsWinner := 0; { If still human ships left, game not finished yet }
end;
end.
Any errors indicating unknown identifiers arise from the lack of a uses list. See the help files of your version of Pascal for which unit to use.