O'Reilly Forums: Day At The Races - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Day At The Races Betting Logic

#1 User is offline   wunderwaffe 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 26
  • Joined: 14-January 14

Posted 20 January 2014 - 04:40 AM

Hello,

I'm having some trouble with betting. My labels always update to "[name] bets 0 on dog 0" where [name] is the relevant guy. However, my debug logging shows the correct info.

I think the problem is around my understanding of this and how new bets are initialised and referred to.

Guy.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace dugs
{
    public class Guy
    {
        public String Name;
        public Bet MyBet;
        public int Cash;

        public RadioButton MyRadioButton;
        public Label Mylabel;

        public void UpdateLabels()
        {
            Mylabel.Text = Name + " bets " + MyBet.Amount + " on " + MyBet.Dog;
        }

        public void ClearBet()
        {
            //meh stub
        }

        public bool PlaceBet(int Amount, int Dog)
        {
            MyBet = new Bet() { Bettor = this };
            System.Diagnostics.Debug.WriteLine("Amount is " + Amount + " Dog is " + Dog + " Bettor is " + Name);
            return true;
        }

        public void Collect(int Winner)
        {
            //meh stub
        }
    }

    }



From1.cs

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 dugs
{
    public partial class form1 : Form
    {
        Greyhound g1;
        Greyhound g2;
        Greyhound g3;
        Greyhound g4;
        Greyhound[] greyRay;

        Guy m1;
        Guy m2;
        Guy m3;
        Guy[] biGuy;

        int minBet = 5;

        public form1()
        {
            InitializeComponent();
            minBet = 5;
            g1 = new Greyhound() { name = "Brutus1", StartingPosition = pictureBox2.Location, RaceTrackLength = (pictureBox1.Width - pictureBox2.Width), MyPictureBox = pictureBox2 };
            g2 = new Greyhound() { name = "Rover2", StartingPosition = pictureBox3.Location, RaceTrackLength = (pictureBox1.Width - pictureBox2.Width), MyPictureBox = pictureBox3 };
            g3 = new Greyhound() { name = "Pedro3", StartingPosition = pictureBox4.Location, RaceTrackLength = (pictureBox1.Width - pictureBox2.Width), MyPictureBox = pictureBox4 };
            g4 = new Greyhound() { name = "Cadger4", StartingPosition = pictureBox5.Location, RaceTrackLength = (pictureBox1.Width - pictureBox2.Width), MyPictureBox = pictureBox5 };

            greyRay = new Greyhound[4];
            greyRay[0] = g1;
            greyRay[1] = g2;
            greyRay[2] = g3;
            greyRay[3] = g4;

            m1 = new Guy() { Cash = 100, Mylabel = label3, MyRadioButton = joeButton, Name = "Joe" };
            m2 = new Guy() { Cash = 100, Mylabel = label4, MyRadioButton = alButton, Name = "Al" };
            m3 = new Guy() { Cash = 100, Mylabel = label5, MyRadioButton = bobButton, Name = "Bob" };

            biGuy = new Guy[3];
            biGuy[0] = m1;
            biGuy[1] = m2;
            biGuy[2] = m3;

          
        }


        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (Greyhound g in greyRay)
            {
                g.TakeStartingPosition();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            button1.Enabled = false;

            for (int i = 0; i < greyRay.Length; i++)
            {
                if (greyRay[i].Run() == true)
                {
                    timer1.Stop();
                    MessageBox.Show("Dog " + greyRay[i].name + " wins");
                    foreach (Greyhound g in greyRay)
                    {
                        g.TakeStartingPosition();
                    }
                    button1.Enabled = true;

                }
                // else{g

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
              for(int i = 0; i < biGuy.Length; i++){
                  int stake = Convert.ToInt32(numericUpDown1.Value);
                  int dogNo = Convert.ToInt32(numericUpDown2.Value);
                if((label6.Text != "name") && (biGuy[i].Cash >= stake) && (biGuy[i].MyRadioButton.Checked)){
                    biGuy[i].PlaceBet(stake, dogNo);
                    biGuy[i].UpdateLabels();
                }
        }
    }

        private void joeButton_CheckedChanged(object sender, EventArgs e)
        {
            if (joeButton.Checked)
            {
                label6.Text = "Joe";
            }
        }

        private void bobButton_CheckedChanged(object sender, EventArgs e)
        {
            if (bobButton.Checked)
            {
                label6.Text = "Bob";
            }
        }

        private void alButton_CheckedChanged(object sender, EventArgs e)
        {
            if (alButton.Checked)
            {
                label6.Text = "Al";
            }
        }
    }

}


Bet.cs (unfinished)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dugs
{
    public class Bet
    {
        public int Amount;
        public int Dog;
        public Guy Bettor;

        public string GetDescription()
        {
            return Bettor.ToString();
        }

        public int PayOut(int Winner)
        {

            return 20;
        }
    }
}

0

#2 User is offline   AndrewStellman 

  • Andrew Stellman
  • PipPipPipPipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 738
  • Joined: 08-October 08
  • Gender:Male
  • Location:Brooklyn, NY
  • Interests:Author of: "Head First C#", "Beautiful Teams", "Head First PMP", "Applied Software Project Management"

Posted 20 January 2014 - 07:48 PM

Just glancing through your code, I don't see any place where you set the Amount field in a Bet object. Try setting it right above where you're printing out that debug message.
Andrew Stellman
Author, Head First C#
Building Better Software -- http://www.stellman-greene.com
0

#3 User is offline   wunderwaffe 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 26
  • Joined: 14-January 14

Posted 22 January 2014 - 07:31 AM

View PostAndrewStellman, on 20 January 2014 - 07:48 PM, said:

Just glancing through your code, I don't see any place where you set the Amount field in a Bet object. Try setting it right above where you're printing out that debug message.


Bingo.

Really cool that the author engages with book owners. :)
0

#4 User is offline   wunderwaffe 

  • Active Member
  • PipPip
  • Group: Members
  • Posts: 26
  • Joined: 14-January 14

Posted 23 January 2014 - 04:51 AM

View Postwunderwaffe, on 22 January 2014 - 07:31 AM, said:

Bingo.

Really cool that the author engages with book owners. :)


Ok, I'm having trouble linking it all together now. My problem is I can't get a race result to trigger +/- the guys' cash bases on the race result.

Bet.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace dugs
{
    public class Bet
    {
        public int Amount;
        public int Dog;
        public Guy Bettor;

        public string GetDescription()
        {
            return Bettor.ToString();
            
        }

        public int PayOut(int Winner)
        {

            if (this.Dog == Winner)
            {
                return Amount;
            }
            else
            {
                return -Amount;
            }
        }
    }
}


Guy.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace dugs
{
    public class Guy
    {
        public String Name;
        public Bet MyBet;
        public int Cash;

        public RadioButton MyRadioButton;
        public Label Mylabel;

        public void UpdateLabels()
        {
            Mylabel.Text = Name + " bets " + MyBet.Amount + " on " + MyBet.Dog;
        }

        public void ClearBet()
        {
            //meh stub
        }

        public bool PlaceBet(int Amount, int Dog)
        {
            MyBet = new Bet() { Bettor = this, Amount = Amount, Dog = Dog };
            System.Diagnostics.Debug.WriteLine("Amount is " + Amount + " Dog is " + Dog + " Bettor is " + Name);
            //System.Diagnostics.Debug.WriteLine(MyBet);
            return true;
        }

        public void Collect(int Winner)
        {
            this.Cash = this.Cash + MyBet.PayOut(Winner);
        }
    }

    }



Form1.cs

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 dugs
{
    public partial class form1 : Form
    {
        Greyhound g1;
        Greyhound g2;
        Greyhound g3;
        Greyhound g4;
        Greyhound[] greyRay;

        Guy m1;
        Guy m2;
        Guy m3;
        Guy[] biGuy;

        int minBet = 5;

        public form1()
        {
            InitializeComponent();
            minBet = 5;
            g1 = new Greyhound() { name = "Brutus1", StartingPosition = pictureBox2.Location, RaceTrackLength = (pictureBox1.Width - pictureBox2.Width), MyPictureBox = pictureBox2 };
            g2 = new Greyhound() { name = "Rover2", StartingPosition = pictureBox3.Location, RaceTrackLength = (pictureBox1.Width - pictureBox2.Width), MyPictureBox = pictureBox3 };
            g3 = new Greyhound() { name = "Pedro3", StartingPosition = pictureBox4.Location, RaceTrackLength = (pictureBox1.Width - pictureBox2.Width), MyPictureBox = pictureBox4 };
            g4 = new Greyhound() { name = "Cadger4", StartingPosition = pictureBox5.Location, RaceTrackLength = (pictureBox1.Width - pictureBox2.Width), MyPictureBox = pictureBox5 };

            greyRay = new Greyhound[4];
            greyRay[0] = g1;
            greyRay[1] = g2;
            greyRay[2] = g3;
            greyRay[3] = g4;

            m1 = new Guy() { Cash = 100, Mylabel = label3, MyRadioButton = joeButton, Name = "Joe" };
            m2 = new Guy() { Cash = 100, Mylabel = label5, MyRadioButton = alButton, Name = "Al" };
            m3 = new Guy() { Cash = 100, Mylabel = label4, MyRadioButton = bobButton, Name = "Bob" };

            biGuy = new Guy[3];
            biGuy[0] = m1;
            biGuy[1] = m2;
            biGuy[2] = m3;

          
        }


        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (Greyhound g in greyRay)
            {
                g.TakeStartingPosition();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            button1.Enabled = false;

            for (int i = 0; i < greyRay.Length; i++)
            {
                if (greyRay[i].Run() == true)
                {
                    timer1.Stop();
                    MessageBox.Show("Dog " + greyRay[i].name + " wins");
                    foreach (Greyhound g in greyRay)
                    {
                        g.TakeStartingPosition();
                    }
                    button1.Enabled = true;
                    foreach (Guy guy in biGuy)
                    {
                        guy.MyBet.PayOut(guy.MyBet.Dog);
                        System.Diagnostics.Debug.WriteLine(guy.Name + " now has " + guy.Cash);
                    }
                }
                // else{g

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
              for(int i = 0; i < biGuy.Length; i++){
                  int stake = Convert.ToInt32(numericUpDown1.Value);
                  int dogNo = Convert.ToInt32(numericUpDown2.Value);
                if(label6.Text == "name"){
                    MessageBox.Show("Please select a guy");
                }
                else if (biGuy[i].Cash < stake){
                    String tmpName = biGuy[i].Name;
                    int tmpCash = biGuy[i].Cash;
                    MessageBox.Show(tmpName + " only has " + tmpCash);
                }
                else if (biGuy[i].MyRadioButton.Checked == true){
                    biGuy[i].PlaceBet(stake, dogNo);
                    biGuy[i].UpdateLabels();
                }
                }
              
        
    }

        private void joeButton_CheckedChanged(object sender, EventArgs e)
        {
            if (joeButton.Checked)
            {
                label6.Text = "Joe";
                numericUpDown1.Maximum = m1.Cash;
            }
        }

        private void bobButton_CheckedChanged(object sender, EventArgs e)
        {
            if (bobButton.Checked)
            {
                label6.Text = "Bob";
                numericUpDown1.Maximum = m2.Cash;
            }
        }

        private void alButton_CheckedChanged(object sender, EventArgs e)
        {
            if (alButton.Checked)
            {
                label6.Text = "Al";
                numericUpDown1.Maximum = m3.Cash;
            }
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

    }

}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users