坚持首先C#lab1

时间:2016-05-18 18:53:28

标签: c# winforms

我正在阅读Head First C#书,我被困在第一个实验室。我已经掌握了很多东西,但我已经遇到了一些障碍,我把头发拉了过来。

  1. 我似乎无法让myBet.GetDescription()中的Guy工作,即使看起来所有的部分都在那里。

  2. 当我点击比赛按钮时,就好像它被卡在无限循环中一样。几乎像raceTrackLength没有终点。

  3. 任何帮助都非常感谢。

    这是我到目前为止的代码。

    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;
            }
        }
    };
    

    修改 我似乎已经在你的帮助下想出了这两个问题的解决方案,但是现在我似乎无法让竞赛停止。他们只是循环到起点,而不是仅仅结束比赛。我一直在玩它并在过去一小时内更换比赛按钮但没有成功。任何帮助?

        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 个答案:

答案 0 :(得分:2)

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