head first C# 賽狗日

距離第一次更新,已經過去好多天了。有動力學習,但是碼不動筆記。。。非常憂傷。

這次主要是做完了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;
            }
        }


    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章