-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Chapter 10 Hide and Seek test (HouseTests) #39
Comments
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. |
<!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
-->Hi Andrew thank you for your very quick response! I guess I’m still confused, I mean I do understand the concept of the game and its logic flow. My question is more of the technical type and it lies on the problem of accessing a local variable of a constructor/method outside it, directly by another class (locals should be out of scope). In other words how can hallway be directly accessed from the test class since “hallway” is a local variable to a House class method/constructor. It would need to be accessed through the Entry field member of the class which is the only one that’s public and can get the hallway object as part of its dictionary.I’m even getting red squiggly lines (does not exist in the current context message) under hallway if I try accessing it directly in the test method, but not if I access it as a value of the House.Entry.Exits[] dictionary. Am I not getting it? Sent from Mail for Windows From: Andrew StellmanSent: Sunday, February 27, 2022 11:52 AMTo: head-first-csharp/fourth-editionCc: DarwinsCorn; AuthorSubject: Re: [head-first-csharp/fourth-edition] Chapter 10 Hide and Seek test (HouseTests) (Issue #39) 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.—Reply to this email directly, view it on GitHub, or unsubscribe.Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you authored the thread.Message ID: ***@***.***>
|
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:
That field is set in the House constructor:
And now Entry points to a Location object. The constructor then creates two new Locations objects:
And later it uses Entry.AddExit to add them to the Entry location's exit collection:
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 Does that help make sense of it? |
<!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
code
{mso-style-priority:99;
font-family:"Courier New";}
.MsoChpDefault
{mso-style-type:export-only;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
-->Yes you are correct and I agree with it. I see my problem was that I wasn’t referencing “var hallway” in the test method, and I understood that the solution was calling that reference directly from the “house class” which wouldn’t be possible, and it was why Visual Studio was giving me an out of scope error. Thank you for giving me so much of your time that I can’t possible give back. Sent from Mail for Windows From: Andrew StellmanSent: Sunday, February 27, 2022 1:57 PMTo: head-first-csharp/fourth-editionCc: DarwinsCorn; AuthorSubject: Re: [head-first-csharp/fourth-edition] Chapter 10 Hide and Seek test (HouseTests) (Issue #39) 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?—Reply to this email directly, view it on GitHub, or unsubscribe.Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you authored the thread.Message ID: ***@***.***>
|
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. |
The code below for the test for the House class of the project have the following asserts, for which the the first three seem to be written correctly (they use House.Entry). The ones for kitchen and the rest of the rooms of the house seem to be incorrect as they are defined in the House as "var" within a static constructor and are not field members of the class and thus would not be accessible directly outside it as described in the solution.
Unless I'm missing something let me know.
incorrect (from below):
correct (what it should be):
(Excerpt from the solution) as written in the addendum project solution online:
The text was updated successfully, but these errors were encountered: