3

我创建的灰狗应用程序有问题。当我使用单选按钮选择下注的人(比如说鲍勃)并将他的赌注金额设置为 10 时,在我点击赌注时(见图 #1),以更新“鲍勃没有下任何赌注”带有 description() 方法的标签我得到一个错误(见图#2)

图片 #1

图片 #2

应该发生的是它说“鲍勃没有下任何赌注”,现在应该改为“鲍勃在狗 #4 上下注 10 美元。我已经确定这是失败的,因为投注者返回 NULL,但我不知道为什么会出现这种情况投注者正在引用包含投注者姓名的 Guy 类。

附带说明一下,当比赛结束时,如果 Bob 获胜,他会得到报酬,因此 PayOut() 或 Collect() 方法也不起作用。

以下是我的 3 个课程和我的 Form1.cs

灰狗.cs

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

namespace A_Day_at_the_Races
{
    public 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 the racetrack
        public Random Randomizer; // An instance of Random

        public bool Run()
        {
            //1. Move forward either 1,2,3 or 4 spaces at random
            int moveforward = Randomizer.Next(1, 4); // declare an int called 'moveforward' will move forward 1,2,3 or 4 spaces at random

            //2. Update the position of my PictureBox on the form
            Point p = MyPictureBox.Location; // current location of the picture of the greyhound
            p.X += moveforward;
            MyPictureBox.Location = p;

            //3. Return true if I won the race
            if (p.X >= RacetrackLength)
                return true;
            else
                return false;
        }

        public void TakeStartingPosition()
        {
            //Reset my location to the start line
            //MyPictureBox.Location.X = StartingPosition;

            StartingPosition = 0;
        }
    }
}

盖伊.cs

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

namespace A_Day_at_the_Races
{
    public class Guy
    {
        public string Name; // The guy's name
        public Bet MyBet = null; // An instance of bet() that has how much he's betting
        public int Cash; // How much cash he has
        // these last two fields are the guy's GUI controls on the form
        public RadioButton MyRadioButton; // My RadioButton
        public Label MyLabel; // My Label

        public void UpdateLabels()
        {
            //1.Set my label to my bet's description, 
            if (MyBet == null)
                MyLabel.Text = Name + " hasnt placed any bets";
            else
                MyLabel.Text = MyBet.GetDescription();

            //2.Set the label on my radio button to show my cash ("Joe has 43 dollars")
            MyRadioButton.Text = Name + " has " + Cash + " bucks";
        }

        public void ClearBet()
        {
            //1.Reset my bet so it's zero
            MyBet = null;
        }

        //1.Place a new bet and store it in my bet field
        //2.Return true if the guy had enough money to bet
        public bool PlaceBet(int Amount, int Dog)
        {
            this.MyBet = new Bet();

            if (Cash >= Amount)
            {
                Cash = Cash - Amount;
                MyLabel = new Label();
                MyBet.Amount = Amount;
                MyBet.Dog = Dog;
                UpdateLabels();
                return true;
            }
            else
            {
                return false;
            }
        }

        public void Collect(int Winner)
        {
            if (MyBet != null)
                //1.Ask my bet to pay out (hint use the bet object to do the work)
                Cash += MyBet.PayOut(Winner);
        }
    }
}

投注.cs

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

namespace A_Day_at_the_Races
{
    public class Bet
    {
         public int Amount; // The 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()
         {
             if (Amount > 0)
                 return Bettor.Name + " bets " + Amount + " bucks on dog #" + Dog;
             else
                 return Bettor.Name + " hasnt placed a bet";
         }

         public int PayOut(int Winner)
         {
             if (Winner == Dog)
                 return Amount;
             else
                 return -1 * 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 A_Day_at_the_Races
{
    public partial class Form1 : Form
    {
        Guy[] Bettors;
        Greyhound[] Dogs;
        Guy CurrentBettor;

        public Form1()
        {
            InitializeComponent();
            Random Randomizer = new Random();

            //initialise all my guys and dogs
            Bettors = new Guy[3];
            Dogs = new Greyhound[4];

            //guys
            Bettors[0] = new Guy();
            Bettors[0].Name = "Joe";
            Bettors[0].MyRadioButton = joeRadioButton;
            Bettors[0].MyLabel = joeBetLabel;
            Bettors[0].Cash = 50;
            Bettors[0].UpdateLabels();

            Bettors[1] = new Guy();
            Bettors[1].Name = "Bob";
            Bettors[1].MyRadioButton = bobRadioButton;
            Bettors[1].MyLabel = bobBetLabel;
            Bettors[1].Cash = 75;
            Bettors[1].UpdateLabels();

            Bettors[2] = new Guy();
            Bettors[2].Name = "Al";
            Bettors[2].MyRadioButton = alRadioButton;
            Bettors[2].MyLabel = alBetLabel;
            Bettors[2].Cash = 45;
            Bettors[2].UpdateLabels();


            int StartPosition = pictureBoxDog1.Location.X;
            int distance = pictureBox1.Width;
            for (int i = 0; i < 4; i++)
            {
                Dogs[i] = new Greyhound();
                Dogs[i].Randomizer = Randomizer;
                Dogs[i].RacetrackLength = distance;
                Dogs[i].Location = Dogs[i].StartingPosition = StartPosition;
            }

            Dogs[0].MyPictureBox = pictureBoxDog1;
            Dogs[1].MyPictureBox = pictureBoxDog2;
            Dogs[2].MyPictureBox = pictureBoxDog3;
            Dogs[3].MyPictureBox = pictureBoxDog4;

            CurrentBettor = Bettors[0];
        }

        private void RaceButton_Click(object sender, EventArgs e)
        {
            int winner = 0;
            int num_winners = 0;

            while (num_winners == 0)
            {
                for (int i = 0; i < Dogs.Length; i++)
                {
                    if (Dogs[i].Run())
                    {
                        num_winners++;
                        winner = i + 1;
                    }
                }
                Application.DoEvents();
                System.Threading.Thread.Sleep(3);
            }

            if (num_winners > 1)
                MessageBox.Show("We have " + num_winners + " winners");
            else
                MessageBox.Show(" Dog #" + winner + "wins!");

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

            for (int i = 0; i < Bettors.Length; i ++)
            {
                Bettors[i].Collect(winner);
                Bettors[i].ClearBet();
                Bettors[i].UpdateLabels();
            }

            numericUpDownBet.Value = numericUpDownBet.Minimum;
            numericUpDownDog.Value = numericUpDownDog.Minimum;
        }

        private void joeRadioButton_CheckedChanged(object sender, EventArgs e)
        {
            SetBettor(0);
        }

        private void bobRadioButton_CheckedChanged_1(object sender, EventArgs e)
        {
            SetBettor(1);
        }

        private void alRadioButton_CheckedChanged_1(object sender, EventArgs e)
        {
            SetBettor(2);
        }

        private void BetsButton_Click(object sender, EventArgs e)
        {
            CurrentBettor.PlaceBet((int)numericUpDownBet.Value, (int)numericUpDownDog.Value);
            CurrentBettor.UpdateLabels();
        }
        private void SetBettor(int index)
        {
            CurrentBettor = Bettors[index];
            NameLabel.Text = CurrentBettor.Name;
            if (CurrentBettor.MyBet != null)
            {
                numericUpDownBet.Value = CurrentBettor.MyBet.Amount;
                numericUpDownDog.Value = CurrentBettor.MyBet.Dog;
            }
            else
            {
                numericUpDownBet.Value = numericUpDownBet.Minimum;
                numericUpDownDog.Value = 1;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            minimumBetLabel.Text = "Minimum Bet $5.00";
        }

        private void ResetButton_Click(object sender, EventArgs e)
        {
            pictureBoxDog1.Location = new Point(61,32);
            pictureBoxDog2.Location = new Point(61,84);
            pictureBoxDog3.Location = new Point(61,131);
            pictureBoxDog4.Location = new Point(61,181);
        }
    }
}

Form1.Designer.cs

namespace A_Day_at_the_Races
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            this.pictureBoxDog1 = new System.Windows.Forms.PictureBox();
            this.pictureBoxDog2 = new System.Windows.Forms.PictureBox();
            this.pictureBoxDog3 = new System.Windows.Forms.PictureBox();
            this.pictureBoxDog4 = new System.Windows.Forms.PictureBox();
            this.RaceButton = new System.Windows.Forms.Button();
            this.minimumBetLabel = new System.Windows.Forms.Label();
            this.joeRadioButton = new System.Windows.Forms.RadioButton();
            this.bobRadioButton = new System.Windows.Forms.RadioButton();
            this.alRadioButton = new System.Windows.Forms.RadioButton();
            this.BetsLabel = new System.Windows.Forms.Label();
            this.joeBetLabel = new System.Windows.Forms.Label();
            this.bobBetLabel = new System.Windows.Forms.Label();
            this.alBetLabel = new System.Windows.Forms.Label();
            this.NameLabel = new System.Windows.Forms.Label();
            this.BetsButton = new System.Windows.Forms.Button();
            this.numericUpDownBet = new System.Windows.Forms.NumericUpDown();
            this.label1 = new System.Windows.Forms.Label();
            this.numericUpDownDog = new System.Windows.Forms.NumericUpDown();
            this.ResetButton = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDog1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDog2)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDog3)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDog4)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDownBet)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDownDog)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(12, 12);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(602, 201);
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            // 
            // pictureBoxDog1
            // 
            this.pictureBoxDog1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBoxDog1.Image")));
            this.pictureBoxDog1.Location = new System.Drawing.Point(22, 21);
            this.pictureBoxDog1.Name = "pictureBoxDog1";
            this.pictureBoxDog1.Size = new System.Drawing.Size(75, 21);
            this.pictureBoxDog1.TabIndex = 1;
            this.pictureBoxDog1.TabStop = false;
            // 
            // pictureBoxDog2
            // 
            this.pictureBoxDog2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBoxDog2.Image")));
            this.pictureBoxDog2.Location = new System.Drawing.Point(22, 70);
            this.pictureBoxDog2.Name = "pictureBoxDog2";
            this.pictureBoxDog2.Size = new System.Drawing.Size(75, 22);
            this.pictureBoxDog2.TabIndex = 2;
            this.pictureBoxDog2.TabStop = false;
            // 
            // pictureBoxDog3
            // 
            this.pictureBoxDog3.Image = ((System.Drawing.Image)(resources.GetObject("pictureBoxDog3.Image")));
            this.pictureBoxDog3.Location = new System.Drawing.Point(22, 120);
            this.pictureBoxDog3.Name = "pictureBoxDog3";
            this.pictureBoxDog3.Size = new System.Drawing.Size(75, 24);
            this.pictureBoxDog3.TabIndex = 3;
            this.pictureBoxDog3.TabStop = false;
            // 
            // pictureBoxDog4
            // 
            this.pictureBoxDog4.Image = ((System.Drawing.Image)(resources.GetObject("pictureBoxDog4.Image")));
            this.pictureBoxDog4.Location = new System.Drawing.Point(22, 170);
            this.pictureBoxDog4.Name = "pictureBoxDog4";
            this.pictureBoxDog4.Size = new System.Drawing.Size(75, 24);
            this.pictureBoxDog4.TabIndex = 4;
            this.pictureBoxDog4.TabStop = false;
            // 
            // RaceButton
            // 
            this.RaceButton.Location = new System.Drawing.Point(468, 269);
            this.RaceButton.Name = "RaceButton";
            this.RaceButton.Size = new System.Drawing.Size(146, 73);
            this.RaceButton.TabIndex = 5;
            this.RaceButton.Text = "Race!";
            this.RaceButton.UseVisualStyleBackColor = true;
            this.RaceButton.Click += new System.EventHandler(this.RaceButton_Click);
            // 
            // minimumBetLabel
            // 
            this.minimumBetLabel.AutoSize = true;
            this.minimumBetLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.minimumBetLabel.Location = new System.Drawing.Point(9, 241);
            this.minimumBetLabel.Name = "minimumBetLabel";
            this.minimumBetLabel.Size = new System.Drawing.Size(0, 13);
            this.minimumBetLabel.TabIndex = 6;
            // 
            // joeRadioButton
            // 
            this.joeRadioButton.AutoSize = true;
            this.joeRadioButton.Location = new System.Drawing.Point(12, 269);
            this.joeRadioButton.Name = "joeRadioButton";
            this.joeRadioButton.Size = new System.Drawing.Size(85, 17);
            this.joeRadioButton.TabIndex = 7;
            this.joeRadioButton.TabStop = true;
            this.joeRadioButton.Text = "radioButton1";
            this.joeRadioButton.UseVisualStyleBackColor = true;
            this.joeRadioButton.CheckedChanged += new System.EventHandler(this.joeRadioButton_CheckedChanged);
            // 
            // bobRadioButton
            // 
            this.bobRadioButton.AutoSize = true;
            this.bobRadioButton.Location = new System.Drawing.Point(12, 293);
            this.bobRadioButton.Name = "bobRadioButton";
            this.bobRadioButton.Size = new System.Drawing.Size(85, 17);
            this.bobRadioButton.TabIndex = 8;
            this.bobRadioButton.TabStop = true;
            this.bobRadioButton.Text = "radioButton1";
            this.bobRadioButton.UseVisualStyleBackColor = true;
            this.bobRadioButton.CheckedChanged += new System.EventHandler(this.bobRadioButton_CheckedChanged_1);
            // 
            // alRadioButton
            // 
            this.alRadioButton.AutoSize = true;
            this.alRadioButton.Location = new System.Drawing.Point(12, 317);
            this.alRadioButton.Name = "alRadioButton";
            this.alRadioButton.Size = new System.Drawing.Size(85, 17);
            this.alRadioButton.TabIndex = 9;
            this.alRadioButton.TabStop = true;
            this.alRadioButton.Text = "radioButton2";
            this.alRadioButton.UseVisualStyleBackColor = true;
            this.alRadioButton.CheckedChanged += new System.EventHandler(this.alRadioButton_CheckedChanged_1);
            // 
            // BetsLabel
            // 
            this.BetsLabel.AutoSize = true;
            this.BetsLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.BetsLabel.Location = new System.Drawing.Point(164, 241);
            this.BetsLabel.Name = "BetsLabel";
            this.BetsLabel.Size = new System.Drawing.Size(32, 13);
            this.BetsLabel.TabIndex = 10;
            this.BetsLabel.Text = "Bets";
            // 
            // joeBetLabel
            // 
            this.joeBetLabel.AutoSize = true;
            this.joeBetLabel.Location = new System.Drawing.Point(164, 269);
            this.joeBetLabel.Name = "joeBetLabel";
            this.joeBetLabel.Size = new System.Drawing.Size(35, 13);
            this.joeBetLabel.TabIndex = 11;
            this.joeBetLabel.Text = "label1";
            // 
            // bobBetLabel
            // 
            this.bobBetLabel.AutoSize = true;
            this.bobBetLabel.Location = new System.Drawing.Point(164, 293);
            this.bobBetLabel.Name = "bobBetLabel";
            this.bobBetLabel.Size = new System.Drawing.Size(35, 13);
            this.bobBetLabel.TabIndex = 12;
            this.bobBetLabel.Text = "label1";
            // 
            // alBetLabel
            // 
            this.alBetLabel.AutoSize = true;
            this.alBetLabel.Location = new System.Drawing.Point(164, 317);
            this.alBetLabel.Name = "alBetLabel";
            this.alBetLabel.Size = new System.Drawing.Size(35, 13);
            this.alBetLabel.TabIndex = 13;
            this.alBetLabel.Text = "label1";
            // 
            // NameLabel
            // 
            this.NameLabel.AutoSize = true;
            this.NameLabel.Location = new System.Drawing.Point(9, 359);
            this.NameLabel.Name = "NameLabel";
            this.NameLabel.Size = new System.Drawing.Size(0, 13);
            this.NameLabel.TabIndex = 14;
            // 
            // BetsButton
            // 
            this.BetsButton.Location = new System.Drawing.Point(54, 354);
            this.BetsButton.Name = "BetsButton";
            this.BetsButton.Size = new System.Drawing.Size(75, 23);
            this.BetsButton.TabIndex = 15;
            this.BetsButton.Text = "Bets";
            this.BetsButton.UseVisualStyleBackColor = true;
            this.BetsButton.Click += new System.EventHandler(this.BetsButton_Click);
            // 
            // numericUpDownBet
            // 
            this.numericUpDownBet.Location = new System.Drawing.Point(135, 357);
            this.numericUpDownBet.Minimum = new decimal(new int[] {
            5,
            0,
            0,
            0});
            this.numericUpDownBet.Name = "numericUpDownBet";
            this.numericUpDownBet.Size = new System.Drawing.Size(72, 20);
            this.numericUpDownBet.TabIndex = 16;
            this.numericUpDownBet.Value = new decimal(new int[] {
            5,
            0,
            0,
            0});
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(214, 359);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(113, 13);
            this.label1.TabIndex = 17;
            this.label1.Text = "bucks on dog number ";
            // 
            // numericUpDownDog
            // 
            this.numericUpDownDog.Location = new System.Drawing.Point(334, 356);
            this.numericUpDownDog.Maximum = new decimal(new int[] {
            4,
            0,
            0,
            0});
            this.numericUpDownDog.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.numericUpDownDog.Name = "numericUpDownDog";
            this.numericUpDownDog.Size = new System.Drawing.Size(79, 20);
            this.numericUpDownDog.TabIndex = 18;
            this.numericUpDownDog.Value = new decimal(new int[] {
            1,
            0,
            0,
            0});
            // 
            // ResetButton
            // 
            this.ResetButton.Location = new System.Drawing.Point(468, 349);
            this.ResetButton.Name = "ResetButton";
            this.ResetButton.Size = new System.Drawing.Size(146, 41);
            this.ResetButton.TabIndex = 19;
            this.ResetButton.Text = "Reset";
            this.ResetButton.UseVisualStyleBackColor = true;
            this.ResetButton.Click += new System.EventHandler(this.ResetButton_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(626, 410);
            this.Controls.Add(this.ResetButton);
            this.Controls.Add(this.numericUpDownDog);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.numericUpDownBet);
            this.Controls.Add(this.BetsButton);
            this.Controls.Add(this.NameLabel);
            this.Controls.Add(this.alBetLabel);
            this.Controls.Add(this.bobBetLabel);
            this.Controls.Add(this.joeBetLabel);
            this.Controls.Add(this.BetsLabel);
            this.Controls.Add(this.alRadioButton);
            this.Controls.Add(this.bobRadioButton);
            this.Controls.Add(this.joeRadioButton);
            this.Controls.Add(this.minimumBetLabel);
            this.Controls.Add(this.RaceButton);
            this.Controls.Add(this.pictureBoxDog4);
            this.Controls.Add(this.pictureBoxDog3);
            this.Controls.Add(this.pictureBoxDog2);
            this.Controls.Add(this.pictureBoxDog1);
            this.Controls.Add(this.pictureBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDog1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDog2)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDog3)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBoxDog4)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDownBet)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.numericUpDownDog)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.PictureBox pictureBox1;
        private System.Windows.Forms.PictureBox pictureBoxDog1;
        private System.Windows.Forms.PictureBox pictureBoxDog2;
        private System.Windows.Forms.PictureBox pictureBoxDog3;
        private System.Windows.Forms.PictureBox pictureBoxDog4;
        private System.Windows.Forms.Button RaceButton;
        private System.Windows.Forms.Label minimumBetLabel;
        private System.Windows.Forms.RadioButton joeRadioButton;
        private System.Windows.Forms.RadioButton bobRadioButton;
        private System.Windows.Forms.RadioButton alRadioButton;
        private System.Windows.Forms.Label BetsLabel;
        private System.Windows.Forms.Label joeBetLabel;
        private System.Windows.Forms.Label bobBetLabel;
        private System.Windows.Forms.Label alBetLabel;
        private System.Windows.Forms.Label NameLabel;
        private System.Windows.Forms.Button BetsButton;
        private System.Windows.Forms.NumericUpDown numericUpDownBet;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.NumericUpDown numericUpDownDog;
        private System.Windows.Forms.Button ResetButton;
    }
}
4

4 回答 4

4

PlaceBet您忘记设置 Bettor 属性的方法中:

            if (Cash >= Amount)
            {
                Cash = Cash - Amount;
                MyLabel = new Label();
                MyBet.Amount = Amount; // HERE
                MyBet.Dog = Dog; // HERE
                UpdateLabels();
                return true;

只需添加一行:

                MyBet.Bettor = this;

解释/理由:关键是没有它,当一个“家伙”创建一个“赌注”时,赌注不会知道是谁创造了它。您在代码中的任何地方都没有设置 Bettor 字段,因此它永远没有机会被设置为有意义的东西。投注将仅在此字段中保留默认的 NULL 值。由于我实际上并没有阅读/跟踪所有代码,我认为最简单的方法是让 Bet 从一开始就直接了解他的所有者。

于 2012-08-31T08:45:06.987 回答
2

在 Guy.cs 中,当您调用 PlaceBet(...) 并创建实例化赌注时,您需要设置:

this.MyBet.Bettor = this;

在此函数中,this指的是下注的 Guy 实例。

您遇到的第二个问题是创建 的新实例Label,而实际上并没有用新的标签替换表单上的标签。总之,PlaceBet(...)像这样修复方法:

public bool PlaceBet(int Amount, int Dog)
{
    this.MyBet = new Bet();

    if (Cash >= Amount)
    {
        Cash = Cash - Amount;

        // remove the following line
        // MyLabel = new Label();

        MyBet.Amount = Amount;
        MyBet.Dog = Dog;

        // insert this line...
        MyBet.Bettor = this;

        UpdateLabels();
        return true;
    }
    else
    {
        return false;
    }
}
于 2012-08-31T08:48:06.453 回答
1

我刚刚在书中自己进行了这个练习......一直在运行它。我查看了你的代码,看看你是如何解决这个问题的,我发现你如何处理支付更好的人的一个很大的错误 - 在 form1.cs 中,当你允许你的循环捕获多个获胜者时,你不会继续分配奖金使用 Collect 方法对除最后一只获胜狗之外的任何人下注

while (num_winners == 0)
        {
            for (int i = 0; i < Dogs.Length; i++)
            {
                if (Dogs[i].Run())
                {
                    num_winners++;
                    winner = i + 1;
                }
            }
            Application.DoEvents();
            System.Threading.Thread.Sleep(3);
        }

        if (num_winners > 1)
 // you say that you have multiple winners right here but you never eval on 
 //it - winner is still set to one value above
            MessageBox.Show("We have " + num_winners + " winners"); 
        else
            MessageBox.Show(" Dog #" + winner + "wins!");

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

        for (int i = 0; i < Bettors.Length; i ++)
        {
            Bettors[i].Collect(winner);
            Bettors[i].ClearBet();
            Bettors[i].UpdateLabels();
        }

需要更改哪些内容以纠正此问题并适当分散赌注:

1) 使投注方案更现实 - 下注时 - 减去钱。您需要允许重新投注,以防有人想重做首页上的某个用户

in guy.cs
    public bool PlaceBet(int amount, int dog)
    {
        // place a new bet and store it in the bet field, return true if there is enough money
        if (amount > Cash)
        {
            MessageBox.Show("I don't have enough money for that bet!", Name + " says...");
            return false;
        }
        else
        {
            if (this.MyBet == null)
            {
                this.MyBet = new Bet() { Amount = amount, Bettor = this, Dog = dog };
                this.Cash -= amount;
                this.UpdateLabels();
                return true;
            }
            else
            {
                this.Cash += this.MyBet.Amount;
                this.MyBet = null;
                this.MyBet = new Bet() { Amount = amount, Bettor = this, Dog = dog };
                this.Cash -= amount;
                this.UpdateLabels();
                return true;
            }
        }
    }

2) 现在更好的人已经输掉/提交了他们的钱,如果他们的狗没有赢,我们不必担心向他们发送负数,Bet.cs 支付方法可以更新为仅在以下情况下提供正回报获胜的狗匹配他们的下注狗。如果没有,它什么也不返回。

    public int PayOut(int Winner)
    {
        // the parameter is the winner of the race. If the dog won, return the amount bet.
        // otherwise return nothing

        if (Winner == Dog)
        {
            int payout = Amount*2;
            return payout;
        }
        else
        {
            int payout = 0;
            return payout;
        }        
    }

3)现在我们可以在 form1.cs 中调整我们的方案来处理支付倍数(即使这从未真正发生过)

private void formButtonRace_Click(object sender, EventArgs e)
    {
        int winner = 0;
        int windog = 0;
        int count = 0;
        for (int i = 0; i < Bettors.Length; i++)
        {
            if (Bettors[1].MyBet != null)
            {
                count++;
            }
        }

        if (count == Bettors.Length)
        {
            while (winner == 0)
            {
                for (int i = 0; i < Dogs.Length; i++)
                {
                    if (Dogs[i].Run())
                    {
                        winner++;
                        windog = i + 1;
                        for (int i2 = 0; i2 < Bettors.Length; i2++)
                        {
                            Bettors[i2].Collect(i + 1);
                        }
                    }
                    Application.DoEvents();
                    System.Threading.Thread.Sleep(3);
                }
            }
            if (winner > 1)
            {
                MessageBox.Show("Multiple winners!", "WOW");
            }
            else
            {
                MessageBox.Show("The winner was dog #" + windog);
            }
            for (int i = 0; i < Bettors.Length; i++)
            {
                Bettors[i].ClearBet(true);
            }
            for (int i = 0; i < Dogs.Length; i++)
            {
                Dogs[i].TakeStartingPosition();
            }
        }
        else
        {
            MessageBox.Show("Not all players have placed their bets!", "Wait wait!");
        }
    }

我的 ClearBet 传递布尔值的原因是,如果按下重置按钮,我必须恢复每个 Guy 的赌注。但是,如果比赛结束,我想重置它们而不返回他们的赌注。

这是我的代码中的那个部分:

public void ClearBet(bool isRaceOver)
    {
        if (isRaceOver)
        // reset bet when race is over
        {
            MyBet = null;                
        }
        else
        {
            if (this.MyBet != null)
            {
                Cash += this.MyBet.Amount;
                this.MyBet = null;                 
            }
            else
            {
                this.MyBet = null;
            }                 
        }
        UpdateLabels();
    }
于 2016-05-12T22:41:57.670 回答
0

在 PlaceBet() 方法中,您正在用新的 Label() 覆盖 MyLabel,因此表单上标签的原始上下文丢失了。

尽管您实际上是在更新标签,但它不是表单上的标签。

public bool PlaceBet(int Amount, int Dog)
        {
            this.MyBet = new Bet();

            if (Cash >= Amount)
            {
                Cash = Cash - Amount;
                MyLabel = new Label(); // remove this
                MyBet.Amount = Amount;
                MyBet.Dog = Dog;
                UpdateLabels();
                return true;
            }
            else
            {
                return false;
            }
        }
于 2012-08-31T12:16:20.333 回答