head first C# 赛狗日

本文记录了作者使用C#完成HeadFirst教程中的赛狗日实验过程,包括类的设计、实例化及遇到的问题,如访问权限错误、定时器控制及图片重置等问题,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

距离第一次更新,已经过去好多天了。有动力学习,但是码不动笔记。。。非常忧伤。

这次主要是做完了head first中的第一实验:赛狗日,以此留恋。同时提出一些我遇到的问题和我的解决办法。

  1.       在类数组定义和引用时遇到的一些问题和一些要注意的方面:
首先我在命名空间中声明了Guy,Greyhound和Bet三个公共类型
    public class Greyhound
    {
        public int StartingPosition;
        public int RacetrackLength;
        public int Location = 0;
        public PictureBox MyPictureBox;
        public Random Randomizer;
        public bool Run()
        {
            int temp;
            bool IsGetEnd = false;
            Location += Randomizer.Next(0, 50);
            temp = MyPictureBox.Location.Y;
            MyPictureBox.Location = new Point(StartingPosition + Location,temp);
            if(Location >= RacetrackLength)
            {
                IsGetEnd = true;
            }
            return IsGetEnd;
        }
        public void TakeStartingPosition()
        {
            MyPictureBox.Location = new Point(StartingPosition-MyPictureBox.Width , MyPictureBox.Location.Y);
            Location = 0;
        }
    }
    public class Guy
    {
        public string Name;
        public Bet MyBet;
        public int Cash;
        public RadioButton MyRadioButton;
        public Label MyLabel;

        public void UpdateLabels()
        {
            MyLabel.Text = MyBet.GetDescription();
  
        }
        public void ClearBet()
        {
            MyBet.Amount = 0;
        }
        public bool PlaceBet(int BetAmount,int DogToWin)
        {
            MyBet.Bettor = this;
            MyBet.Amount = BetAmount;
            MyBet.Dog = DogToWin;
            bool IsCashEnough = true;
            if(BetAmount > this.Cash)
            {
                UpdateLabels();
                IsCashEnough =!IsCashEnough;
                return IsCashEnough;
            }
            else
            {

                Cash -= BetAmount;
            }
            return IsCashEnough ;
        }
        public void Collect(int Winner)
        {
            Cash += MyBet.PayOut(Winner);
            MyRadioButton.Text = Name + " has " + Cash + " bucks";
            if (MyBet.PayOut(Winner) > 0)
                MyLabel.Text = Name + " won " + MyBet.PayOut(Winner)  + " bucks";
            else
            {
                MyLabel.Text = Name + " loss " + MyBet.Amount  + " bucks";
            }
        }
    }
    public class Bet


    {
        public int Amount;
        public int Dog;
        public Guy Bettor;
        public string GetDescription()

        {
            string Description="";
            if(this.Bettor.Cash-Amount   >= 0)
            {
                Description = Bettor.Name + " bets " + Amount + " on dog #" + Dog;
            }
            else if(Amount == 0)
            {
                Description = Bettor.Name + " hasn't placed a bet " ;
            }
            else

            {
                Description = Bettor.Name + " only has "+Bettor.Cash +" not enough to pay off";
            }
            return Description;
        }
        public int PayOut(int Winner)
        {
            if (Dog == Winner)
            {
                return Amount * 2;
            }
            else
            { 
                return 0;
            }
        }
    }

但是当我在命名和调用的时候却出现了一个难以理解的问题,“访问可用性不一致”也就是找不到声明。这个问题困扰了我很久,直到我后来将声明放在

    public partial class Form1 : Form
    {
        Random MyRandomizer = new Random();

        Greyhound[] Greyhounds = new Greyhound[4];
        Guy[] Guys = new Guy[3];

然后在public Form1()函数中进行初始化才能正常工作。这个问题还有待日后进一步学习了解后解决。

2.第二个问题关于定时器timer的使能和停止:
我在何时调用timer.stop()这个函数上也遇到了一些小的问题,即在某些位置上调用这个方法并不能使定时器关闭或者说这个方法没有被执行。
  private void RaceBeginButton_Click(object sender, EventArgs e)


        {
            timer1.Start();
            BetPanel.Enabled  = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < 4; i++)
            {
                if(Greyhounds[i].Run())
                {
                    i++;                                      //获得正确的胜利者序号
                    timer1.Stop();
                    for (int n = 0; n < 3; n++)
                    {
                        Guys[n].Collect(i);
                    }							//结算
             //       MessageBox.Show("No." + i + "Won!");   模块测试
		//初始化
                    BetPanel.Enabled = true;                  
                    for (int m = 0; m < 4; m++)
                    {
                        Greyhounds[m].TakeStartingPosition();
                    }
                    for (int k = 0; k < 3; k++)
                    {
                        Guys[k].ClearBet();
                    }
                }
            }

比如说当我将timer1.Stop();放在messageBox.Show();的后一句时,则会出现MessageBox被反复调用的情况,从而无法正常终止定时器。

猜测可以是messageBox.show()的调用时间与定时器世界发生冲突。

3.在比赛结束后,重置图片位置时,如果是一二号获得胜利,会出现图片重置失败的问题:

3,4号图片会超过起跑线一个身位,目前没有找到原因。

还有一些要注意的细节,类成员在赋初值的时候须用“,”分隔开!!!而不是“;”看似小问题,有时候却会给自己造成很多不必要的麻烦。

还有PictureBox.Right等是只读的,当我们需要改变图片控件的位置时需要调用PictureBox.Location(x,y)这个函数。

下面是我的一些不成熟的代码,分享给大家。希望大家看过之后可以给出一些建议或者对新手有所帮助。(注释就没有加了,因为书上都有)

namespace FuckingDog
{

    public partial class Form1 : Form
    {
        Random MyRandomizer = new Random();
        Greyhound[] Greyhounds = new Greyhound[4];
        Guy[] Guys = new Guy[3];
        bool EndFlag = false;
        public Form1()
        {
            InitializeComponent();
            Greyhounds[0] = new Greyhound()
            {
                MyPictureBox = GreyhoundPicture1,
                StartingPosition = GreyhoundPicture1.Right,
                RacetrackLength = RacePicture.Right - GreyhoundPicture1.Right,
                Randomizer = MyRandomizer
            };
            Greyhounds[1] = new Greyhound()
            {
                MyPictureBox = GreyhoundPicture2,
                StartingPosition = GreyhoundPicture2.Right,
                RacetrackLength = RacePicture.Right - GreyhoundPicture2.Right,
                Randomizer = MyRandomizer
            };
            Greyhounds[2] = new Greyhound()
            {
                MyPictureBox = GreyhoundPicture3,
                StartingPosition = GreyhoundPicture3.Right,
                RacetrackLength = RacePicture.Right - GreyhoundPicture3.Right,
                Randomizer = MyRandomizer
            };
            Greyhounds[3] = new Greyhound()
            {
                MyPictureBox = GreyhoundPicture4,
                StartingPosition = GreyhoundPicture4.Right,
                RacetrackLength = RacePicture.Right - GreyhoundPicture4.Right,
                Randomizer = MyRandomizer
            };
            Guys[0] = new FuckingDog.Guy()
            {
                Name = "Joe",
                Cash = 50,
                MyBet = new FuckingDog.Bet(),
                MyLabel = JoeBetlabel,
                MyRadioButton = JoeEnsureButton
            };
            Guys[1] = new FuckingDog.Guy()
            {
                Name = "Bob",
                Cash = 75,
                MyBet = new FuckingDog.Bet(),
                MyLabel = BobBetlabel,
                MyRadioButton = BobEnsureButton
            };
            Guys[2] = new FuckingDog.Guy()
            {
                Name = "Al",
                Cash = 45,
                MyBet = new FuckingDog.Bet(),
                MyLabel = AlBetlabel,
                MyRadioButton = AlEnsureButton
            };
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void BetsButton_Click(object sender, EventArgs e)
        {
            if(JoeEnsureButton.Checked )
            {
                if(Guys[0].PlaceBet((int)BetNumber.Value, (int)DogNumber.Value))
                Guys[0].UpdateLabels();
            }
            if (BobEnsureButton.Checked)
            {
                if (Guys[1].PlaceBet((int)BetNumber.Value, (int)DogNumber.Value))
                    Guys[1].UpdateLabels();
            }
            if (AlEnsureButton.Checked)
            {
                if (Guys[2].PlaceBet((int)BetNumber.Value, (int)DogNumber.Value))
                    Guys[2].UpdateLabels();
            }
        }

        private void RaceBeginButton_Click(object sender, EventArgs e)
        {
            timer1.Start();
            BetPanel.Enabled  = false;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            for (int i = 0; i < 4; i++)
            {
                if(Greyhounds[i].Run())
                {
                    i++;
                    timer1.Stop();
                    for (int n = 0; n < 3; n++)
                    {
                        Guys[n].Collect(i);
                    }
             //       MessageBox.Show("No." + i + "Won!");
                    BetPanel.Enabled = true;

                    for (int m = 0; m < 4; m++)
                    {
                        Greyhounds[m].TakeStartingPosition();
                    }
                    for (int k = 0; k < 3; k++)
                    {
                        Guys[k].ClearBet();
                    }
                }
            }
        }

    }
    public class Greyhound
    {
        public int StartingPosition;
        public int RacetrackLength;
        public int Location = 0;
        public PictureBox MyPictureBox;
        public Random Randomizer;
        public bool Run()
        {
            int temp;
            bool IsGetEnd = false;
            Location += Randomizer.Next(0, 50);
            temp = MyPictureBox.Location.Y;
            MyPictureBox.Location = new Point(StartingPosition + Location,temp);
            if(Location >= RacetrackLength)
            {
                IsGetEnd = true;
            }
            return IsGetEnd;
        }
        public void TakeStartingPosition()
        {
            MyPictureBox.Location = new Point(StartingPosition-MyPictureBox.Width , MyPictureBox.Location.Y);
            Location = 0;
        }
    }
    public class Guy
    {
        public string Name;
        public Bet MyBet;
        public int Cash;
        public RadioButton MyRadioButton;
        public Label MyLabel;

        public void UpdateLabels()
        {
            MyLabel.Text = MyBet.GetDescription();
  
        }
        public void ClearBet()
        {
            MyBet.Amount = 0;
        }
        public bool PlaceBet(int BetAmount,int DogToWin)
        {
            MyBet.Bettor = this;
            MyBet.Amount = BetAmount;
            MyBet.Dog = DogToWin;
            bool IsCashEnough = true;
            if(BetAmount > this.Cash)
            {
                UpdateLabels();
                IsCashEnough =!IsCashEnough;
                return IsCashEnough;
            }
            else
            {

                Cash -= BetAmount;
            }
            return IsCashEnough ;
        }
        public void Collect(int Winner)
        {
            Cash += MyBet.PayOut(Winner);
            MyRadioButton.Text = Name + " has " + Cash + " bucks";
            if (MyBet.PayOut(Winner) > 0)
                MyLabel.Text = Name + " won " + MyBet.PayOut(Winner)  + " bucks";
            else
            {
                MyLabel.Text = Name + " loss " + MyBet.Amount  + " bucks";
            }
        }
    }
    public class Bet
    {
        public int Amount;
        public int Dog;
        public Guy Bettor;
        public string GetDescription()
        {
            string Description="";
            if(this.Bettor.Cash-Amount   >= 0)
            {
                Description = Bettor.Name + " bets " + Amount + " on dog #" + Dog;
            }
            else if(Amount == 0)
            {
                Description = Bettor.Name + " hasn't placed a bet " ;
            }
            else
            {
                Description = Bettor.Name + " only has "+Bettor.Cash +" not enough to pay off";
            }
            return Description;
        }
        public int PayOut(int Winner)
        {
            if (Dog == Winner)
            {
                return Amount * 2;
            }
            else
            { 
                return 0;
            }
        }


    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值