Doubts with generation of random values

Asked

Viewed 150 times

3

I have a question about a college job that I have been doing. It consists of creating a naval battle game, in PASCAL. I’m using the contents of this link as a guide to write the program:

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

The doubt is as follows, as I establish how many positions a particular vessel should occupy on the board?

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

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

Thank you.

1 answer

1

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 boxes 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';

Browser other questions tagged

You are not signed in. Login or sign up in order to post.