人:Joe、Bob和AI希望参见赛狗赌博。最初,Joe有50元,Bob有75元,AI有45元。每次比赛前,他们都会各自决定是否下注以及所押的赌金。直到比赛前,他们都可以改变赌金,但是一旦比赛开始,赌金就再不能更改了。
赌场:赌场会跟踪每个人持有的现金,以及每个人下注的对象。每次下注至少5元。一场比赛中,赌场对每个人只取一次赌金;也就是说每个人不得重复下注。赌场会检查下注的人确实有足够的现金支付他的赌金,所以如果没有钱来作赌资这个人就不能下注。
下注:每次下注都会“翻倍或全陪”,要求最低5元,而且每个人对一只狗最多下注15元。
比赛:有4只狗在直道上比赛。比赛胜者是第一只穿过终点线的狗。
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;
namespace Chapter3
{
public partial class Form1 : Form
{
Greyhound[] greyhounds;
Guy[] guys;
public Form1()
{
InitializeComponent();
greyhounds = new Greyhound[]{
new Greyhound(pictureBox4),
new Greyhound(pictureBox3),
new Greyhound(pictureBox2),
new Greyhound(pictureBox1),
};
guys = new Guy[]{
new Guy(){Name ="小希",Cash=50,MyRadioButton=radioButtonJoe,MyLabel=label2,MyBet = new Bet()},
new Guy(){Name ="小哲",Cash=75,MyRadioButton=radioButtonBob,MyLabel=label2,MyBet = new Bet()},
new Guy(){Name ="小小希",Cash=45,MyRadioButton=radioButtonAI,MyLabel=label2,MyBet = new Bet()},
};
}
private void button1_Click(object sender, EventArgs e)
{
int cashmount = (int)numericUpDown1.Value;
int dognum = (int)numericUpDown2.Value;
int chooseFlag = 1;
if (radioButtonJoe.Checked)
chooseFlag = 1;
else if(radioButtonBob.Checked)
chooseFlag = 2;
else if (radioButtonAI.Checked)
chooseFlag = 3;
switch(chooseFlag)
{
case 1:
try
{
guys[0].PlaceBet(cashmount, dognum);
}
catch(Exception e1)
{
System.Console.WriteLine("现金补足",e1);
}
labelJoe.Text = guys[0].MyBet.GetDescription();
guys[0].UpdateLabels();
break;
case 2:
guys[1].PlaceBet(cashmount, dognum);
labelBob.Text = guys[1].MyBet.GetDescription();
guys[1].UpdateLabels();
break;
case 3:
guys[2].PlaceBet(cashmount, dognum);
labelAI.Text = guys[2].MyBet.GetDescription();
guys[2].UpdateLabels();
break;
}
}
private void button2_Click(object sender, EventArgs e)
{
bool win = false;
int winnum = 0;
string finalName="";
while (!win)
{
for (int i = 0; i < 4; i++)
{
win = greyhounds[i].Run();
if (win)
{
winnum = i + 1;
break;
}
}
}
for (int i = 0; i < 3; i++)
{
guys[i].Collect(winnum);
guys[i].UpdateLabels();
if(guys[i].MyBet.dog==winnum)
finalName+=(guys[i].Name+"\n");
}
if(finalName.Equals(""))
MessageBox.Show("赢得狗是:" + winnum + "号\n" + finalName + "哈哈没人获胜!!");
else
MessageBox.Show("赢得狗是:" + winnum + "号\n"+finalName+"获胜!!");
for (int i = 0; i < 4; i++)
{
greyhounds[i].TakeStartingPosition();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace Chapter3
{
public class Greyhound
{
public int StartingPos;
public int RaceLength=544;
public PictureBox MyPictureBox = null;
public int Location = 0;
public Random Randomizer;
public Greyhound(PictureBox picturebox)
{
this.MyPictureBox = picturebox;
this.StartingPos = picturebox.Location.X;
}
public bool Run()
{
Randomizer = new Random();
Point p = MyPictureBox.Location;
int speed = Randomizer.Next(1,15);
p.X += speed;
System.Threading.Thread.Sleep(10);
MyPictureBox.Location = p;
MyPictureBox.Refresh();
Location = (int)p.X;
if (Location >= RaceLength)
{
return true;
}
else
return false;
}
public void TakeStartingPosition()
{
Point p = MyPictureBox.Location;
p.X = StartingPos;
MyPictureBox.Location = p;
MyPictureBox.Refresh();
}
}
public class Guy
{
public string Name;
public Bet MyBet;
public int Cash;
public RadioButton MyRadioButton;
public Label MyLabel;
public void UpdateLabels()
{
MyRadioButton.Checked = true;
MyRadioButton.Text = Name + " 现金: " + Cash+"$";
MyLabel.Text = Name;
}
public void ClearBet()
{
MyBet.amount = 0;
MyBet.dog = 0;
}
public bool PlaceBet(int amount,int dog)
{
if (amount > Cash)
return false;
else
{
MyBet.Bettor = this;
ClearBet();
MyBet.dog = dog;
MyBet.amount = amount;
return true;
}
}
public void Collect(int Winner)
{
Cash += MyBet.PayOut(Winner);
}
}
public class Bet
{
public int amount;
public int dog;
public Guy Bettor;
public string GetDescription()
{
string mes = "";
if(amount>Bettor.Cash)
{
mes = "You have not enough money to bet!";
}
else if (amount == 0)
{
mes = Bettor.Name + "hasn't placed a bet";
}
else
{
mes = Bettor.Name + " bets "+amount+"$ on dog"+dog;
}
return mes;
}
public int PayOut(int Winner)
{
if(Winner == dog)
return 2 * amount;
else
return -amount;
}
}
}