0
votes

I'm currently porting the VMS Pascal version of the classic Moria game, but I'm not sure if I'm understanding the limits of if/then statements (I've never programmed in Pascal before).

My understanding so far is that with no begin/end, then an if/then block only encloses one following statement. If that's the case, then in the following code;

if (i4 > 0) then
  with inventory[i4] do
    begin
      objdes(out_val,i4,false);
      msg_print('Your ' + out_val + ' glows faintly!');
      if (enchant(toac)) then
        begin
          flags := uand(%X'7FFFFFFF',flags);
          py_bonuses(blank_treasure,0);
        end
      else
        msg_print('The enchantment fails...');
    end;
  ident := true;

the ident := true; would be outside of the if (tval > 0) then block, meaning that even if i4 is 0, ident would still be set to true.

If that is correct, then does it mean the following code from UMoria (a C port) is wrong?

i_ptr = &inventory[INVEN_WIELD];
if (i_ptr->tval != TV_NOTHING) {
  objdes(tmp_str, i_ptr, FALSE);
  (void) sprintf(out_val, "Your %s glows faintly!", tmp_str);
  msg_print(out_val);
  if (enchant(&i_ptr->tohit, 10))
    {
      i_ptr->flags &= ~TR_CURSED;
      calc_bonuses();
    }
  else
    msg_print("The enchantment fails.");
  ident = TRUE;
}

...as the ident = TRUE; is inside the if block.

I've seen similar examples in several places -- I guess it's possible that these were changed for the C port -- but I'm hoping to get clarification before I change too much code.

1

1 Answers

2
votes

Your assessment of the flow control is correct. However, the assignment of indent to true in the original Pascal code most likely was meant to be in the if/then statement due to the indentation.

This is why I always run an auto indentation on the source code in an IDE. It flushes out these bugs. (Python is an over reaction to this as I've seen indentation bugs in it and its not as amenable to automatic IDE help.)

I suspect the C port to be correct if someone proofread it and tested it.

Test driven development helps here as it helps define what is truly intended.