2
\$\begingroup\$

I am trying to do a randomize function. It randomly selects one person (from two) to insert something into the console. The second person goes next, and then they keep alternating until the loop ends.

  randomize();
  RandomTurn := random(2)+1;  
  while (1) do
  begin
      if RandomTurn = 1 then 
        PersonTurn((NumberOfMoves mod 2)+1)
      else 
        PersonTurn(round(2/((NumberOfMoves mod 2)+1)));

      NumberOfMoves := NumberOfMoves + 1;
  end;

What do you think? How could this function be improved based on number of moves/user inputs?

\$\endgroup\$
  • \$\begingroup\$ What's the argument to PersonTurn() and why do you need it? \$\endgroup\$ – Thijs van Dien Jul 7 '13 at 9:32
  • \$\begingroup\$ There are two persons who are changing in their turn like in chess or any other game, so I wanted to do a fnction which will always turn another person BUT at the begining it chooses randomly on of them ... \$\endgroup\$ – Byakugan Jul 24 '13 at 20:19
3
+25
\$\begingroup\$

Randomness

This answer suggests that Random(2) would be okay for most uses. If you want more randomness, you could for example use an implementation of Mersenne Twister. This page lists a few options for Pascal/Delphi, for example this implementation.

Alternating values

Now that I know what you are looking for, I would write the code like this:

// If Value is divisible by 2 then return 1, otherwise return 2
procedure AlternateValue(Value) : ShortInt;
begin
    if Value mod 2 = 0 then Result := 1 else Result := 2
end;

// Your code
randomize();
RandomTurn := random(2)+1;
NumberOfMoves := RandomTurn

while (1) do
begin
    PersonTurn(AlternateValue(NumberOfMoves))
    NumberOfMoves := NumberOfMoves + 1;
end;

You should swap the two lines in the while loop if you want to start with the same number as Random gives. From what I gather, it should not matter much, however.

(I have never written any Pascal or Delphi before, so the code and/or syntax may not be perfect or correct.)

\$\endgroup\$
  • \$\begingroup\$ Yes I need to randomize first input to be randomly 1 or 2 and after that changing 1 - 2 - 1 - 2 etc. So only first number will be random and then others will always change, does this code of yours do that? \$\endgroup\$ – Byakugan Jul 4 '13 at 11:51
  • 2
    \$\begingroup\$ @Byakugan: To switch between 1 and 2, you could just do NewValue := 3 - OldValue: 3 - 1 -> 2, 3 - 2 -> 1. \$\endgroup\$ – Andriy M Jul 4 '13 at 17:29
  • \$\begingroup\$ Yes, the code I posted would do that. I have revised my answer, however, to indicate how I would write it now that I understand what you are after. You could also do as @AndriyM suggests, but I think my example is cleaner and more readable, because it states exactly what the intent of the code is. Does this answer your question? \$\endgroup\$ – Lstor Jul 4 '13 at 19:21
  • \$\begingroup\$ My snippet had a bug, because I forgot to do mod 2. That is fixed now. \$\endgroup\$ – Lstor Jul 5 '13 at 10:22
  • \$\begingroup\$ @AndriyM Or Result := Value mod 2 + 1;. It would be cleaner to use 0 and 1 rather than 1 and 2... \$\endgroup\$ – Thijs van Dien Jul 7 '13 at 9:29
0
\$\begingroup\$

I would improve the readability a bit by using:

PersonToPlay := RandomInitial();
For i:=MoveIndex := 1 to TotalNumberOfMoves do begin
  PersonTurn(PersonToPlay);
  PersonToPlay := 3 - PersonToPlay;
end;

To make it even more readable:

PersonToPlay := RandomInitial();
For i:=MoveIndex := 1 to TotalNumberOfMoves do begin
  PersonTurn(PersonToPlay);
  PersonToPlay := TheOtherPlayer(PersonToPlay);
end;
....

Function TheOtherPlayer (aPlayer : TPlayer) : TPlayer;
Begin
  if aPlayer := ThisPlayer then 
    Result := ThatPlayer
  else
    Result := ThisPlayer;
End;

Rate Readability over Speed (usually). Make your code comment itself.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.