实验背景:
人:Joe、Bob和AI希望参见赛狗赌博。最初,Joe有50元,Bob有75元,AI有45元。每次比赛前,他们都会各自决定是否下注以及所押的赌金。直到比赛前,他们都可以改变赌金,但是一旦比赛开始,赌金就再不能更改了。
赌场:赌场会跟踪每个人持有的现金,以及每个人下注的对象。每次下注至少5元。一场比赛中,赌场对每个人只取一次赌金;也就是说每个人不得重复下注。赌场会检查下注的人确实有足够的现金支付他的赌金,所以如果没有钱来作赌资这个人就不能下注。
下注:每次下注都会“翻倍或全陪”,要求最低5元,而且每个人对一只狗最多下注15元。
比赛:有4只狗在直道上比赛。比赛胜者是第一只穿过终点线的狗。
书中给出的作为参考的类图:
根据这些类图得到如下代码:
namespace 赛狗日 { public partial class frmDogRace : Form { private Guy[] guys;//人物类集合 private Greyhound[] greyhounds;//狗类集合 public frmDogRace() { InitializeComponent(); greyhounds = new Greyhound[] { new Greyhound(picDog1){Index=1}, new Greyhound(picDog2){Index =2}, new Greyhound(picDog3){Index =3}, new Greyhound(picDog4){Index=4}, }; guys = new Guy[] { new Guy(){ Name="Joe", Cash=50, MyRadioButton=rad0,MyLabel=labJoe}, new Guy(){ Name="Bob", Cash=75, MyRadioButton=rad1,MyLabel=labBob}, new Guy(){ Name="AI", Cash=45,MyRadioButton=rad2,MyLabel=labAI}, }; InitialForm(); } /// <summary> /// 初始化数据 /// </summary> public void InitialForm() { foreach (Guy guy in guys) { guy.ClearBet(); guy.UpdateRadio(); } } private void radJoe_Click(object sender, EventArgs e) { labName.Text = ((RadioButton)sender).Tag.ToString(); labName.Tag = ((RadioButton)sender).Name.ToString().Substring(((RadioButton)sender).Name.ToString().Length-1, 1); } private void btnBets_Click(object sender, EventArgs e) { int i=int.Parse( labName.Tag.ToString()); Guy guy = guys[i]; if (!guy.PlaceBet(int.Parse(numAmount.Value.ToString()), int.Parse(numDog.Value.ToString()))) { MessageBox.Show(guy.Name+"赌金不足,无法下注","提示"); } } private void btnRun_Click(object sender, EventArgs e) { //比赛开始后将不能下注 btnBets.Enabled = false; while (true) { foreach (Greyhound greyhound in greyhounds) { if (greyhound.Run()) { MessageBox.Show(greyhound.Index.ToString() + "号猎犬赢得比赛!"); EndRace(greyhound.Index); return; } else { //暂停循环,刷新窗体 Application.DoEvents(); } } } } /// <summary> /// 比赛结束需要进行的操作 /// </summary> private void EndRace(int Winner) { foreach (Greyhound greyhound in greyhounds) { greyhound.TakeStartingPosition(); } foreach (Guy guy in guys) { guy.Collect(Winner); } btnBets.Enabled = true; } } /// <summary> /// 狗类 /// </summary> public class Greyhound { public int Index;//狗的号码 public int StartingPosition =25;//图片起始坐标 public int RacetrackLength = 455;//路程 public PictureBox MyPictureBox;//图片控件 public int Location = 0;//以完成路程 public Random Randomizer;//速度随即生成变量 public Greyhound(PictureBox picturebox) { this.MyPictureBox = picturebox; } /// <summary> /// 狗开始跑 /// </summary> /// <returns>是否获得第一</returns> public bool Run() { Randomizer = new Random(); int speed = Randomizer.Next(1,20); Point p = MyPictureBox.Location; p.X += speed; Location += speed; System.Threading.Thread.Sleep(100); MyPictureBox.Location = p; if (Location >= RacetrackLength) { return true; } else { return false; } } /// <summary> /// 回复原位 /// </summary> public void TakeStartingPosition() { this.MyPictureBox.Location = new Point(this.StartingPosition,MyPictureBox.Location.Y); this.Location = 0; } } /// <summary> /// 人物类 /// </summary> public class Guy { public string Name;//姓名 public Bet Mybet=new Bet ();//赌注信息 public int Cash;//剩余现金 public RadioButton MyRadioButton; public Label MyLabel; /// <summary> /// 下注更新显示信息 /// </summary> public void UpdateRadio() { MyRadioButton.Text = string.Format("{0} has {1} bucks",this.Name,this.Cash.ToString()); } /// <summary> /// 重置赌注为0 /// </summary> public void ClearBet() { MyLabel.Text=string.Format("{0} has't placed a bet",this.Name); Mybet.Amout = 0; } /// <summary> /// 下注 /// </summary> public bool PlaceBet(int Amout, int Dog) { if (Amout > this.Cash) return false; this.Mybet.Amout = Amout; this.Mybet.Dog = Dog; this.Mybet.Bettor = this; MyLabel.Text= Mybet.GetDescription(); return true; } /// <summary> /// 清算结果 /// </summary> public void Collect(int Winner) { this.Cash += this.Mybet.PayOut(Winner); UpdateRadio(); ClearBet(); } } /// <summary> /// 赌注类 /// </summary> public class Bet { public int Amout;//下注数量 public int Dog;//猜哪只狗 public Guy Bettor;//下注者 /// <summary> /// 获得下注信息 /// </summary> public string GetDescription() { return Bettor.Name + " bets " + Amout.ToString() + " bucks on dog #" + Dog.ToString(); } /// <summary> /// 清算赛跑结果 /// </summary> public int PayOut(int Winner) { if (Winner == Dog) { return Amout; } else return -Amout; } } }
最后得到的软件运行效果图如下: