Hi Everyone,

It has been some time since I've posted here but I am truly having trouble seeing the forest for the trees so to speak. I have changed my code so many times I missed the time it worked because I want it to do more.

Upon Exit btn click event total winnings and total spent should display in a msgbx. Spin click event shows msgbx with winner and winnings.

I would really appreciate a fresh set of eyes and a shove in the right direction. For some reason it shows only the amtEntered in the text box and 0.00 winnings - as if it is always branching to the else in the btnExit_Click. I'm sure I have some superfluous stuff hanging out in here...pls be kind.

Thanks.

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 SlotMachine
{
    public partial class SlotMachine : Form
    {

        public SlotMachine()
        {
            InitializeComponent();
        }
        decimal amtEntered;
        Random rand = new Random();
        decimal total;
        decimal win1;
        decimal win2;
        decimal totalWin;


        private void GamePlay()
        {
            try
            {
                txtbxInputBet.Focus();


                int index1 = rand.Next(imageListFruit.Images.Count);

                int index2 = new int();
                index2 = rand.Next(imageListFruit.Images.Count);

                int index3 = new int();
                index3 = rand.Next(imageListFruit.Images.Count);


                pictbxOne.Image = imageListFruit.Images[index1];
                pictbxTwo.Image = imageListFruit.Images[index2];
                pictbxThree.Image = imageListFruit.Images[index3];


                amtEntered = decimal.Parse(txtbxInputBet.Text.Trim());
                total += amtEntered;

                
                win1 = amtEntered * 2;
                win2 = amtEntered * 3;
                totalWin = 0;

                if (index1 == index2 || index1 == index3 || index2 == index3)
                {
                    MessageBox.Show("Excellent! You win " + win1.ToString("c") + " !");
                    
                }

                else if (index1 == index2 && index1 == index3 && index2 == index3)
                {
                    MessageBox.Show("Excellent! You win " + win2.ToString("c") + " !");
                    
                }
                else
                {
                    MessageBox.Show("House Wins!");
                }
                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
           
        }



        private void btnSpin_Click(object sender, EventArgs e)
        {
            GamePlay();

        }

        private void btnExit_Click(object sender, EventArgs e)
        {

            try
            {


                int index1 = rand.Next(imageListFruit.Images.Count);

                int index2 = new int();
                index2 = rand.Next(imageListFruit.Images.Count);

                int index3 = new int();
                index3 = rand.Next(imageListFruit.Images.Count);


                amtEntered = decimal.Parse(txtbxInputBet.Text.Trim());
               
                win1 = amtEntered * 2;
                win2 = amtEntered * 3;
                total = 0;
                

                    if (index1 == index2 || index1 == index3 || index2 == index3)
                    {
                        total += amtEntered;
                        totalWin += win1;
                        MessageBox.Show(" You spent " + total.ToString("c"));
                        MessageBox.Show("You won: " + win1.ToString("c"));
                        

                    }


                        else if (index1 == index2 && index1 == index3 && index2 == index3)
                        {
                            total += amtEntered;
                            totalWin += win2;
                            MessageBox.Show(" You spent " + total.ToString("c"));
                            MessageBox.Show("You won: " + win2.ToString("c"));
                        }

                        else
                        {
                            total += amtEntered;
                            
                            MessageBox.Show("Total Winnings: $0.00. You spent " + total.ToString("c"));
                        }

                    

                    txtbxInputBet.Clear();    

                    }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            


            this.Close();
        }

        
        }
    }

The first thing I notice looking at your code is that the "and-ed" if-statement should be before your "or-ed" if-statement, because and's will always be false if or's are. I'm not 100% sure, but I think the statements int index3 = new int(); should be replaced by simply int index3 = rand.Next(imageListFruit.Images.Count); like you have for index1. Also, are you sure you need to basically duplicate the code from the GamePlay method in the btnExit_Click method?

Hi,

Thanks for the reply. In answer to your question of duplication...Actually no, I am not sure but I am willing to take it out and see. It's been a week since inception but I'm sure I was trying to get access to the btnSpin_Click event - this was before I had coded the GamePlay method.

Fairly sure int index was giving me the same random image in each pictureBox from the ImageList which is why I decided to assign new but will work that again as well.

Thank you for the tip about and or - I'm sure I should have remembered that but when you are just starting out it's difficult to think of every lesson learned. I do hope this gets easier with practice. So far I've been doing fairly well but i seemed to do better in C++...*sigh*.

I will try your suggestions and post if it works out!

So far I've been doing fairly well but i seemed to do better in C++...*sigh*.

I feel completely the opposite, C++ confounds me!

I'm pretty sure the random number generator in C# uses the current system time in milliseconds to generate its next int. This means numbers generated in the same millisecond by the same instance of Random will be the same. One solution might be to Thread.Sleep(1); between each generation to ensure you get a tick between them.

Good luck and have fun!

Hello ~

Thank you again for the replies. I will be using the code without the Thread.Sleep(1). It is working well without it for now but I will definitely note that one for further programming challenges.

Removing the redundancies in the btnExit_Click Event actually did the trick. You know, I did not even try it without adding that code first. I guess I was just confounded as to how to access the btnSpin_Click Event in the btnExit_Click Evt(I was trying to find a way to update amtEntered with each btnSpin_Click adding it to total. Looking through next semesters Java book and knowing Javascript mouse events made me want to go there!). Unfortunately you have to walk before you can run I guess. I'm sure there must be a way to do what I was trying to do, I just haven't arrived there at this point in my studies.

One more thing ~ Thanks for not berating me about posting for help with an assignment. I really don't post to have someone give me the actual code, just a friendly shove in the right direction and a bit of encouragement that I am on the right track.

I do have one program that I wrote for a past assignment with two-dimensional arrays. I have already turned it in and am willing to take whatever I get on it but I would like to have an explanation of what I've done wrong or what I am missing. I hope I'm not imposing when I ask if this is something you would be willing to take a look at as well?

Thanks Again for your help,
Sharon

I do have one program that I wrote for a past assignment with two-dimensional arrays. I have already turned it in and am willing to take whatever I get on it but I would like to have an explanation of what I've done wrong or what I am missing. I hope I'm not imposing when I ask if this is something you would be willing to take a look at as well?

Absolutely, please feel free to ask questions about whatever you need. That's what DaniWeb is all about! I have always said that the best way to learn is to make mistakes and find out what went wrong.

However it might be a good idea to start a new thread for your new topic.

Sorry for the late post but thank you I will do that.

S

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.