Nested Classes

Not all classes defined within other classes are declared without a name. You can also use the class keyword to define a named class nested inside of another class. In this section, you will define a new GameInput class nested within the Game object.

Now that you have defined a game loop, you will want to apply some control over the user input passed to your game. NyetHack is a text adventure, driven by the user entering commands to the readLine function. There are two things you need to ensure about the user’s commands: First, that they are valid commands. Second, that commands with multiple parts, like “move east,” are handled correctly. You want “move” to trigger a move function and “east” to provide the move function ...

Get Kotlin Programming: The Big Nerd Ranch Guide, First Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.