O'Reilly Forums: Day At The Races - Can't Get Dogs To Run - O'Reilly Forums

Jump to content

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

Day At The Races - Can't Get Dogs To Run

#1 User is offline   CubemonkeyNYC 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 10-February 13

Posted 10 February 2013 - 12:08 PM

As stated, I'm trying to write the for () {if()} that the book calls for on page 175 in order to get the dog's picture boxes to move. There's something I'm not grasping. I run a For loop, the picture box location values change by 1-4, then the middle term in the for loop "i < 4" becomes false and the loop ends. How can I have the dogs keep going? I keep trying to think of ways for the For loop to repeat until Dogs.Run() is true, but I can't get it. The Else I have in the code below doesn't seem to matter.

The other issue is that I have no idea how to have the timer start the race. I don't see any way to START the timer_Tick method with the Race button click. So I put the Run() method under the button click.


private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
            int i = 0;
            int Winner;
            bool noWinner = true;

            while (noWinner)
            {
                while (!Dogs[i].Run())
                {
                    for (i = 0; i < 4; i++)
                    {
                        if (i < 4)
                        {
                            Dogs[i].Run();
                        }
                        else
                        {
                            i = 0;
                        }
                    }

                    noWinner = false;
                    Winner = i;
                }



                timer1.Stop();
                MessageBox.Show("We have a winner!");
            }
        }

0

#2 User is offline   ClintMc 

  • New Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 20-February 13

Posted 20 February 2013 - 04:07 PM

View PostCubemonkeyNYC, on 10 February 2013 - 12:08 PM, said:

As stated, I'm trying to write the for () {if()} that the book calls for on page 175 in order to get the dog's picture boxes to move. There's something I'm not grasping. I run a For loop, the picture box location values change by 1-4, then the middle term in the for loop "i < 4" becomes false and the loop ends. How can I have the dogs keep going? I keep trying to think of ways for the For loop to repeat until Dogs.Run() is true, but I can't get it. The Else I have in the code below doesn't seem to matter.


I actually took a different path and separated the start race code from the run code.

when you click the race button it starts the timer
          private void raceButton_Click(object sender, EventArgs e)
        {
            raceTimer.Start();
            groupBox1.Enabled = false;
        }


when the timer is clicked it runs every dog and checks to see if that dog won. I haven't attached all the code but just enough to give you a nudge in the right direction. Let me know if you want me to attach the entire source.
         private void raceTimer_Tick(object sender, EventArgs e)
            // every tick of the timer will have the dogs run and see if they won
            // if a dog has then the timer will stop and bets will be paid out
        {

            for (int i = 0; i <= 3; i++)
            {
                if (dogsInRace[i].Run())
                {
                    raceTimer.Stop();
                    MessageBox.Show("Dog #" + (i + 1) + " has won the race!");

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