Code Monkey home page Code Monkey logo

Comments (5)

andrewstellman avatar andrewstellman commented on September 1, 2024

Hi! Thanks so much for reporting this. I'm trying to understand the issue you're raising—I definitely want to fix it if it's a problem, but I'm not sure I get it yet.

You're looking at the code from page 11 of Chapter_10_project.pdf, right? That code definitely works—it's copied and pasted from HouseTests.cs, and I just downloaded and tested that solution, it compiles and all the tests pass. I just checked, and the code is identical. So there doesn't seem to be a syntax error in it. But that doesn't mean there isn't an issue! It might just be that I need to add some notes to the code to explain it a little better, because I could definitely see that it might not be 100% clear at first glance.

The idea with the way that test is structured is that first it's trying to test the layout of the house. It starts with the entry, using House.Entry, and makes sure it's correct by checking its Name. Then it uses the GetExit that Entry inherits because it extends Location—that's how it gets a reference to the hallway location, and it checks its Name property to make sure it has the right location. Then it uses the hallway object's GetExit to get to the kitchen and the bathroom, and tests their names to make sure they're the right object.

The reason it uses House.Entry to get the reference to the first location but uses GetExit to get to the rest of the locations is because it's navigating throughout the layout of the house, the same way the player will later in the project.

Does that help explain what's going on? If that helps, I'll add that to the PDF to help other readers.

If it's still confusing, I'd love to hear your thoughts! I really want to try to make this project as effective as possible. And the nice thing about PDF downloads is that I can update them immediately, so I definitely want to help out here if I can.

from fourth-edition.

DarwinsCorn avatar DarwinsCorn commented on September 1, 2024

from fourth-edition.

andrewstellman avatar andrewstellman commented on September 1, 2024

I think maybe you're just overthinking it a bit! Here's how this all works.

It all starts with Entry, a readonly field in the static House class:

public static readonly Location Entry;

That field is set in the House constructor:

Entry = new Location("Entry");

And now Entry points to a Location object. The constructor then creates two new Locations objects:

var hallway = new Location("Hallway"); var livingRoom = new Location("Living Room");

And later it uses Entry.AddExit to add them to the Entry location's exit collection:

Entry.AddExit(Direction.East, hallway); Entry.AddExit(Direction.Out, garage);

It's fine to use var for those declarations, because the static House object doesn't need to keep track of them. It just needs to keep track of the one Location reference, Entry, and then it can use its Exits collection to get to them.

The rest of the constructor just uses var to define variables to reference new locations, because House doesn't need to keep track of any of the other Location objects, either. Any time it needs a Location for a new place in the house, it will look it up in another Location object's Exits collection.

Does that help make sense of it?

from fourth-edition.

DarwinsCorn avatar DarwinsCorn commented on September 1, 2024

from fourth-edition.

andrewstellman avatar andrewstellman commented on September 1, 2024

I'm glad to help And thanks for explaining what went wrong—that's really useful to me. I'll think about how I might be able to help other readers prevent this in the future.

from fourth-edition.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.