Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
HELP!!! Battleship program Posted by krazykree on 1 Feb 2005 at 3:02 AM
Hello! Please help! We (my classmates and I) are assigned a final project to make a battleship program in which we need to win against the computer everytime! How in the world can we do that? We are basic programmers, in which we just know how to make a program that can add or subtract or whatnot but not how to make battleship. PLEASE HELP!!!
Report
Re: HELP!!! Battleship program Posted by zibadian on 1 Feb 2005 at 3:54 AM
: Hello! Please help! We (my classmates and I) are assigned a final project to make a battleship program in which we need to win against the computer everytime! How in the world can we do that? We are basic programmers, in which we just know how to make a program that can add or subtract or whatnot but not how to make battleship. PLEASE HELP!!!
:
First you need to create 2 playing-boards: 1 for you and 1 for the computer. I would suggest a 2-D integer array:
var
  ComputerField: array[1..20, 1..20] of integer;
  PlayerField: array[1..20, 1..20] of integer;

Then you can define your ships and water. I would suggest that you take water as a value of 1, and the ships as numbers greater than 1. If a location is hit, you can make it negative to indicate the hit. I would also make an array, in which you count the hits against each ship. This makes it easier to keep track of how many and which ships are sunk.
The game plays by entering the coordinates to hit and then the computer updates the playing fields and the ship lists.
The display can be an simple indicator using various characters, or could be a complete graphical appearance. The easiest way is use GotoXY() to write the characters. You might also want to use colors: TextColor() and TextBackground().
If you truly want to let the player win every time, let the computer know where 1 small ship is. This way you can control where the computer fires last. The most basic game lets the computer pick the locations at random.
  repeat
    x := Random(20)+1;
    y := Random(20)+1;
  until PlayerField[x, y] > 0;

This however might lock the program for some time later in the game. An improvement to this, is to count the number of times the computer picked a location. After a certain number is reached search for the first positive location on the map. This way the computer won't think too long about its move.
Report
Re: HELP!!! Battleship program Posted by krazykree on 4 Feb 2005 at 9:06 PM
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
Report
Re: HELP!!! Battleship program Posted by zibadian on 5 Feb 2005 at 12:33 AM
: 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.
Report
Re: HELP!!! Battleship program Posted by krazykree on 7 Feb 2005 at 3:15 AM
thanks, i'll try this...you are a great help...now hopefully i'll be able to figure this out for my teacher...thanks! i'll just ask you later if i have problems. thank you so much
Report
Re: HELP!!! Battleship program Posted by krazykree on 15 Feb 2005 at 1:48 AM
I'm really really sorry...can you maybe write the program for us? we need it this week and we are really trying to figure it out. you've helped so much but it is still very hard. i know it's asking a lot, but could you please help again. thank you so much.
Report
Re: HELP!!! Battleship program Posted by zibadian on 15 Feb 2005 at 4:01 AM
: I'm really really sorry...can you maybe write the program for us? we need it this week and we are really trying to figure it out. you've helped so much but it is still very hard. i know it's asking a lot, but could you please help again. thank you so much.
:
I won't do your assignments for you, because then you'll never learn Pascal.
Report
Re: HELP!!! Battleship program Posted by gabokool on 19 Feb 2005 at 6:25 PM
: Hello! Please help! We (my classmates and I) are assigned a final project to make a battleship program in which we need to win against the computer everytime! How in the world can we do that? We are basic programmers, in which we just know how to make a program that can add or subtract or whatnot but not how to make battleship. PLEASE HELP!!!
:

Are you from Venezuela? I have a final Project too which is to make a battleship program.. :P I study at the UCV....

Report
Re: HELP!!! Battleship program Posted by krazykree on 20 Feb 2005 at 2:18 AM
: : Hello! Please help! We (my classmates and I) are assigned a final project to make a battleship program in which we need to win against the computer everytime! How in the world can we do that? We are basic programmers, in which we just know how to make a program that can add or subtract or whatnot but not how to make battleship. PLEASE HELP!!!
: :
:
: Are you from Venezuela? I have a final Project too which is to make a battleship program.. :P I study at the UCV....
:
:
no philippines
Report
Re: HELP!!! Battleship program Posted by gabokool on 20 Feb 2005 at 8:16 AM
: : : Hello! Please help! We (my classmates and I) are assigned a final project to make a battleship program in which we need to win against the computer everytime! How in the world can we do that? We are basic programmers, in which we just know how to make a program that can add or subtract or whatnot but not how to make battleship. PLEASE HELP!!!
: : :
: :
: : Are you from Venezuela? I have a final Project too which is to make a battleship program.. :P I study at the UCV....
: :
: :
: no philippines
:

If you find the program I will appreciate that you posted it here please!!! And if i find it i will do the same...




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.