'YesOrNo' in Inform

Several people have asked how to handle binary, yes/no questions in Inform. It's really very simple. Here's a usefully stupid illustration:

Room    firstRoom "The First Room"
  with  description "A very questionable space.^",
	after [;
	  Look:
	    print "Shall we open a window?^";
	    if(YesOrNo())
	    {
	    	print "A window flies open, venting atmosphere to space.";
	    	deadFlag = 1;
	    }
	    else
	    {
	    	print "No, good.  Windows are very dangerous.";
	    }
	];

When the Player enters this room (that is, at the start of the game), s/he is immediately confronted with the Yes/No question, which is triggered after a Look action. (Entering any room initiates Look.)

The line if(YesOrNo()) activates the YesOrNo routine from the Library, which tells the game to wait for input, which must be a line containing "yes" or "no." Non-matching responses such as "maybe," "???" or "foo" elicit the reply, "Please answer yes or no." Elaborated responses such as "No, dammit!!" are read as "no." In the case of ambiguous responses ("yes and no" or "no and yes"), the first keyword is definitive (yes, no).

This routine does not require the Player to "say 'yes'" or "speak 'No'", etc. The answer may be typed directly. "Y", "y", "N", and "n" are valid shortcuts.

Note that we could have written the routine call as:

if(YesOrNo() == true)...

But this is not necessary, since the routine YesOrNo always resolves to true (answer was yes) or false (answer was no).

The complete .inf file for this example, YorN_2.inf, is available in COSC320/1114 within the common directory on student-iat.



University of Baltimore Logo

Last updated: 01/02/07 18:36:00
Copyright © 2006 School of Information Arts and Technologies