简体   繁体   中英

Stuck on Head First C# lab1

I'm going through the Head First C# book and I'm stuck on the first lab. I've got pretty much everything up and running, but I've hit a couple snags that I'm pulling my hair out over.

  1. I can't seem to get get myBet.GetDescription() in the Guy to work even though it seems like all the pieces are there.

  2. When I click the race button, It's as if it is stuck in an infinite loop. Almost like the raceTrackLength doesn't have an end point.

Any help is well appreciated.

Here's the code of what I have so far.

Greyhound.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace WindowsFormsApplication6
{
    class Greyhound
    {

        public int StartingPosition; //Where my picturebox starts
        public int RaceTrackLength; //how long the racetrack is
        public PictureBox myPictureBox = null; //my picturebox object
        public int Location = 0; //My location on racetrack
        public Random Randomizer; //instance of Random



        public bool Run() //use randomizer like in the sub shop example
        {
            //Random Randomizer = new Random();

            int randomMovement = Randomizer.Next(1, 4);

            Point currentPos = myPictureBox.Location;
            currentPos.X += randomMovement;
            myPictureBox.Location = currentPos;

                    //Move forward either 1,2, 3, or 4 spaces at random
                    //update the position of my picture box on form
                    //return true if I won the race

            if (currentPos.X >= RaceTrackLength)
            {
                return true;
            }
            else
                return false;

        }

        public void TakeStartingPosition()
        {
            //reset location to the start line
            myPictureBox.Left = StartingPosition;
            Location = 0;
        }

    }
}

Guy.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
    class Guy
    {

        public string name; //the guy's name
        public Bet myBet = null; //an instance of Bet() that has his bet
        public int cash; //how much cash he has

        public RadioButton myRadioButton;
        public Label myLabel;

        public void UpdateLabels()
        {
            //set my label to my bet's description, and the label on my
            // radio button to show my cash ('Joe has 43 bucks')

            if (myBet == null)
            {
                myLabel.Text = name + " hasn't placed a bet";
            }
            else {
                myLabel.Text = myBet.GetDescription();
                //Why isn't this working??
            }

            myRadioButton.Text = name + " has " + cash + " bucks";

        }
        public void ClearBet() 
        {
            //clear the betting table
            myBet.amount = 0;
        }

        public bool PlaceBet(int amount, int Dog)
        {
            Bet myBet = new Bet();
                myBet.Dog = Dog;
                myBet.amount = amount;
                myBet.Bettor = this;
                UpdateLabels();



            //place a new bet and store it in my bet field
            //return true if the guy had enough money to bet

            if (amount >= cash)
            {
                return false;
            }
            else
                return true;
        }

        public void Collect(int Winner) {
            cash += myBet.PayOut(Winner);
        } //ask my bet to pay out
    }
}

Bet.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication6
{
    class Bet
    {
        public int amount; //amount of cash that was bet
        public int Dog; //The number of the dog the bet is on
        public Guy Bettor; //the guy who placed the bet

        public string GetDescription()
        {
            //return a string that says who placed the bet, how much
            //cash was bet, and which dog he bet on ("Joe bets 8 on
            //dog #4"). If the amount is zero, no bet was placed
            //("Joe hasn't placed a Bet.")

            if (amount > 0)
            {
                 return Bettor.name + " bets " + amount + "on dog #" + Dog;
            }
            else
                return Bettor.name + " has not placed a bet!";
        }

        public int PayOut(int Winner)
        {

            //the parameter is the winner of the race. if the dog won, 
            //returns the amount bet. Otherwise, return the negative of
            //the amount bet. 

            if (Dog == Winner)
            {
                return amount;
            }
            else
                return -amount;
        }
    }
}

Form1.cs

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {

        //initialize dog and guy arrays
        Greyhound[] dogs = new Greyhound[4];
        Guy[] guys = new Guy[3];
        Random myRandom = new Random();


        public Form1()
        {
            InitializeComponent();

            //guys object initialization
            guys[0] = new Guy()
            {
                cash = 50,
                myLabel = joeBetLabel,
                myRadioButton = joeRadioButton,
                name = "Joe"

            };

            guys[1] = new Guy()
            {
                cash = 75,
                myLabel = bobBetLabel,
                myRadioButton = bobRadioButton,
                name = "Bob"
            };

            guys[2] = new Guy()
            {
                cash = 45,
                myLabel = alBetLabel,
                myRadioButton = alRadioButton,
                name = "Al",

            };

            for (int i = 0; i < guys.Length; i++)
            {
                guys[i].UpdateLabels();
            }



            //dogs object
            dogs[0] = new Greyhound()
            {
                myPictureBox = dogPictureBox1,
                StartingPosition = raceTrackPictureBox.Left,
                RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox1.Width,
                Randomizer = myRandom
            };

            dogs[1] = new Greyhound()
            {
                myPictureBox = dogPictureBox2,
                StartingPosition = raceTrackPictureBox.Left,
                RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox2.Width,
                Randomizer = myRandom
            };

            dogs[2] = new Greyhound() 
            {
                myPictureBox = dogPictureBox3,
                StartingPosition = raceTrackPictureBox.Left,
                RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox3.Width,
                Randomizer = myRandom
            };

            dogs[3] = new Greyhound()
            {
                myPictureBox = dogPictureBox4,
                StartingPosition = raceTrackPictureBox.Left,
                RaceTrackLength = raceTrackPictureBox.Width - dogPictureBox4.Width,
                Randomizer = myRandom
            };


        }

        private void betButton_Click(object sender, EventArgs e)
        {

            if (sender == alRadioButton)
            {

                if (!guys[2].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                {
                    //display message saying you can't bet that amount  
                    MessageBox.Show("You can not bet this amount.", "Unable to bet amount");
                }
            }
            else if (sender == joeRadioButton)
            {
                if (!guys[0].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                {
                    //display message saying you can't bet that amount  
                    MessageBox.Show("You can not bet this amount.", "Unable to bet amount");
                }
            }
            else if (sender == bobRadioButton)
            {
                if (!guys[1].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value))
                {
                    //display message saying you can't bet that amount  
                    MessageBox.Show("You can not bet this amount!", "Unable to bet amount");
                }
            }
            for (int i = 0; i < guys.Length; i++)
            {
                guys[i].PlaceBet((int)betNumericUpDown.Value, (int)dogNumericUpDown.Value);
                guys[i].UpdateLabels();
            }
        }

        private void raceButton_Click(object sender, EventArgs e)
        {

            bool noWinner = true;
            int dogWon;

            while (noWinner)
            {
                for (int i = 0; i < dogs.Length; i++)
                {
                    if (dogs[i].Run())
                    {
                        dogWon = i + 1;
                    }
                }
            }
        }

        private void joeRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            label2.Text = guys[0].name;
        }

        private void bobRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            label2.Text = guys[1].name;
        }

        private void alRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            label2.Text = guys[2].name;
        }
    }
};

EDIT I've seemed to have figured out with your help what the solution to both questions were, however now I can't seem to get the race to STOP. They just keep looping around to the the starting point instead of just ending the race. I've been playing with it and changing the race button around for the past hour with no success. Any help??

    private void raceButton_Click(object sender, EventArgs e)
    {

        bool noWinner = false;
        int dogWon;

        while (!noWinner)
        {
           Application.DoEvents();

            for (int i = 0; i < dogs.Length; i++)
            {
                if (dogs[i].Run())
                {
                    dogWon = i +1;
                    MessageBox.Show("The Winner is dog #" + dogWon, "Winner!");
                    for (int j = 0; j < dogs.Length; j++)
                    {
                        dogs[j].TakeStartingPosition();
                    }
                }
            }
        }
    }

不确定问题1,但对于问题2,没有任何事情将noWinner设置为false,因此它将无限循环。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM