Doubts with generating random values

3

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

link

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

For example, if I want to establish that a certain vessel occupies 6 positions on the board should I write in this 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;

Does the i := random(6); command establish that a particular vessel will occupy 6 positions on the board?

Thank you.

    
asked by anonymous 06.07.2014 / 13:31

1 answer

1

Not quite so, 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;

In an analogous way, 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';
    
07.07.2014 / 20:02