Request for optimization/readability help

So, this bit of code does what I want it to. A customer pushes a squeaky shopping cart in and out of the area. Its location is random. But… it’s kind of a mess? The code is a lot less readable than I like—I’m bad enough at this stuff as it is—and I wonder if there is a more elegant, efficient way to achieve what I’m doing. I’ll post it as a snippet. Does anyone have trouble reading Borogrove snippets?

I appreciate any suggestions.

2 Likes

I would not define a global variable R here; you can do the random test inside the every turn rule with a local variable. So just write this:

Every turn when the player is in the pharmacy (this is the random cart rule):
   if the cart is in the location:
      if a random chance of 1 in 2 succeeds:
         say "Some text";
      otherwise:
         say "Other text";
         now the cart is nowhere;
   otherwise:
      if a random chance of 1 in 2 succeeds:
         say "Some text";
      otherwise:
         say "Other text";
         now the cart is in the location;

You keep everything in one place, making it easier to see what is going on. (Untested code.)

4 Likes

This is definitely better. Thanks!

1 Like
Every turn:
  if a random chance of 1 in 2 succeeds begin;
    say "A shopper pushes a [if the cart is in the location]squeaky cart nearby, occasionally stopping to pick up an item[else]shopping cart into sight. It has a squeaky wheel, which suggests it is rubbing a bit[end if].";
    now the cart is in the location;
  else;
    if the cart is in the location begin;
      now the cart is nowhere;
      say "The shopper pushes the squeaky cart down an aisle, vanishing from sight.";
    else;
      say "A squeaking sound can be heard nearby.";
    end if;
  end if;

(Explicit begins and ends is just a personal preference of mine and I’m not representing their use as improving the code.)

2 Likes

That’s helpful, too. It looks like the decision point between Victor’s example and your is whether or not to present one set of alternatives as a single say directive or as two separate ones. Both take advantage of natural language to do randomization instead of declaring a variable.

Which is great. I haven’t used that phrasing before, and it’s bound to come in handy!

2 Likes