Head First C# 賽狗日


基本功能達到了,有些自己修改了下,不好的地方,歡迎指正!

下載地址:http://files.cnblogs.com/xiyueD/HeadFirst_07_DogRace.7z

賽狗類代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;                                         //窗體控件類
using System.Drawing;                                               //Point類
namespace HeadFirst_07_DogRace
{
    /// <summary>
    /// 賽狗類
    /// </summary>
    class GreyHound
    {
        public Point StaringPositing;                               //開始位置  作者開始用的int ,我覺得使用point ,更方便
        public int RacetrackLength;                                 //賽道長度
        public PictureBox MyPictureBox = null;                      //賽狗圖片對象
        public int Location = 0;                                    //在賽道上的位置,橫坐標X
        public Random randomizer;                                   //每次移動的隨機值
        public int Index;                                           //賽狗編號  后來添加的,為了知道哪只賽狗贏了比賽

        
        public bool Run()                                           //
        {
            Point p = MyPictureBox.Location;
            p.X += randomizer.Next(20);
            MyPictureBox.Location = p;
            Location = p.X;
            if (Location>=RacetrackLength)
            {
                return true;
            } 
            else
            {
                return false;
            }
        }
        //保存初始圖像位置
        public void TakeStartingPositing()                          //初始化位置
        {
            StaringPositing = MyPictureBox.Location;
        }
        //自己添加的構造函數
        public GreyHound(PictureBox picDog)
        {
            MyPictureBox = picDog;
        }
    }
}

 

賭注類代碼

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

namespace HeadFirst_07_DogRace
{
    /// <summary>
    /// 賭注類
    /// </summary>
    class Bet
    {
        public int Amount;                              //賭注金額
        public int Dog;                                 //下注賽狗編號
        public Guy Bettor;                              //投注人
        //這個函數沒寫,作者寫的我不明白
        public string GetDescription()
        {
            return "";
        }
        /// <summary>
        /// 返回贏/輸金額
        /// </summary>
        /// <param name="Winner">贏得勝利賽狗編號</param>
        /// <returns></returns>
        public int PayOut(int Winner)
        {
            if (Dog == Winner)
                return Amount;
            else
                return -Amount;
        }
    }
}

下注人類代碼

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace HeadFirst_07_DogRace
{
    /// <summary>
    ///  下注人類
    /// </summary>
    class Guy
    {
        public string Name;                                         //下注人姓名
        public Bet MyBet;                                           //賭注對象
        public int Cash;                                            //總金額

        public RadioButton MyRadioButton;
        public Label MyLabel;
        /// <summary>
        /// 更新投注人信息,投注對象信息
        /// </summary>
        public void UpDateLabel()
        {
            if (MyBet != null)
            {
                MyLabel.Text = Name + " bet's " + MyBet.Amount + " bucks on dog #" + MyBet.Dog;
                MyRadioButton.Text = Name + " has " + Cash + " bucks";
            }
            else
            {
                MyLabel.Text = Name + " hasn't palced a bet.";
                MyRadioButton.Text = Name + " has " + Cash + " bucks";
            }
        }
        /// <summary>
        /// 清除賭注
        /// </summary>
        public void ClearBet()
        {
            MyBet = null;
            UpDateLabel();
        }
        /// <summary>
        /// 生成投注
        /// </summary>
        /// <param name="Amount">金額</param>
        /// <param name="Dog">賽狗編號</param>
        public void PlaceBet(int Amount, int Dog)
        {
            if (Amount <= Cash && Amount > 0)
            {
                MyBet = new Bet() { Amount = Amount, Dog = Dog};
            }
            else
            {
                MessageBox.Show(Name + " 沒有 " + Amount + "$ 投注給 #" + Dog);
            }
        }
        /// <summary>
        /// 結算比賽后金額
        /// </summary>
        /// <param name="Winner">勝利者</param>
        public void Collect(int Winner)
        {
            if (MyBet == null) return;
            Cash += MyBet.PayOut(Winner);
        }
    }
}

 

界面代碼

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;
using CCWin;
namespace HeadFirst_07_DogRace
{
    public partial class Main : CCSkinMain
    {
        #region 初始化對象,參數,界面信息
        Random random = new Random();
        GreyHound[] Dog = new GreyHound[4];
        PictureBox[] picDog = new PictureBox[4];
        Bet[] bet = new Bet[3];
        Guy[] guyArry = new Guy[3];
        public Main()
        {
            InitializeComponent();
            labMinimumBet.Text = "Minimum bet: " + numUDMoney.Minimum.ToString() + " bucks!";
            picDog[0] = picDog1;
            picDog[1] = picDog2;
            picDog[2] = picDog3;
            picDog[3] = picDog4;
            for (int i = 0; i < 4; i++)
            {
                Dog[i] = new GreyHound(picDog[i]);
                Dog[i].Index = i + 1;
                Dog[i].RacetrackLength = 490;
                Dog[i].randomizer = random;
                Dog[i].TakeStartingPositing();
            }
            guyArry[0] = new Guy() { Name = "Joe", Cash = 50, MyRadioButton = rbtnJoe, MyLabel = labJoe };
            guyArry[1] = new Guy() { Name = "Bob", Cash = 75, MyRadioButton = rbtnBob, MyLabel = labBob };
            guyArry[2] = new Guy() { Name = "Al", Cash = 45, MyRadioButton = rbtntAl, MyLabel = labAl };
            foreach (Guy guy in guyArry)
            {
                guy.UpDateLabel();
            }
        }
        #endregion

        #region  投注
        private void btnBeats_Click(object sender, EventArgs e)
        {
            foreach (Guy guy in guyArry)
            {
                if (guy.MyRadioButton.Checked)
                {
                    guy.PlaceBet((int)numUDMoney.Value, (int)numUDDog.Value);
                    guy.UpDateLabel();
                }
            }
        }
        #endregion

        #region 比賽
        private void btnRace_Click(object sender, EventArgs e)
        {
            btnBeats.Enabled = false;
            bool win = false;
            while (!win)
            {
                foreach (GreyHound dog in Dog)
                {
                    win = dog.Run();
                    if (win)
                    {
                        MessageBox.Show(dog.Index + "號贏得勝利");
                        foreach (Guy guy in guyArry)
                        {
                            guy.Collect(dog.Index);
                            guy.UpDateLabel();
                        }
                        break;      //贏得比賽后退出循環
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(20);
                        Application.DoEvents();
                    }
                }
            }
            foreach (GreyHound dog in Dog)
            {
                //賽狗圖片復位
                dog.MyPictureBox.Location = dog.StaringPositing;
            }
            foreach (Guy guy in guyArry)
            {
                //清除賭注對象,並更新信息
                guy.ClearBet();
            }
            btnBeats.Enabled = true;
        }
        #endregion
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM