pascal – Doubts with random value generation

Question:

I have a question regarding a college work I've been doing. It consists of creating a naval battle game, in PASCAL. I'm using the content of this link as a guide to writing the program:

http://www.pb.utfpr.edu.br/omero/Pascal/Exercicios/Geral/BATALHA.Htm

The question is, how do I establish how many positions a particular vessel must occupy on the board?

For example, if I want to establish that a certain vessel occupies 6 positions on the board, should I write that way?

for Conta := 1 to 5 do
begin
repeat
i := random(6);
j := chr(random(9)+65);
until Batalha[i, j] = '0';
Batalha[i, j]:='S';
end;

The command i := random(6); establishes that a certain vessel will occupy 6 positions on the board?

Thanks.

Answer:

Not quite, see your example with very detailed comments:

{ Cria 5 (cinco) Submarinos. }
  for Conta := 1 to 5 do  // laço para o número de submarinos
  begin
    repeat
     //seleciona um número aleatório dentre as colunas (numéricas)
      i := random(6);  
      /* Seleciona uma letra aleatória (A -> J ) [9 letras]
      Isto e' : (número de 0 a 9) + 65 para converter o número em letra) 
      */
      j := chr(random(9)+65); 

    until Batalha[i, j] = '0';
    //adiciona o submarino (uma casa)
    Batalha[i, j]:='S';
  end;

Similarly, you will see that in the destroyer the houses are defined as follows:

//verifica se esta no começo do tabuleiro ou se o sucessor estará no começo
until (Batalha[i, j] = '0') and (Batalha[i, succ(j)] = '0');
//adiciona uma "parte" do destroyer no tabulero
Batalha[i, j] := 'D';
//continua a adicionar o destroyer
Batalha[i, succ(j)] := 'D';
Scroll to Top
AllEscort