I’m swimming out into rough waters with this one, I know… I’m reading a player’s input after prompting them to enter a cardinal direction. This is reading “SW” as “W”.
if the player's command matches the regular expression "w|W|west|West|WEST":
say "West.";
reject the player's command;
otherwise if the player's command matches the regular expression "sw|SW|southwest|Southwest|SOUTHWEST":
say "Southwest.";
reject the player's command;
This condition is true if any contiguous part of the text can be matched against the given regular expression.
This is the version you are using. Any text containing a w matches the pattern w, and that includes any text containing sw and all variants of it as well.
If the listed expressions should match the entire command and not just a part of it, then you should use the second form:
if (text) exactly matches the regular expression (text):
This condition is true if the whole text (starting from the beginning and finishing at the end) can be matched against the given regular expression. The option “case insensitively” causes lower and upper case letters to be treated as equivalent.
Then it will work for your example.
Also, the otherwise is not strictly needed, since a match in the first if will reject the command.
I also added the option case insensitively which makes writing patterns to match much easier.
You do realize that directional commands like west etc are built-in commands? You can always use eg. an instead rule to do something else…
I really appreciate this! A reply for your footer: Haha, yes. For the sake of better understanding regular expressions and getting to the heart of the problem I stripped the context from the original question.
For context: at that moment in the game the player cannot see and is being attacked from random directions by an enemy. The player may attack in a direction in hopes of striking that enemy (with textual clues hinting along the way). So, I use an “instead” to cause a change in command prompt and read their next command to declare if they were successful in striking the enemy or not.
You can use a truth state (boolean) to check whether the fight sequence is happening. Then, you can use instead to stop the player from going in a direction, and check whether that direction will hit the enemy to report a result.
I’m not sure from the original post if you @PatientRock are trying to learn regular expressions, or just trying to make this fight sequence work.
Infrequently, a regular expression test at the ‘after reading the player’s command stage’ is the easiest fix for some systemic programming challenge, but usually when you’re ovelapping what Inform is already great at… in this case, reading any direction, and also automatically reading all the variations for the direction, like S and SOUTH, SW and SOUTHWEST… not doing it with regular expressions will be the better way.
Like @Hidnook said, detecting the fight is on with a boolean, and then intercepting commands with instead rules works very well. I’ve produced a demo that shows how you can code all the elements of this.
You go east to enter the boxing arena. There, only directions are allowed to be entered, and the command prompt changes, while the fight’s happening. A cheat hint shows you what direction you need to go in each round. To leave, you go west again.
"Direction Fight"
Lab is a room. "This is the lab. Go east to enter the boxing ring with the monster.".
Boxing Ring is east of lab. "You're in the boxing ring. It's fight time! Go west to leave the fight.".
A bored-looking monster is a neuter animal in the boxing ring.
fight-is-on is initially false.[the boolean for fight mode]
strike-direction is a direction that varies.[a variable to hold the direction we need to attack in]
Carry out going east in lab:
now command prompt is ">>>";
now fight-is-on is true;
Carry out going west in boxing ring:
now command prompt is ">";
say "You leave the boxing ring, and the fight.";
now fight-is-on is false;
Every turn when fight-is-on is true:
now strike-direction is west;
while (strike-direction is inside or strike-direction is outside or strike-direction is up or strike-direction is down or strike-direction is west):[we have to keep re-rolling the die to get a new random direction until it's NOT west or inside or outside or up or down]
now strike-direction is a random direction;
say "(Hint - the direction you need to move to land the next blow is [strike-direction] )[line break]";
Instead of doing anything other than going when fight-is-on is true:
say "All you can do while the fight is happening is enter directions...";
Instead of going when fight-is-on is true:
if going west:
continue the action;[let the player actually move west and escape]
otherwise if the noun is strike-direction:
say "BAM! You hit the monster.";
otherwise:
say "ZOT! You missed!";
Thanks! This is really neat and I might borrow it for a different part of the game. Questions like the above are tricky for me to frame because I’m pulling out a specific sequence in a very large game (500k words and counting!). So, there are other story and mechanical reasons for why I’m using the player’s command (chief among them is a set of NPC movement rules that I’m terrified of modifying for fear of breaking the entire game).
Such is the curse of being an Inform journeyman-- I know enough to get myself into trouble.
Is there a reason to use a truth state to track “whether the fight scene is happening” rather than a Scene? It seems like it would be useful to be able to say Instead of going during the Fight Sequence.
Yeah, the big difference between scenes and truth states is how they start and end. Scenes start and end based on conditions, while truth states are set in rules. So it all depends on the structure of the actual game—do you want it to start based on the state of the world, or based on an action the player takes, for example?
There’s also a tech quality of scenes that ‘scene changes only happen at the start and end of turns’ (from the docs). Sometimes that’s what you want, but if it’s not, it’s possible to change a truth state mid-turn, or repeatedly during a turn.