Jump to content

C# Program issue

Sergio45
Go to solution Solved by vorticalbox,

right now your code will give you winnings for 2 and 3 matches if you match all 3. I assume this isn't what you're after

 

					
if (fruits[0]==fruits[1]||fruits[0]==fruits[2]||fruits[1]==fruits[2]) 
    {
		if (fruits[0]==fruits[1]&&fruits[0 ==fruits[2]&&fruits[1]==fruits[2])
        {
            amountWon = userInput * threeWinners;
            resultLabel.Text = amountWon.ToString("C");
            totalWon = totalWon + amountWon;
        } else
		{
			//moving the 2 win code to here means you will only win 2 matches or 3 matches not both.
			amountWon = userInput * twoWinners;
			resultLabel.Text = amountWon.ToString("C");
			totalWon = totalWon + amountWon;
		}
    }else
    {
        resultLabel.Text = "You didn't win anything.";
    }

 

 

 

 

Build is 4.03 VS13

 

The program is a slot machine program. I am running into the issue where I get the wrong amount displayed. When the user clicks the spin button three images show up. If two of the images match you double the amount the user entered. If all three images match you triple the amount entered. When you click the exit button the user gets a message showing the amount they entered per spin. So if  I entered 10.00 and hit the spin button 3 times the answer should be 30.00, that part works. The issue is when the amount won shows up. For example if the user enters 10.00 plays the game three times and wins a double (20.00) and a triple (30.00) and noting on the last play. The results should be: Amount entered was $30.00 and Amount won was: $50.00. Well i get a whole different answer. I just want to know if my method is wrong or my math is wrong. I will post the  method where I think the issue is

 

**also 

      double amountWon=0;
      double twoWinners=2;
      double threeWinners=3;
      double totalWon=0;
      double enteredAmount;

 

are global. 

 

Thanks again!

   
      private void GiveItASpinMethod(double userInput) 
      {
         Random rand = new Random();
         int[] fruits = new int[3];

         for (int index = 0; index < fruits.Length; index++) 
         {
            fruits[index] = rand.Next(imageList.Images.Count);

            slotPictureOne.Image = imageList.Images[fruits[0]];
            slotPictureTwo.Image = imageList.Images[fruits[1]];
            slotPictureThree.Image = imageList.Images[fruits[2]];



            if (fruits[0] == fruits[1] || fruits[0] == fruits[2] || fruits[1] == fruits[2] || fruits[1] == fruits[0] || fruits[2] == fruits[0] || fruits[2] == fruits[1])
            {
                amountWon = userInput * twoWinners;
                resultLabel.Text = amountWon.ToString("C");
                totalWon = totalWon + amountWon;


                if ((fruits[0] == fruits[1] && fruits[0] == fruits[2]) || (fruits[1] == fruits[0] && fruits[1] == fruits[2]) || (fruits[2] == fruits[0] && fruits[2] == fruits[1]))
                {
                    amountWon = userInput * threeWinners;
                    resultLabel.Text = amountWon.ToString("C");
                    totalWon = totalWon + amountWon;
                }
            }
            else
            {
                resultLabel.Text = "You didn't win anything.";
            }
         }
          
      }


      private void exitButton_Click(object sender, EventArgs e) 
      {
         MessageBox.Show("The amount won:"+" "+ totalWon.ToString("C")+"\n"+"The entered amount was:"+" "+enteredAmount.ToString("C"));
        
         //this closes the form and the program window.
         this.Close();
      }

 

Link to comment
Share on other sites

Link to post
Share on other sites

You don't need to check both a == b and b == a so your if statements can be simplified a little.

if (a == b || a == c || b == c)
{
    // at least two match
    // ...
  
    if (a == b && a == c && b == c)
    {
        // all three match
    }
}
else
{
    // No matches
}

Just in case it's not clear, replace a, b and c with fruits[0], fruits[1], and fruits[2] respectively.

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, madknight3 said:

 


if (a == b && a == c && b == c)

 

Same as 

if (a == b && a == c)

Transitive property ;)

i7 4790k | MSI Z97S SLI Krait Edition | G.Skill Ripjaws X 16 GB | Samsung 850 EVO 500 GB | 2x Seagate Barracuda 2TB | MSI GTX 970 Twin Frozr V | Fractal Design R4 | EVGA 650W

A gaming PC for your budget: $800 - $1000 - $1500 - $1800 - $2600 - $9001

Remember to quote people if you want them to see your reply!

Link to comment
Share on other sites

Link to post
Share on other sites

If (fruits[0]==fruits[1]||fruits[0]==fruits[2]||fruits[1]==fruits[2])
{
  if (fruits[0]==fruits[1]&&fruits[0]==fruits[2]&&fruits[1]==fruits[2])
  {
    //would this take care of all possible triple matches? 
  }
}
else 
{ 
  //No matches
}

I see what you mean and I will do that. 

 

Thanks again 

Edited by Sergio45
Link to comment
Share on other sites

Link to post
Share on other sites

I did get rid of the redundant code.  Here's an updated look. 

 private void GiveItASpinMethod(double userInput) 
 {
   Random rand = new Random();
   int[] fruits = new int[3];

   for (int index = 0; index < fruits.Length; index++) 
   {
     fruits[index] = rand.Next(imageList.Images.Count);
     
     slotPictureOne.Image = imageList.Images[fruits[0]];       
     slotPictureTwo.Image = imageList.Images[fruits[1]];    
     slotPictureThree.Image = imageList.Images[fruits[2]];
     
     if (fruits[0]==fruits[1]||fruits[0]==fruits[2]||fruits[1]==fruits[2]) 
     {
       amountWon = userInput * twoWinners;
       resultLabel.Text = amountWon.ToString("C");
       totalWon = totalWon + amountWon;
       
       if (fruits[0]==fruits[1]&&fruits[0 ==fruits[2]&&fruits[1]==fruits[2])
       {
         amountWon = userInput * threeWinners;
         resultLabel.Text = amountWon.ToString("C");
         totalWon = totalWon + amountWon;
       }
      }
    else
    {
     resultLabel.Text = "You didn't win anything.";
     }
 }
}


      private void exitButton_Click(object sender, EventArgs e) 
      {
        MessageBox.Show("The amount won:"+" "+ totalWon.ToString("C")+"\n"+"The entered amount was:"+" "+enteredAmount.ToString("C"));
        
         //this closes the form and the program window.
         this.Close();
      }

 

Edited by Sergio45
Link to comment
Share on other sites

Link to post
Share on other sites

right now your code will give you winnings for 2 and 3 matches if you match all 3. I assume this isn't what you're after

 

					
if (fruits[0]==fruits[1]||fruits[0]==fruits[2]||fruits[1]==fruits[2]) 
    {
		if (fruits[0]==fruits[1]&&fruits[0 ==fruits[2]&&fruits[1]==fruits[2])
        {
            amountWon = userInput * threeWinners;
            resultLabel.Text = amountWon.ToString("C");
            totalWon = totalWon + amountWon;
        } else
		{
			//moving the 2 win code to here means you will only win 2 matches or 3 matches not both.
			amountWon = userInput * twoWinners;
			resultLabel.Text = amountWon.ToString("C");
			totalWon = totalWon + amountWon;
		}
    }else
    {
        resultLabel.Text = "You didn't win anything.";
    }

 

 

 

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

So the original code was stating that if three matches were true than it would count it as a double and a triple? 

The edited version from VorticalBox states that you can only get a triple or double noting else, correct? 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, HPWebcamAble said:

Same as 


if (a == b && a == c)

Transitive property ;)

Damn it, that's what I meant to write. I guess I need to pay better attention. Oh well, good catch.

Link to comment
Share on other sites

Link to post
Share on other sites

I have found and fixed the issue. Thanks again for the feed back and steering me towards the correct answer. Thumbs up to all of you that help me!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×