From the course: Object-Oriented Programming with C#
The room - C# Tutorial
From the course: Object-Oriented Programming with C#
The room
- [Instructor] The room class is going to be a key part of our house. Each house instance will have multiple rooms and each room will have its own unique name, location and items. The first thing we need to do is define all the directions you can move from room to room. To do this, we're going to create a new enum called Directions. Since we're going to replace all the code, you can either choose to use the empty class template or the empty enumeration template. Let's make a public enum called Directions and inside of it we're going to add none, north, east, south, and west. An enum is going to allow us to avoid accidental typos when we're comparing directions inside of our game. Each value inside of an enum represents an integer. So none equals zero, north equals one, east equals two, so on and so forth. This allows us to avoid what we call magic strings which is when you use a string to compare two values. If you have a typo, sometimes it's hard to debug where that typo is when you're typing out strings by hand. Now, when we compare each of the directions the player wants to move against the directions enum, we can use the compiler to let us know if we have any errors in our code. Now we're going to be using the program language and English classes a lot, so I'm going to pin them to the top of my editor. And inside of the languages class, I'm going to create two more properties that represent the name and the description for the room. Now we can go into the English class and add the values for these strings. We're going to create a template for the default room name that says room and the zero token will be replaced by the room's unique ID. And in parentheses, we'll replace the one in two tokens with its X and Y position on the grid. This will help us debug later as we move around from room to room. For the default room description, we'll add the following string. For this string template, we're going to replace the zero token with all the directions you're able to move based on the room that you're in. Now it's time to create our room class. Now we need to set up the room's name and description properties. We'll sign them to the values that we just created on our language class. Now we're going to need a dictionary that represents all the neighbors that this room can have. We're going to type it to our direction enum and int. We'll call it neighbors and instantiate it with some default values. We'll set each one of these neighbors to negative one, letting us know that there is no neighbor in that direction. Finally, we'll need to set a property that the room has been visited. Now that we have our directions and our room set up, the next video we're going to learn how to set up the logic to move from room to room.