<< Back

Objects

In this lesson, we'll be discussing ways of handling more complex data, and introducing the concept of types.

Review

Lets start by glancing at all of the code we've written so far:

We're making this program where we walk around in a dungeon, but why not think about rooms and your current location? We currently don't have any concept of those things. We're going to need some variables, but this time we're going to use some more complicated ones, and go into the basics of objects.

Objects

Objects are a concept in most programming languages, and all of them give the concept a slightly different meaning. In C#, nearly everything is an object in some way or other. Really, all it means is some kind of container for data and/or methods. We'll be needing one for a coordinate, and one for some rooms. We could save all of the rooms into their own variables, but we can get some value out of the container object itself, it turns out.

Lets try this for real. First, we'll add a new variable on line 4:

This looks a little strange, right? This variable has some interesting properties. First, it is constructed from two integers named South and East, with their values both set to 5. If you add a new line and type position. you will be provided a completion list which contains South and East. Some things you could do with this are: position.South = 1; . Now the value of your position object's South is set to 1! Or you could try Console.WriteLine(position.South); to show it on screen when you run your program. You can also do some more complicated things to make it output nicely, like Console.WriteLine($"Current Position: South: {position.South} East: {position.East}");. You can mix and match these and mess around with ifs, but that gives you a starting point. Second, variables are always locked to a type, which means that position will always have a South and an East, and those will always be integers. You'll always know what you're dealing with!

Next, let's store the rooms.

What we're going to use is called an Array. Arrays are the simplest form of a group of things where the positions of those things don't change and need to be addressed with a coordinate. For instance, if you wanted to create an array of numbers (let''s say some scores you got in a game) you might declare it as var scores = new[] { 10, 8, 9, 5 };. This creates a variable containing an array that contains 10, 8, 9, and 5. If you were then to call Console.WriteLine($"Score 1: {scores[0]} Score 4: {scores[3]}"); you would get an output of "Score 1: 10 Score 4: 5". We'll be doing something considerably more elaborate.

Don't panic! There is a lot going on here, but it's actually very simple. All of those things with const string on them are called constants. They're used for values that should never be allowed to change. You have to include string (or int, or whatever other type the constant is) when defining one (strings, as you probably know by now, are text). We're using those because they take up less space and can be reused when defining the map below them. Most of the dungeon is solid walls with no rooms, and all of those are being represented with the value null. The rooms are declared as a string containing the message that should show up when the room is entered. I also included a comment to draw a picture of the dungeon, using equals signs as the borders. Our next goal is to hook all of this up so you can walk around in this dungeon, or define your own! (You can add some more constants if you want to clean this up more!)

Now for our first change to the main loop!

If you run the program now, you will see the greeting for the room defined by the starting position. Because position never changes, you never 'go' anywhere. Let's first figure out which ways you can go.

What we've done here is created some simple booleans to store whether or not a direction is allowed, based on the dimensions and content of our dungeon. You'll notice that we used a strange looking character combination, && to mean logical "and" - this logical operator combines two booleans on its left and right, only returning true if they both are true. In the code ahead, we'll be using || - the logical "or" operator, which returns true if at least one of the booleans around it is true.

We've just replaced one line of code with so many new lines of code! Lets go through what it does. First, we start our line with a Console.Write() method call. You haven't seen this before, but the way it works is that instead of putting your string onscreen and then starting a new line, it leaves the cursor in place so that we can continue the line.

We then have ifs, one for each direction, which check whether this direction is possible, and if so display which key corresponds to the direction. If there are subsequent directions we add a comma, otherwise we continue on. This continues until the full message is built. If you run this now, you should see that you are allowed to go North or South.

We now add our 4 direction checks, similarly to how they were done before. Now, we also check whether you can go in a direction before allowing it and we display nothing. Instead, we update the position based on the direction you attempted to go. If you run this now, you can walk around in the dungeon!

That's enough for this lesson!