基本功能达到了,有些自己修改了下,不好的地方,欢迎指正!
下载地址:https://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 } }