tags:

views:

63

answers:

4

I am doing this lab out of a book on my own, and I made an application in which sharks are racing. There is a radio button that should update a label on the right dynamically, as well as a button that actually starts the race. Everything used to work and then I renamed a few things, and now nothing works.

Screenshot of application:

image

Form 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;

        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() {myName="Joe", cash=50, myRadioButton=rbGuy1, myLabel=labelBet1},
                    new Guy() {myName="Bob", cash=75, myRadioButton=rbGuy2, myLabel=labelBet2},
                    new Guy() {myName="Al", cash=45, myRadioButton=rbGuy3, myLabel=labelBet3}
                };

            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.myName;
        }

        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;
            selectedGuy.placeBet(bet, myFish);
            updateGui();
        }

        private void raceBtn_Click(object sender, EventArgs e)
        {
            betBtn.Enabled = false;

            bool noWinner = true;
            while(noWinner)
            {
                for (int dogFish = 0; dogFish < sharks.Length; dogFish++)
                {
                    Application.DoEvents();
                    if(sharks[dogFish].Swim())
                    {
                        showWinner(dogFish);
                        collectBets(dogFish);
                        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(fish + 1);
                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();
            }

            updateMin();
        }
    }
}

Shark 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;
        }
    }
}

I can supply more resources if needed, but this is the main gist of it.

+1  A: 

Make sure that the events on your controls are still connected to the correct event handlers in your code. Sometimes when you rename things, this link can get broken.

Brian
they are. there are no compiler errors and I believe everything is linked properly. However, I think there is something wrong with the for loops.
classicrock985
A: 

what did you rename?

D0cNet
Looks like you renamed "private void Bet_Click(object sender, EventArgs e)".Rename it back to"private void betBtn_Click(object sender, EventArgs e)".and ensure that the event is linked to the button.
D0cNet
well the main problems are that the raceBtn doesn't launch the Swim function in the Shark class, and the labelBet2 and labelBet3 are not updating, however labelBet1 is.
classicrock985
Ok check the "raceBtn_Click()" event. Set some breakpoints in the raceBtn _click() event and in the swim method of the shark class and debug.
D0cNet
+2  A: 

when you renamed them you probably did it by editing the code rather than by changing the control properties.

THe Winforms designer in VS created code for you behind the scenes that wires the invents up. This codes uses the control names. Look for a file called formname_designer.cs. Notice that there are lines that still have the old control names. You can change this code

This is why its a good habit to give controls nice names when you start.

pm100
it changed the names of the controls in my Shark and Form class, but maybe it didn't change them in the designer class. I'll check there right now, thanks!
classicrock985
nope, that wasn't it. Thanks for the suggestion though! everything seems to match up. it looks like it might be a problem with the for loops. "raceBtn" isn't launching the Swim function like it's supposed to.
classicrock985
+2  A: 

Using Visual Studio, make sure of the following:

1) For each radio button, verify the CheckedChanged event is hooked up to your myChanged function.
2) Verify the "Bets" Button.Click event is hooked up to your Bet_Click function.
3) Verify the "Race!" Button.Click event is hooked up to your raceBtn_Click function.

A safe way to rename things is to right click on the variable name, Refactor, Rename. This will ensure any references to the variable are renamed properly

SwDevMan81
+1 Refactoring > Rename is the way to go.
DevDemon
how do I verify? this seems like it might actually be the problem. for example, whenever I double click on the button in the "designer" mode, it should normally take me to the class that is assigned to when it's clicked right? well when I double click any of those buttons, it tries to create a new raceBtn_Click_1 function instead of taking me to raceBtn_Click. Is there a way to link them up?
classicrock985
You can close VS and open it back, then click on the events section for the raceBtn, enter the correct event raceBtn_Click. That should hook it up.
D0cNet
@classicrock985 - In the designer, click on the "Race!" button. Then right click and select 'Properties' at the bottom. In the Properties box, you will see a lightning bolt under the Button name. Click the lightning bolt. Scroll down and find the 'Click' event. Then you can click the box to the right and select the 'raceBtn_Click' function.
SwDevMan81
how do i click on the events section? I'm sorry, I've really not been using this program for very long.
classicrock985
You can repeat that process for the radio buttons and 'Bets' button as well. As I mentioned, the radio buttons event you want to look for is 'CheckedChanged' not the 'Click' event like the other two buttons
SwDevMan81
where is CheckedChanged exactly? I'm looking for it. This actually worked for my Run button, and I think you so very very much for helping me with that. Now I jut need to fix the radio buttons!
classicrock985
I see CheckedChanged actually, but what should it be linked to? right now none of them are linked and somehow the label1 is still being updated with radio button 1. the other radio buttons dont work though.
classicrock985
Just click on each Radio button under 'Minimum Bets'. Select each individually (Joe, Bob, Al), then right click and do properties (may not need to open the properties window again if its docked and already opened). Click on the lightning bolt again, and scroll down until you find `CheckChanged`. Click the box to the right and select `myChanged`. Repeat that for all RadioButtons (Joe, Bob and Al)
SwDevMan81
Wow, you really are a lifesaver. Thanks!!!
classicrock985
Yeah no problem. Like I mentioned in my answer, next time right click on the variable name and select Refactor, then select Rename. That will save you a ton of time and headache. Good luck!
SwDevMan81