views:

84

answers:

2

Ok so I am adding on to my last project, where I created a shark race. Everything ran smoothly, and then it got fancy.

Here are the requirements: 1) Declare all of the instance variables to be public properties defined with get and set accessor methods along with corresponding private backing fields. 2) Remove the MyBet instance variable from the Guy class and instead provide an array within the Form1 class that holds each Bet instance. The betting parlor should now allow for each Guy to place multiple bets. However, the Amount of each Bet should be deducted from the Cash property of each Guy at the time the Bet is made. Winnings are thus only positive quantities that are paid out after the race. 3) Have a scoreboard (or some type of visual display) that indicates the order in which each racer finished, allowing for the possibility that there are ties for 1st, 2nd, or 3rd places.

I'm getting about 50 of the same error, and it seems like they are all very quick fixes, but for some reason I can't figure them out. Thanks in advance!

With that in mind, here are my forms:

Forms Class:

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 project1 { public partial class Game : Form { private Shark[] sharks; private Guy[] guys; private Guy selectedGuy; private Bet[] bets; private int[] winners = new int[4];

    public Game()
    {
        InitializeComponent();

        Random moreRandom = new Random();

        int start = myTrack.Location.X;
        int finish = myTrack.Width - 65;

        sharks = new Shark[4]
            {
                new Shark() {myRandom = moreRandom, myPictureBox = myShark1, myPBStart = start, trackLength = finish},
                new Shark() {myRandom = moreRandom, myPictureBox = myShark2, myPBStart = start, trackLength = finish},
                new Shark() {myRandom = moreRandom, myPictureBox = myShark3, myPBStart = start, trackLength = finish},
                new Shark() {myRandom = moreRandom, myPictureBox = myShark4, myPBStart = start, trackLength = finish}
            };

        guys = new Guy[3]
            {
                new Guy() {Name="Joe", Moneys=50, MyRB=rbGuy1, GuyLabel=labelBet1},
                new Guy() {Name="Bob", Moneys=75, MyRB=rbGuy2, GuyLabel=labelBet2},
                new Guy() {Name="Al", Moneys=45, MyRB=rbGuy3, GuyLabel=labelBet3}
            };

        bets = new Bet[12];

        for (int = 0; i<bets.Length; i++)
        {
            bets[i] = Bet.EmptyBet;
        }

        selectedGuy = guys[0];
        rbGuy1.Tag = guys[0];
        rbGuy2.Tag = guys[1];
        rbGuy3.Tag = guys[2];            

        updateGui();
    }

    private void myChanged(object sender, EventArgs e)
    {
        selectedGuy = getSelectedGuy(sender);
        betterLabel.Text = selectedGuy.Name;
    }

    private void betAmountValue(object sender, EventArgs e)
    {
        updateMin();
    }

    private void bet_Click(object sender, EventArgs e)
    {
        int bet = (int) betAmount.Value;
        int myFish = (int) sharkNumber.Value;
        Bet temp = selectedGuy.placeBet(bet,myFish);
        if (!temp == Bet.EmptyBet)
        {
        for (int i = 0; i<3; i++)
        {
            if (guys[i].Name == selectedGuy.Name)
            {
                bets[i + (3 * myFish)] = temp;
            }
        }

            /*for (int = 0; i<bets.Length, i++)
            {
            bets[i] = temp;
            }*/

        }
        //selectedGuy.placeBet(bet, myFish);
        updateGui();
    }

    private void raceBtn_Click(object sender, EventArgs e)
    {
        betBtn.Enabled = false;
        Application.DoEvents();
        //bool noWinner = true;

        Random rand = new Random();
        /*int winner = rand.Next(1,5);

        showWinner(winner-1);
        collectBets(winner-1);
        */
        for(int i=0; i<4; i++)
            winners[i] = 0;

        for(int i=0; i<4; i++)
        {
            bool placed = false;
            int temp;
            while (!placed)
            {
                temp = rand.Next(0,4);
                if (winners[temp] == 0)
                {
                    winners[temp] = i;
                    placed = true;
                }
            }
        }

        showWinner(winners[0] - 1);
        collectBets(winners[0] - 1);

        /*while(noWinner)
        {
            for (int fish = 0; fish < sharks.Length; fish++)
            {
                Application.DoEvents();
                if(sharks[fish].Swim())
                {
                    showWinner(fish);
                    collectBets(fish);
                    noWinner = false;
                }
            }
        }*/

        updateGui();

        betBtn.Enabled = true;
    }



    private void showWinner(int fish)
    {
        MessageBox.Show(string.Format("Winner Winner People Dinner! \nShark {0} won!", fish + 1));

    }

    private void collectBets(int fish)
    {
        for (int guyNumber = 0; guyNumber < guys.Length; guyNumber++)
        {
            guys[guyNumber].collect(betTotal(), winningSharkTotal());
            guys[guyNumber].resetBet();

        }
    }

    private void updateMin()
    {
        minBetLabel.Text = string.Format("Minimum bet: 5 bucks", betAmount.Value);
    }

    private Guy getSelectedGuy(object sender)
    {
        RadioButton rb = (RadioButton)sender;
        return (Guy)rb.Tag;
    }

    private void updateGui()
    {
        for (int guyNumber = 0; guyNumber < guys.Length; guyNumber++)
        {
            guys[guyNumber].updateLabels();
        }

        for (int fish = 0; fish < sharks.Length; fish++)
        {
            sharks[fish].startPosition();
        }

        /*betTable1 = bets[0];
        betTable2 = bets[1];
        betTable3 = bets[2];*/


        bet1_1.Text = string.Format(bets[0].Amount);
        bet1_2.Text = bets[1].Amount;
        bet1_3.Text = bets[2].Amount;
        bet2_1.Text = bets[3].Amount;
        bet2_2.Text = bets[4].Amount;
        bet2_3.Text = bets[5].Amount;
        bet3_1.Text = bets[6].Amount;
        bet3_2.Text = bets[7].Amount;
        bet3_3.Text = bets[8].Amount;
        bet4_1.Text = bets[9].Amount;
        bet4_2.Text = bets[10].Amount;
        bet4_3.Text = bets[11].Amount;

        updateMin();

        firstWinner.Text = winners[0];
        secondWinner.Text = winners[1];
        thirdWinner.Text = winners[2];
        fourthWinner.Text = winners[3];

    }

    private int betTotal()
    {
    int Result = 0;
    for (int i=0; i<bets.Length; i++)
    {
    Result += bets[i].Amount;
    }
    return Result;
    }

    private int winningSharkTotal(int Winner)
    {
    int Result = 0;


    for (int i = 0; i<3; i++)
    {
        Result += bets[i+(winner*3)];

    }
    return Result;


    }

}

}

Sharks Class:

using System;

using System.Drawing; using System.Threading; using System.Windows.Forms;

namespace project1 { public class Shark { public int myPBStart; // Where the PictureBox starts public int trackLength; // How long the racetrack is public PictureBox myPictureBox = null; // The PictureBox object public int location = 0; // My location on the racetrack public Random myRandom; // An instance of Random

    public Shark()
    {
        location = 0;
        myPictureBox = new PictureBox();
        myRandom = new Random();
        trackLength = 100;
        myPBStart = 0;
    }

    public bool Swim()
    {
        int distance = myRandom.Next(1, 4);
        location += distance;

        movePB(distance);

        return location > trackLength;
    }

    private void movePB(int distance)
    {
        Point p = myPictureBox.Location;
        p.X += distance;
        myPictureBox.Location = p;
    }

    public void startPosition()
    {
        location = myPBStart;

        Point p = myPictureBox.Location;
        p.X = location;
        myPictureBox.Location = p;
    }
}

}

Guy Class:

using System.Windows.Forms;

namespace project1 { public class Guy { private string myName; //public Bet myBet; private int cash;

    // The last two fields are the the guy's GUI controls on the form
    private RadioButton myRadioButton;
    private Label myLabel;
    const int minBet = 5;
    const int maxBet = 15;

    public Guy()
    {
        //myBet = emptyBet();

        //myBet = new Bet() { amount = 0, fish = 0, better = this };

    }

    public void updateLabels()
    {
        get{
        // Set my label to my bet's description and its label
        myLabel.Text = myBet.Description();

        // cash radio buttons
        myRadioButton.Text = radioButtonText();
        }
    }

    public void resetBet()
    {
        // reset my bet to 0
        myBet = Bet.EmptyBet;
    }

    public string Name{
    get{
    return myName;
    }

    }

    public int Moneys
    {
        get
        {
            return cash;
        }
    }

    public RadioButton MyRB
    {
        get
        {
            return myRadioButton;
        }
    }

    public Label GuyLabel
    {
        get
        {
            return myLabel;
        }
    }

    public Bet placeBet(int amount, int fish)
    {
        if (amount > cash || amount < minBet || amount > maxBet)
        {
            return Bet.EmptyBet;
            //return false;
        }

        cash -= amount;
        return new Bet() {Amount=amount, Fish=fish, better=this};

        //return true;
    }

    public void collect(int pool, int sharkPool)
    {
        get{
        return cash += myBet.payout(pool, sharkPool);
        }
    }


    private string radioButtonText()
    {
        return string.Format("{0} has {1} bucks", myName, cash);
    }
}

}

Bet Class:

namespace project1

{ public class Bet { private int amount; // The amount of cash that was bet private int fish; // The number of the shark the bet is on. public Guy better; // The guy who placed the bet

    public static Bet EmptyBet{
    get
        {
            return new Bet() { amount = 0, fish = 0, better = this };
        }

    }

    public string Description
    {
    get
        {
            if (noBet())
            {
            return string.Format("{0} hasn't placed a bet", better.Name);    
            }
            else
            {
            return string.Format("{0} bets {1} bucks on shark #{2}", better.Name, amount, fish);    
            }
        }
    }

    public int payout(int pool, int sharkPool)
    {
        get{

        return ((pool * (0.85))/sharkPool) * amount;

        /*if (myShark(pool))
        {
            return amount;
        }
        else
        {
            return -amount;
        }*/
        }
    }

    /*private bool myShark(int winner)
    {
        return winner == fish;
    }*/

    private bool noBet()
    {
        return amount == 0;
    }

    public int Amount 
    {
       get
        {
        return amount;
        }
    }

    public int Fish
    {
        get
        {
            return fish;
        }
    }

}

}

+4  A: 

You are confusing the concept of a method and a property. First remove the get { } from you methods and then consider whether void is the right return type. From the looks of it you are returning some sort of decimal value.

public void collect(int pool, int sharkPool)
{
    get{
    return cash += myBet.payout(pool, sharkPool);
    }
}

In the following code, by including the literal 0.85 the resulting value will have type double. The return type of the method is int.

public int payout(int pool, int sharkPool)
{
    get{

    return ((pool * (0.85))/sharkPool) * amount;

    /*if (myShark(pool))
    {
        return amount;
    }
    else
    {
        return -amount;
    }*/
    }
}

As I mentioned previously, remove the get and mark the return type as double.

public double payout(int pool, int sharkPool)
{
    return ((pool * (0.85))/sharkPool) * amount;
}

There is more but I think you have bigger fish to fry. I would go back and review the fundamentals of the C# syntax and try to get a better understanding of numeric data types. When working with currency you really need to use decimal.

ChaosPandion
+1 for taking the extra effort!
Harpreet
I've seen this kind of method 2 in Guys class and 1 in Bet class.
rob waminal
+1  A: 

Chaos is correct (and props for seeing what I overlooked). It also looks like there are some type conversion errors being generated from assigning Bet.Amount to a TextBox. Call ToString() on your integer values, and these errors should go away.

Error 42 Cannot implicitly convert type 'int' to 'string'

And please remember that UI elements (like labels, radio buttons, picture boxes) do not belong on business objects like Guys or Sharks. This is design decision that is far more fundamental than fixing a few compiler errors (though I'm sure it doesn't seem that way at the moment).

Tim
thank you so much! However, winners[0]-[4] aren't strings are they? So would I do something like.. firstWinner.Text = ConvertToString(winners[0]); ?
classicrock985
firstWinner.Text = winners[0].ToString();
Tim
That's probably the simplest way of doing it, obviously it doesn't check for null values, perform formatting, or perform localization.
Tim