O'Reilly Forums: Lab #1 - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Lab #1 returning value true or false to Run method

#1 User is offline   Zarzelius 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 03-October 12

Posted 06 October 2012 - 05:34 AM

Hi guys.I know there are a zillion topics about this lab, but since im new to C#(i only coded html, css and some c a looong time ago XD) i wanted to ask a very specific question ive got and i couldnt find the answer here.

I always try to start with each step as i feel i should , without following guidelines at heart.Then, when i understand the problem (if my program aint working) i see why and try to apply some other ideas .
In the case here, could i use this to make the dogs stop?

public bool Run()
{
while (location < raceTrackLenght)
{
location += Randomizer.Next(5); //Move forward either 1, 2 , 3 or 4 spaces randomly
Point p = myPictureBox.Location; //Update the position of my picturebox on the form
p.X = location;
myPictureBox.Location = p;
}
return false;


I Think that would make ALL the dogs run to the end of the track, but basically, we want that, and then tell who won.If it is in fact , possible to use a while loop on the Run(), then how could i tell which one reached the location>raceTrackLengh condition first?

Thanx in advance !!
0

#2 User is offline   Sikta_26785 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 67
  • Joined: 18-May 11

Posted 06 October 2012 - 10:59 AM

I see some issues with your code. First of all, what is the point in returning a bool if it will always be false? You might as well define the method as public void Run() in this case. Also, this is going to have each dog run from beginning to end 1 at a time. You could count the number of times through the loop and use this as the amount of time it took them to run the race, return that value as your return type from the run method and whichever had the smallest number won the race. But it would be more like individual time trials and not really a "race" then.
0

#3 User is offline   Zarzelius 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 03-October 12

Posted 06 October 2012 - 12:56 PM

View PostSikta_26785, on 06 October 2012 - 10:59 AM, said:

I see some issues with your code. First of all, what is the point in returning a bool if it will always be false? You might as well define the method as public void Run() in this case. Also, this is going to have each dog run from beginning to end 1 at a time. You could count the number of times through the loop and use this as the amount of time it took them to run the race, return that value as your return type from the run method and whichever had the smallest number won the race. But it would be more like individual time trials and not really a "race" then.

First , thanx for your reply, is good to see people take the time to check some new user's problems.
I probably didnt make myself clear, but this is not the whole program, just the "Run" method.
As for the bool, im following the book example here, 2nd edition , page 172 in this case i would use Return to check who won the race, because the first one to reach the line would then get OUT of the while loop, I do think ive made a mistake and the Return outside the While loop should be True, then use that return in my code to end the race and mark the winner.
0

#4 User is offline   Sikta_26785 

  • Active Member
  • PipPipPip
  • Group: Members
  • Posts: 67
  • Joined: 18-May 11

Posted 06 October 2012 - 04:09 PM

Have you tried your code to see the results? I don't see anywhere in your while loop that you are switching dogs. This means it will move one dog over and over until it reaches the finish line and the other ones will stay put. I assume you have some kind of loop calling this run method as well that would cycle through the dogs. So the first one would run until it reaches the finish line, then the second one would run until it reaches the finish line, etc.
0

Share this topic:


Page 1 of 1
  • 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