Klasa Guy:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DayAtTheRaces
{
public class Guy
{
private int Cash;
private string Name;
private Label MyLabel;
public RadioButton MyRadioButton;
public Bet MyBet;
public Guy(int cash, string name, Label myLabel, RadioButton myRadioButton)
{
Cash = cash;
Name = name;
MyLabel = myLabel;
MyRadioButton = myRadioButton;
}
public bool PlaceBet(int amount, int dogToWin)
{
MyBet = new Bet() { Amount = amount, Dog = dogToWin, Bettor = this };
if (this.MyBet.Bettor.Cash > 5)
return true;
else
return false;
}
public void ClearBet()
{
MyBet = null;
}
public void UpdateLabels()
{
if (this.MyBet == null)
this.MyLabel.Text = Name + " nie postawił zakładu.";
else
this.MyLabel.Text = Name + " postawił " + MyBet.Amount + " na psa numer " + MyBet.Dog + ".";
}
public void Collect(int Winner)
{
this.Cash += MyBet.PayOut(Winner);
}
public void UpdateRadios()
{
this.MyRadioButton.Text = Name + " ma " + Cash + " zł.";
}
}
}
Klasa Greyhound:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DayAtTheRaces
{
public class Greyhound
{
private PictureBox MyPictureBox;
public PictureBox RaceTrackLength;
private static Random Randomizer = new Random();
public Greyhound(PictureBox myPictureBox,PictureBox raceTrackLength)
{
MyPictureBox = myPictureBox;
RaceTrackLength = raceTrackLength;
}
public void Run()
{
MyPictureBox.Left += Randomizer.Next(1, 5);
}
public void TakeStartingPosition()
{
MyPictureBox.Left = RaceTrackLength.Left;
}
public bool IsTheRaceFinished()
{
return MyPictureBox.Right >= RaceTrackLength.Width;
}
}
}
Klasa Bet:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DayAtTheRaces
{
public class Bet
{
public int Amount;
public int Dog;
public Guy Bettor;
public int PayOut(int Winner)
{
if (Bettor.MyBet.Dog == Winner)
return Amount;
else
return -Amount;
}
}
}
I cały formularz:
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;
namespace DayAtTheRaces
{
public partial class Form1 : Form
{
Greyhound[] GreyhoundArray = new Greyhound[4];
Guy[] GuyArray = new Guy[3];
public Form1()
{
InitializeComponent();
GreyhoundArray[0] = new Greyhound(dogPicture1, raceTrackPictureBox);
GreyhoundArray[1] = new Greyhound(dogPicture2, raceTrackPictureBox);
GreyhoundArray[2] = new Greyhound(dogPicture3, raceTrackPictureBox);
GreyhoundArray[3] = new Greyhound(dogPicture4, raceTrackPictureBox);
GuyArray[0] = new Guy(100, "Janek", joeBetLabel, joeRadioButton);
GuyArray[1] = new Guy(150, "Bartek", bobBetLabel, bobRadioButton);
GuyArray[2] = new Guy(50, "Arek", alBetLabel, alRadioButton);
foreach (Guy Guy in GuyArray)
{
Guy.UpdateLabels();
Guy.UpdateRadios();
}
}
private void startButton_Click(object sender, EventArgs e)
{
groupBox1.Enabled = false;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
for (int i = 0; i < GreyhoundArray.Length; i++)
{
GreyhoundArray[i].Run();
if(GreyhoundArray[i].IsTheRaceFinished())
{
timer1.Stop();
i++;
MessageBox.Show("Pies numer " + i + " zwycięzył.", "Mamy zwycięzcę.");
groupBox1.Enabled = true;
for (int j = 0; j < GuyArray.Length; j++)
{
if (GuyArray[j].MyBet == null)
break;
else
{
GuyArray[j].Collect(i);
GuyArray[j].UpdateRadios();
GuyArray[j].ClearBet();
GuyArray[j].MyRadioButton.Enabled = true;
GuyArray[j].MyRadioButton.Checked = false;
nameLabel.Text = "Imię";
}
GuyArray[j].UpdateLabels();
}
}
}
}
private void joeRadioButton_CheckedChanged(object sender, EventArgs e)
{
nameLabel.Text = "Janek";
}
private void bobRadioButton_CheckedChanged(object sender, EventArgs e)
{
nameLabel.Text = "Bartek";
}
private void alRadioButton_CheckedChanged(object sender, EventArgs e)
{
nameLabel.Text = "Arek";
}
private void betButton_Click(object sender, EventArgs e)
{
int guyNumber = 3;
if (joeRadioButton.Checked) guyNumber = 0;
if (bobRadioButton.Checked) guyNumber = 1;
if (alRadioButton.Checked) guyNumber = 2;
GuyArray[guyNumber].PlaceBet((int)betNumericUpdAndDown.Value, (int)dogNumericUpAndDown.Value);
GuyArray[guyNumber].UpdateLabels();
GuyArray[guyNumber].MyRadioButton.Enabled = false;
}
private void resetButton_Click(object sender, EventArgs e)
{
foreach (Greyhound Dog in GreyhoundArray)
{
Dog.TakeStartingPosition();
}
}
}
}
Tyle co mi się udało wymyślić przez 1 dzień kod w formularzu zmniejszył się o 6 linijek, może o trochę więcej bo dodałem przycisk do resetu toru, ponieważ po resecie z pętli psy jeszcze przeskakiwały na formularzu o wartość Random która im pozostała po skończonym wyścigu co powodowało, że nie ustawiały się na starcie w jednej pozycji.