Dealing with bad input

In the Text101 project, the ManageState method will throw an “index out of bounds” exception if the user presses the “3” key in a room with less than three exits. My solution was to check the length of the array before trying to read from it.

In a simple program with three possible inputs and a handful of states, the solution only required modifying a single line of code. But in a large program with dozens of inputs and millions of possible states, it would be almost impossible to deal with.

In general, how do I prevent players from breaking my games by doing things they’re not supposed to?

Hi @ChrisF,

Welcome to our community! :slight_smile:

Good job on fixing the problem. At the moment, Rick’s solution is hard-coded, and it works only if we have an array with a specific length and do not press any “wrong” key on our keyboard.

In lecture “For Loops”, Rick is going to replace his current solution with a more flexible one which allows us to have a different number of “next states” in each State object.

Did this help?

See also:

Yes, that’s very helpful!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms