O'Reilly Forums: Lab 1: Getting The Dogs Moving? - O'Reilly Forums

Jump to content

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

Lab 1: Getting The Dogs Moving?

#1 User is offline   JDCAce 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 09-February 12

Posted 09 February 2012 - 08:00 PM

So I've gotten pretty much everything coded for the first lab, A Day At The Races, but I'm now stuck on how to program the actual race. This is what I have now:

In Greyhound.cs:
Randomizer = new Random();
for(int i = this.StartingPosition; i < RacetrackLength; i++)
{
    Point p = this.MyPictureBox.Location;
    p.X += Randomizer.Next(5);
    MyPictureBox.Location = p;
}


In Form1.cs:
private void raceButton_Click(object sender, EventArgs e)
{
    dogs[0].Run();
    dogs[1].Run();
    dogs[2].Run();
    dogs[3].Run();
}


When I click the "Race!" button, Dog #1 teleports to the end of the track, while Dog #2 teleports about 3/4 of the way, Dog #3 teleports about 1/2 of the way, and Dog #4 teleports about 1/4 of the way.

I have two problems, and I have a feeling they're related: 1) How do I get them to slowly trot along, like in the sample program you can download from this site?
2) How do I actually make it so #1 isn't always in the lead?
0

#2 User is offline   JDCAce 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 09-February 12

Posted 09 February 2012 - 08:10 PM

An idea popped into my head right after I submitted my question, and I have solved the teleporting problem. I have implemented a Timer. Here is my new code:

In Greyhound.cs:
Randomizer = new Random();
Point p = this.MyPictureBox.Location;
p.X += Randomizer.Next(5);
MyPictureBox.Location = p;


In Form1.cs
private void raceButton_Click(object sender, EventArgs e)
{
    timer1.Enabled = true;
}

private void timer1_Tick(object sender, EventArgs e)
{
    dogs[0].Run();
    dogs[1].Run();
    dogs[2].Run();
    dogs[3].Run();
}


However, the dogs move forward the exact same amount as each other every tick. When Dog #1 moves forward 2, so do the other three. Also, the ticks appear to be in a cycle. That is, it appears the random number generator is spitting out a repeating pattern.
0

#3 User is offline   Sikta_26785 

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

Posted 09 February 2012 - 08:15 PM

I assume that was your Run() method in greyhound? Right now you have it go until your dog finishes the race. Change it to only move once per call and return whether the dog has crossed the finish line like this:

      public bool Run()
      {
        int distance = this.Randomizer.Next(1, 5);
        Point p = this.MyPictureBox.Location;

        p.X += distance;
        this.MyPictureBox.Location = p;
        if (this.MyPictureBox.Location.X >= this.RacetrackLength)
          return true;
        else
          return false;
      }


Then in the raceButton_Click you can cycle through the dogs until you have a winner:
    private void RaceBtn_Click(object sender, EventArgs e)
    {
      bool isThereWinner = false;

      while (!isThereWinner)
      {
        for (int i = 0; i < 4; i++)
        {
          if (dog[i].Run())
          {
            isThereWinner = true;

            //put some logic for handling the betting results here
            break;
          }
          System.Threading.Thread.Sleep(3);
        }
      }
    }

0

#4 User is offline   JDCAce 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 09-February 12

Posted 09 February 2012 - 08:20 PM

Cycle through the dogs! Why didn't I think of that? Thanks for the help!

EDIT: However, now Dog #1 is always the first winner after I start up the program. If I perform another race without reopening the program, the winner is random. Why is the first race different, and how do I let other dogs win?

This post has been edited by JDCAce: 09 February 2012 - 09:16 PM

0

#5 User is offline   Diaz 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 30-September 12

Posted 04 October 2012 - 11:40 AM

Can someone please publish a proper code for using Timer here?

1. Without changing Run() method
2. By keeping RACEButton_Clicked() event


is it possible? I am trying very hard... still not able to get it fixed. Easy way is DoEvents. But I want to implement the timer here.
I believe everything else of my code works fine... excep this... part..

Thank you for any reasonable guidance.

 JDCAce, on 09 February 2012 - 08:20 PM, said:

Cycle through the dogs! Why didn't I think of that? Thanks for the help!

EDIT: However, now Dog #1 is always the first winner after I start up the program. If I perform another race without reopening the program, the winner is random. Why is the first race different, and how do I let other dogs win?

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