O'Reilly Forums: Day At The Races - O'Reilly Forums

Jump to content

  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Day At The Races Are you nuts?

#1 User is offline   McSemboy 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 17-July 10

Posted 17 July 2010 - 04:49 PM

alright let me begin by saying this is really good book! awesome

and is really good at keeping your head into it by jokes and seriousness when it needs to be.


anyways everything went great until i came across "Day at the races"

first of all i have no idea how to work with pictures or graphical things

and in the book there is no were that says something about that


until it says "you have to make this program about 4 dogs racing "

yeah right?


anyways i am really in shock , i thought it was for beginners and suddenly i have to creat big program such as this.

so i am asking you guys? is this normal reaction ? from me

i really want to learn programing but this book felt a bit pushy like this (( beginners and suddenly to pro) that is how it felt for me , so now my question is , if i just ignore this part em i going to understand what is next in-stored for me ?

also you guys have any ideas of websites that gives ideas of what kind of program i should do ?
for (beginners) (remember that)


thanks for replay tongue.gif , once again it´s great book i hope i wont give up on this that easy
0

#2 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 802
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 18 July 2010 - 04:30 PM

QUOTE (McSemboy @ Jul 17 2010, 04:49 PM) <{POST_SNAPBACK}>
anyways everything went great until i came across "Day at the races"

first of all i have no idea how to work with pictures or graphical things

and in the book there is no were that says something about that

until it says "you have to make this program about 4 dogs racing "

yeah right?


It's certainly a bigger project than anything you did in chapters 1 through 4, but if you did all of the projects in those chapters then you really do have all the tools you need to solve it! And you'll definitely learn a lot in the process.

As far as the graphics go, I know it seems crazy when you first see it, but take a closer look -- those are just PictureBox controls, just like the one you used in chapter 1. Here's something to help you get started. First, download the greyhound graphics from the website. Then start a new Windows Forms Applications project, drag a PictureBox control onto it, and set the image to the greyhound (just like in chapter 1). Then go to the fourth page of the lab, in the section that says "Your object can control things on your form..." -- see that bit of code? That's how you make a PictureBox move. See if you can make the PictureBox move around the form, either using a DoEvents loop (like in FlashyThing at the end of chapter 2), or if you're using the second edition, a Timer.

If you get that working, see if you can create a Dog object with a Run() method that makes the PictureBox move. The lab has some details on how to do that. Once you've got that far, you're already done with some of the hardest bits of the lab.

If it seems too overwhelming, definitely move forward in the book. You can always come back to it! Remember, everyone learns differently. Your style might be to keep plugging forward, and then come back to the lab when you've had a chance to let it roll around in the back of your brain for a while.

But I definitely recommend plugging away at it, even if you have to skip ahead for now and come back to it later. There's some good stuff that you only learn when you make a bigger program with moving parts that have to work together. That's the only way I know of to become a better programmer.

I hope this helps -- and also, thanks for the kind words about our book. Let us know how it goes!
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#3 User is offline   BoruBurosu 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 22-July 10

Posted 22 July 2010 - 01:24 PM

I got the same reaction, so I came up with a response of the same magnitude:

SKIPPPPPP!

Awsome book btw, took hours to decide which book to get at Barnes n Nobles ... this book has alot of pictures! biggrin.gif
0

#4 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 802
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 24 July 2010 - 06:46 AM

Thanks for the kind words! After a couple more chapters, I bet you'll come back to the lab and realize that it's not nearly as daunting as it seemed at first.
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#5 User is offline   saleleb 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 30-August 10

Posted 30 August 2010 - 06:49 AM

I am having the same issue as the first gentleman that posted. I thought I was doing really well until I came upon this lab. I have spent days trying to figure out how just do the greyhound class. I love the book in general, but now I am stuck on this project and I am the type of person that does not like to give up and move forward or cheat by looking up code online. I want to figure this out on my own. I will continue to rattle my brain and see what happens. I am going back in the book in hopes to find something I may have over read or just forgotten, but so far nothing.
0

#6 User is offline   anoir 

  • New Member
  • Pip
  • Group: Members
  • Posts: 9
  • Joined: 04-August 10

Posted 30 August 2010 - 06:37 PM

maybe i have java experience before, i don't find any difficulties of that lab and i think it is quite easy actually if you compare it with lab2 and lab3...

the key is that you have to determine the RacetrackLength first. You can move a Greyhound picture box in the end of the runway, and check its location property to know the X axis so that you can find the RacetrackLength.

when the dog runs, simply just move the picture boxes, if its location (X axis) is larger than RacetrackLength, then return true (it wins)

**
public bool Run()
{
MyRandom = new Random();
int r = MyRandom.Next(4);

Point p = MyPictureBox.Location;
p.X = p.X + r;
MyPictureBox.Location = p;

if (p.X >= RacetrackLength)
return true;
else
return false;
}
**
0

#7 User is offline   carcyon 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 08-October 10

Posted 08 October 2010 - 04:25 AM

I have to agree. I'm at Lab #1 now and kinda pissed. If you are a beginner, and you probably will be because this book aims at beginners, there is no way in hell you are up to the challenge.

I wish they would provide more but smaller coding exercises that lead up to such a 'big' program instead of a bit here and there and suddenly throw Lab #1 in your face. I was motivated till Lab #1... now I'm just frustrated. sad.gif

This post has been edited by carcyon: 08 October 2010 - 04:26 AM

0

#8 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 802
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 09 October 2010 - 05:55 AM

If it helps, I've heard other people say exactly the same thing, and then end up doing the lab and finding it really satisfying to finish. There are a lot of lessons that you can only learn by diving into a larger project.

When people get stuck at lab #1, it's usually because they're overwhelmed by its size. They flip back and forth through it, skimming each page, thinking, "There's no way I can do this."

If you're overwhelmed by the size of the lab, then the first thing to do is take a deep breath. You'll be fine. If you understood everything in the book so far, then you have all the knowledge you need to do the lab.

Here are a few tips that have helped other people:
  • Don't think of the lab as one big problem. Try to think of it as a lot of little problems.
  • Read through the whole thing -- and actually read it, not just skim it. Then put it down for an hour. Even better, put it down overnight. Your brain processes problems while you're asleep.
  • Sometimes when you're stuck, it's because you haven't found a good a starting point. Start by creating a project in Visual Studio, then adding each of the classes and filling in the code that's there so far (including the comments), then building the form.
  • Any time you get stuck, don't think, "How do I finish the lab?" Just think, "Okay, what's the next thing I haven't done?"
  • Making visible progress really helps keep you motivated. Make a new, throwaway program that just moves a PictureBox control (using the Location code on the bottom of the page that has the Greyhound class).


This is the first problem in the book that doesn't have a solution. A lot of people get in the habit of checking the solution every time they run into a trouble spot, and that's not an option here. If you're getting frustrated by that, just remember that you can definitely build the lab using nothing but the tools we gave you so far. If there's an actual concept that you just don't understand, the lab is a really good tool for realizing that and fixing it.

I hope this helps!
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#9 User is offline   woodsonjt 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 18-October 10

Posted 19 October 2010 - 04:23 AM

it's a very well written assignment....challenging....for us...but enough clues to pull it together...

i need a hint though smile.gif

i'm struggling with the randomizer. I declared the randomizer in the greyhound class as suggested in the book (public Random Randomizer;)...but get a null reference error when using it....

then...declared it as
public Random Randomizer = new Random();

this was the only way i could get it to work...but obviously now i'm running into the bug you warned about in the book....each greyhound instance has its own random() object....and they get the same random seed.

i've tried declaring it in the form initiation....but that doesn't work (don't quite understand why since it is public)

went back and looked at the sandwich maker app...and it seems to be set up exactly the same....but doesn't work....

this is clearly a very important thing to get straight since random seeds are part of most programs...i am just not sure where my error in thinking is...

help!!....lost in the random woods
thanks!!
-Justin




QUOTE (AndrewStellman @ Oct 9 2010, 05:55 AM) <{POST_SNAPBACK}>
If it helps, I've heard other people say exactly the same thing, and then end up doing the lab and finding it really satisfying to finish. There are a lot of lessons that you can only learn by diving into a larger project.

When people get stuck at lab #1, it's usually because they're overwhelmed by its size. They flip back and forth through it, skimming each page, thinking, "There's no way I can do this."

If you're overwhelmed by the size of the lab, then the first thing to do is take a deep breath. You'll be fine. If you understood everything in the book so far, then you have all the knowledge you need to do the lab.

Here are a few tips that have helped other people:
  • Don't think of the lab as one big problem. Try to think of it as a lot of little problems.
  • Read through the whole thing -- and actually read it, not just skim it. Then put it down for an hour. Even better, put it down overnight. Your brain processes problems while you're asleep.
  • Sometimes when you're stuck, it's because you haven't found a good a starting point. Start by creating a project in Visual Studio, then adding each of the classes and filling in the code that's there so far (including the comments), then building the form.
  • Any time you get stuck, don't think, "How do I finish the lab?" Just think, "Okay, what's the next thing I haven't done?"
  • Making visible progress really helps keep you motivated. Make a new, throwaway program that just moves a PictureBox control (using the Location code on the bottom of the page that has the Greyhound class).


This is the first problem in the book that doesn't have a solution. A lot of people get in the habit of checking the solution every time they run into a trouble spot, and that's not an option here. If you're getting frustrated by that, just remember that you can definitely build the lab using nothing but the tools we gave you so far. If there's an actual concept that you just don't understand, the lab is a really good tool for realizing that and fixing it.

I hope this helps!


0

#10 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 802
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 19 October 2010 - 08:46 PM

QUOTE (woodsonjt @ Oct 19 2010, 08:23 AM) <{POST_SNAPBACK}>
i'm struggling with the randomizer. I declared the randomizer in the greyhound class as suggested in the book (public Random Randomizer;)...but get a null reference error when using it....

then...declared it as
public Random Randomizer = new Random();

this was the only way i could get it to work...but obviously now i'm running into the bug you warned about in the book....each greyhound instance has its own random() object....and they get the same random seed.

i've tried declaring it in the form initiation....but that doesn't work (don't quite understand why since it is public)


Don't worry -- all of these objects and references will make a lot more sense as you go through the next couple of chapters.

Here's what's going on. When you put this inside the Greyhound class:

CODE
class Greyhound {
public Random Randomizer = new Random();
// ... rest of the class ...
}


then, as you saw, it gives each Greyhound its own instance of Random(). The bug happens because each Random object picks the same random numbers. If they all shared the same instance of Random, then calling its Next() method would pick different random numbers each time.

But you also saw that if you did this:

CODE
class Greyhound {
public Random Randomizer;
// ... rest of the class ...
}

// and later in the Form
Dogs[0] = new Greyhound() { MyPictureBox = dogPictureBox1 /* etc. */ };


then you got a NullReferenceException.

The reason is because the Greyhound object's Random field is set to null, so when you try to call its Next() method like this:

Randomizer.Next(1, 4);

it throws the exception.

What that's telling you is that you need to set that Randomizer field to something.

Go back to the sandwich example and take a close look at exactly how the MenuMaker object is initialized:

MenuMaker menu = new MenuMaker() { Randomizer = new Random() };

Notice how you used that object initializer to set its Randomizer field?

So in this case, you want to create a single instance of Random, and then set each Greyhound object's Randomizer field to contain a reference to that same Random object.

In case it's still not clear, here's another hint -- I stuck it inside a spoiler, because you should try to figure it out first (there's a good lesson in there).

(since this is a spoiler, you need to highlight it with your mouse to see the text)


Here's what to do.

At the top of your form, add this: public Random Randomizer = new Random();

Then, when you initialize each Greyhound object, you have two options. You can set its Randomizer field in the object initializer, which would look something like this:

Dogs[1] = new Greyhound()
{
MyPictureBox = pictureBox1,
StartingPosition = startingPosition,
RacetrackLength = racetrackLength,
Randomizer = randomizer
};

or you can do it explicitly:

Dogs[1] = new Greyhound();
Dogs[1].Randomizer = Randomizer;
// ... etc. ...


Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#11 User is offline   woodsonjt 

  • New Member
  • Pip
  • Group: Members
  • Posts: 5
  • Joined: 18-October 10

Posted 20 October 2010 - 04:30 AM

that was VERY enlightening....it is NOT however how i ended up solving it....

the thing is i didn't really understand WHY it worked when i fixed it...but it did....

what I ended up doing was
placing the "new" keyword inside the Run() method, leaving the delcaration

public Random Randomizer;

in the greyhound class. this worked liked a charm and was an easy fix at the end of the day...but it still seems to me like i am creating new random objects for each instance of greyhound....seems like you are doing that by definition if you place it in the class.


thanks again for the great reply!!
0

#12 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 802
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 24 October 2010 - 08:06 AM

QUOTE (woodsonjt @ Oct 20 2010, 08:30 AM) <{POST_SNAPBACK}>
that was VERY enlightening....it is NOT however how i ended up solving it....

the thing is i didn't really understand WHY it worked when i fixed it...but it did....


Want to know -- in my opinion -- the difference between someone who becomes a good programmer and someone who becomes a great one? It's the refusal to ever put up with not understanding why something works (or doesn't work). Remember, there's always an explanation for every problem -- and every solution.

I think I have a good idea of why your program worked, and you've got a real good opportunity here to figure this out for yourself. If you do, I guarantee that you will take a major step towards becoming a better C# developer.

Here's what I think you should do. There should only be one line where you use new Random() to create a new instance of Random. Put a breakpoint on that line and run the program. I suspect that your program will run that line each time any of your dogs moves one pace.

Take a minute and use a pencil and paper to draw pictures of the objects in the heap, just like I did earlier in chapter 4 with the Dog objects. (It helps to label them "Random object #1", "Random object #2", etc.) Can you figure out what is on the heap at any time, and what objects are marked for garbage collection?

Now here's your goal: to change your program so that the new Random() statement is only executed once. The way you do that is create the instance of Random first, before you create any of the Greyhound objects. Then you make each of the Greyhound's Randomizer fields point to that same Random instance. And you'll know you got it right if your program works, the dogs are all calling the Next() method properly, but the new statement was only called once.

If you can get that working, and understand exactly what's going on and what objects are on the heap, then you will be in extremely good shape for understanding the next few chapters.
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#13 User is offline   Elenaro 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 03-November 10
  • Location:Australia

Posted 03 November 2010 - 09:41 PM

I must say that the first Lab was really challenging and it took me a few days to figure it all out, but it was really satisfying in the end tongue.gif
I am half way through the book now and have just finished second Lab which, I thought, was a piece of cake compared to the first one. But really enjoyable too. laugh.gif Can't wait to get to the end of the book and make Invaders next! wink.gif
0

#14 User is offline   kschug 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 04-November 10

Posted 04 November 2010 - 11:48 AM

I can't believe I'm hung up on this but ...
I'm having trouble with a Point reference.

I have the line:
Point p = MyPictureBox.Location;

But when I compile I get: "The type or namespace name "Point" could not be found."

I have added "using System.Windows.Forms" to the beginning of the file and still have the problem.

Other references (e.g. to PictureBox and Random) are working fine.

Thanks for the help.
0

#15 User is offline   Majik_00 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 04-November 10

Posted 04 November 2010 - 03:08 PM

QUOTE (kschug @ Nov 4 2010, 11:48 AM) <{POST_SNAPBACK}>
I can't believe I'm hung up on this but ...
I'm having trouble with a Point reference.

I have the line:
Point p = MyPictureBox.Location;

But when I compile I get: "The type or namespace name "Point" could not be found."

I have added "using System.Windows.Forms" to the beginning of the file and still have the problem.

Other references (e.g. to PictureBox and Random) are working fine.

Thanks for the help.



try adding using System.Drawing; at the top of your greyhound class that should take care of the problem. smile.gif
0

#16 User is offline   kschug 

  • New Member
  • Pip
  • Group: Members
  • Posts: 2
  • Joined: 04-November 10

Posted 04 November 2010 - 07:13 PM

QUOTE (Majik_00 @ Nov 4 2010, 03:08 PM) <{POST_SNAPBACK}>
try adding using System.Drawing; at the top of your greyhound class that should take care of the problem. smile.gif



That worked! Thanks for the help.
0

#17 User is offline   Majik_00 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 04-November 10

Posted 05 November 2010 - 08:28 AM

QUOTE (kschug @ Nov 4 2010, 07:13 PM) <{POST_SNAPBACK}>
That worked! Thanks for the help.



My pleasure.
0

#18 User is offline   Ayeohx 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 20
  • Joined: 04-November 10

Posted 06 November 2010 - 02:51 AM

QUOTE (Majik_00 @ Nov 4 2010, 03:08 PM) <{POST_SNAPBACK}>
try adding using System.Drawing; at the top of your greyhound class that should take care of the problem. smile.gif


The only problem I got right so far.

I think if I had less information I'd be better off. I'm finding myself trying to use the puzzle pieces that I've been given and it's interrupting my own troubleshooting process.

Of course, there's a good chance, left to my own devices, that I'd be terribly lost (more so than now).

And I'll choose to take Andrew's "if you don't have patience you'll never be a real programmer" comment as a challenge rather than a death knell. I keep quitting other C# lessons right around this stage so I'll press on.

We'll see how I feel in an hour though.

This post has been edited by Ayeohx: 06 November 2010 - 02:51 AM

0

#19 User is offline   Ayeohx 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 20
  • Joined: 04-November 10

Posted 06 November 2010 - 09:25 PM

I've noticed that I'm having an issue with scope - where variables should go, how they are accessed by other classes and methods and how to write proper code to access them. I think a little more focus on scope in the previous chapters, or a scope chapter before this, would have helped.
0

#20 User is offline   Majik_00 

  • New Member
  • Pip
  • Group: Members
  • Posts: 6
  • Joined: 04-November 10

Posted 07 November 2010 - 09:16 PM

QUOTE (Ayeohx @ Nov 6 2010, 09:25 PM) <{POST_SNAPBACK}>
I've noticed that I'm having an issue with scope - where variables should go, how they are accessed by other classes and methods and how to write proper code to access them. I think a little more focus on scope in the previous chapters, or a scope chapter before this, would have helped.



What do you mean exactley? Post code or give an example and we can help you out on the forums.
0

Share this topic:


  • 2 Pages +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users