畫面大概是這個樣子。
狗1:picturebox2
狗2:picturebox3
狗3:picturebox4
整體架構來說會是這個樣子
由於書上的說明其實滿清楚了,那樣就來貼上程式碼吧....
From.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
namespace 賽狗場的一天
{
public partial class Form1 : Form
{
Random myrandom = new Random();
Greyhound[] DOG = new Greyhound[4];
Guy[] Person = new Guy[3];
public Form1()
{
InitializeComponent();
DOG = new Greyhound[] { new Greyhound(pictureBox2) { indexdog = 1 }, new Greyhound(pictureBox3) { indexdog = 2 }, new Greyhound(pictureBox4) { indexdog = 3 }};
Person = new Guy[]
{
new Guy(){ Name="Joe ",cash=100, MyRadioButton=JOE_radioButton1,Mylabel=joeBet_lable},
new Guy(){ Name="Bob ",cash=100, MyRadioButton=BOB_radioButton2,Mylabel=BobBet_lable},
new Guy(){ Name="AI ",cash=100,MyRadioButton=AI_radioButton3,Mylabel=AIBet_lable},
};
initiafrom(); //初始化數據
}
public void initiafrom()
{
foreach (Guy guy in Person)
{
guy.clearBet();
guy.updatelabels();
}
}
private void T1(int index, PictureBox number)
{
//設定狗。跑道初始值
// DOG[index] = new Greyhound();
DOG[index].MyDOG = number;
DOG[index].startingPosition = 10;
DOG[index].TakeStartingPositon();
DOG[index].Racetracklength = pictureBox1.ClientSize.Width - DOG[index].MyDOG.Width;
DOG[index].r = myrandom;
}
private void T2(int index, Label number, RadioButton radio, string _name)
{ //設定玩家
Person[index] = new Guy();
Person[index].Mylabel = number;
Person[index].MyRadioButton = radio;
Person[index].Name = _name;
Person[index].cash = 100;
Person[index].PlaceBet(0, 0);
Person[index].updatelabels();
}
private void button2_Click(object sender, EventArgs e) //進行賽狗
{
//設定賽狗編號
groupBox1.Enabled = false;
while (true)
{
foreach (Greyhound greyhound in DOG)
if (greyhound.Run())
{
MessageBox.Show(greyhound.indexdog.ToString() + "號狗贏了");
EndRace(greyhound.indexdog);
return;
}
else
{
Application.DoEvents();
}
}
}
private void button1_Click(object sender, EventArgs e)
{
int Dog_No = (int)dog_numericUpDown2.Value; //設定下注狗的編號
#region 下注者的radionButton
if (JOE_radioButton1.Checked)
{
Person[0].MyBet.Bettor = Person[0];
Person[0].MyBet.Amount = (int)money_numericUpDown1.Value;
if (Person[0].PlaceBet(Person[0].MyBet.Amount, Dog_No) == false)
{
return;
}
joeBet_lable.Text = Person[0].MyBet.GetDescription();
}
if (BOB_radioButton2.Checked)
{
Person[1].MyBet.Bettor = Person[1];
Person[1].MyBet.Amount = (int)money_numericUpDown1.Value;
if (Person[1].PlaceBet(Person[1].MyBet.Amount, Dog_No) == false)
{
return;
}
BobBet_lable.Text = Person[1].MyBet.GetDescription();
}
if (AI_radioButton3.Checked)
{
Person[2].MyBet.Bettor = Person[2];
Person[2].MyBet.Amount = (int)money_numericUpDown1.Value;
if (Person[2].PlaceBet(Person[1].MyBet.Amount, Dog_No) == false)
{
return;
}
AIBet_lable.Text = Person[2].MyBet.GetDescription();
}
#endregion
}
private void Form1_Load(object sender, EventArgs e)
{
// T2(0, label1.play1, "pictureBox1");
}
private void EndRace(int winner)
{
foreach (Greyhound greyhound in DOG)
{
greyhound.TakeStartingPositon();
}
foreach (Guy guy in Person)
{
guy.Collect(winner);
}
groupBox1.Enabled = true;
}
}
}
Greyhound.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace 賽狗場的一天
{
class Greyhound
{
public int indexdog;//狗號碼
public int startingPosition=20; //起點
public int Racetracklength=380;//跑到距離
public PictureBox MyDOG=null; //記錄賽狗 public Label MyDog=null; //紀錄賽狗
public int loaction = 0;
public Random r;
public Greyhound(PictureBox pictureBox) //構造函數:
{
this.MyDOG = pictureBox;
}
public bool Run()
{
//隨機向前移動
//更新picturebox在表單上的位子
r = new Random();
int speed = r.Next(1, 20); //給予隨機移動的距離
Point p = MyDOG.Location;
p.X += speed;
loaction = loaction + speed;
System.Threading.Thread.Sleep(100);
MyDOG.Location = p;
if (loaction>= Racetracklength) //假如狗贏了比賽 ,傳true
{
return true;
}
else return false;
}
public void TakeStartingPositon()
{
this.MyDOG.Location = new Point(this.startingPosition, MyDOG.Location.Y);
//位置重設道起跑點
this.loaction=0;
}
}
}
Greyhound.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 賽狗場的一天
{
class Guy
{
public string Name; //人名
public Bet MyBet=new Bet(); //賭注
public int cash; //剩下的錢
public RadioButton MyRadioButton;
public Label Mylabel;
public void updatelabels()
{
//設定關於下注金額描述的標籤
//設定顯示現金餘額之圓形選紐旁的標籤("joe has 43 bucks")
MyRadioButton.Text = string.Format("{0} has{1} $",this.Name, this.cash);
}
public void clearBet()
{
Mylabel.Text = string.Format("{0}還沒有下注", this.Name);
MyBet.Amount = 0;
//將下注金額重設為0
/* MyBet._Dog = 0;
updatelabels();*/
}
public bool PlaceBet(int BetAmount, int WinDog)
{
//要求mybet 收付款-->金額,狗,投注者
if (BetAmount > this.cash)
{ return false; }
this.MyBet.Amount=BetAmount;
this.MyBet._Dog = WinDog;
this.MyBet.Bettor = this;
Mylabel.Text = MyBet.GetDescription();
return true;
}
public void Collect(int Winner)
{
//傳回贏家及獎金()`,並顯示messagebox
cash += MyBet.payout(Winner);
updatelabels();
if (MyBet._Dog == Winner)
{
MessageBox.Show(MyBet.Bettor.Name + "恭喜贏錢");
}
}
}
}
Bet.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 賽狗場的一天
{
class Bet //應用程式中GUY用來表示下注金額的物件
{
public int Amount; //要求mybet收付款
public int _Dog; //壓哪隻狗
public Guy Bettor; //下注者是誰
public string GetDescription()
{
//傳回一個字串,說明誰在下注,壓多少金額
//以及押哪一隻狗,("job bets 8 on dog # 4"
//假如下注金額為0,表示沒有下注
// job has's placed a bet
if (Amount > 0)
{
return Bettor.Name + " Bet " + Amount + " on " + _Dog;
}
else return Bettor.Name + "沒有下注";
}
public int payout(int winner)
{
//參數winner代表營的狗
//假如押注的狗輸了,傳回等量的下注金額
//否則,傳回負值的下注金額。
if (winner == _Dog)
{
return Amount;
}
else return -Amount;
}
}
}
留言列表