aroberge / reeborg

Enhanced Karel-the-robot clone
http://reeborg.ca/reeborg.html
Other
47 stars 36 forks source link

Need to switch to the correct wold when loading a saved file. #452

Closed aroberge closed 5 years ago

aroberge commented 5 years ago

Problem reported by @schellenberg with a proposed fix described as fragile. https://github.com/aroberge/reeborg/pull/451/commits/1c59a666bebd8f93b6b134e847364da0dd75d874

aroberge commented 5 years ago

If I understand correctly:

  1. Currently, when a solution is saved for a world, the filename is automatically chosen so as to match the world name; for example: "Step 1.py"
  2. It may happen that a file by that name already exists; in which case, the new solution might be saved as "Step 1 (1).py" (at least, on Windows).

What is desired is that, when this solution is loaded, that the corresponding world be "automagically" selected, based on the .py file's name.

This is done for convenience to the students, and is guaranteed not to affect either their program's content nor the world chosen. Nothing about the filename affects the UI in any way.

= = = Currently, the recommended method is to have students write something like:

World("Step 1")

as an instruction in their program. This command ensures that the correct world is loaded when the program is run. The filename chosen for the program file is completely ignored as it is deemed to be irrelevant: the only thing that matters is the program's content.

The current behaviour is based on the premise that, if a student wants a given world to be loaded, he or she should explicitly do so, either by using the html select, or by using the World() function.

The choice of using World() is consistent with Python's "Explicit is better than implicit" philosophy. = = = @schellenberg I am currently not convinced that the "magic" method provides a significant improvement, nor am I certain that it could not lead to some undesired and unexpected behaviour when a program is saved with a different name. In my opinion, students that decide to change the name of a program saved on their computer, and load it up later, should not be surprised by a side-effect related to a different file name they chose. Perhaps I am missing something.

schellenberg commented 5 years ago

@aroberge I would compare this feature to how the library tab is "automagically" bundled into a single Python file, then extracted to two separate tabs when opened by Reeborg. Automatically opening the correct world if the filename matches a world exactly simply improves the user experience with file IO, without causing any unexpected behavior (since nothing is done if the file name doesn't exactly match a world file, and the user can always use the world selector to choose the desired world).

aroberge commented 5 years ago

Ok, change accepted. After reading the code, and struggling to remember the syntax for regular expressions (something I always struggle with), I replaced the regex by a simple string method which, as far as I can tell, gives the same end result.