I have written the following slot machine program in C# .NET framework. The game worked fine until I attempted to add a feature that displays the total amount of money entered into the slot machine and the total amount won from the game after the exit button is clicked. I know my code currently has the following errors but I'm not sure how to fix them. Any and all help with this is greatly appreciated.
THE CODE:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JewellK_SlotMach
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void spinButton_Click(object sender, EventArgs e)
{
//Decalre variable to store the amount and the winning amount.
double amount;
double winningAmount;
double totalAmount;
double totalWinningAmount;
//Take the users entered amount from the text box and check to see if it is a double data type
bool isValidAmount = Double.TryParse(amountTextBox.Text, out amount);
//If the amount entered is a double data type the the program continues
if (isValidAmount)
{
//Get the total number of images in the Image List Control
int numberOfImages = picturesImageList.Images.Count;
//Create a Random object to generate random numbers
Random rand = new Random();
//Store the 3 images in 3 random locations
int image1Index = rand.Next(0, numberOfImages);
int image2Index = rand.Next(0, numberOfImages);
int image3Index = rand.Next(0, numberOfImages);
//Display the 3 images in the picture boxes
image1PictureBox.Image = picturesImageList.Images[image1Index];
image2PictureBox.Image = picturesImageList.Images[image2Index];
image3PictureBox.Image = picturesImageList.Images[image3Index];
//Check to see if all 3 images match
if ((image1Index == image2Index) && (image2Index == image3Index))
{
//Multiply the users entered amount by 3
winningAmount = amount * 3;
//Display the winning amount
MessageBox.Show("You have won $" winningAmount);
}
//Check to see if 2 of the images match
else if ((image1Index == image2Index) || (image2Index == image3Index) || (image3Index == image1Index))
{
//Multiply the users entered amount by 2
winningAmount = amount * 2;
//Display the winning amount
MessageBox.Show("You have won $" winningAmount);
}
//If none of the images match
else if ((image1Index != image2Index) || (image2Index != image3Index) || (image3Index != image1Index))
{
//Set the winning amount to zero
winningAmount = 0;
//Display that the user has won zero dollars
MessageBox.Show("You have won $0");
}
//Add the users entered amount to the total amount variable
totalAmount = totalAmount amount;
//Add the winning amount to the total winning amount variable
//This amount should be display when the user hits the exit button
totalWinningAmount = totalWinningAmount winningAmount;
}
}
private void exitButton_Click(object sender, EventArgs e)
{
//Message box to show how much money the user has entered and how much they have won
MessageBox.Show("You have entered $" totalAmount ". You have won $" totalWinningAmount);
//Exit the game
this.Close();
}
}
THE ERRORS:
- Use of unassigned local variable 'totalAmount' - Line 82
- Use of unassigned local variable 'totalWinningAmount' - Line 86
- Use of unassigned local variable 'winningAmount' - Line 86
- The name 'totalAmount' does not exist in the current context - Line 93
- The name 'totalWinningAmount' does not exist in the current context - Line 93
CodePudding user response:
Moved my variables that were declared inside the spin button function to the class level so that all functions could access their information.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SlotMach
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Decalre variable to store the amount entered, winning amount, total amount, and total winning amount
double amount;
double winningAmount;
double totalAmount;
double totalWinningAmount;
private void spinButton_Click(object sender, EventArgs e)
{
//Take the users entered amount from the text box and check to see if it is a double data type
bool isValidAmount = Double.TryParse(amountTextBox.Text, out amount);
//If the amount entered is a double data type the the program continues
if (isValidAmount)
{
//Get the total number of images in the Image List Control
int numberOfImages = picturesImageList.Images.Count;
//Create a Random object to generate random numbers
Random rand = new Random();
//Store the 3 images in 3 random locations
int image1Index = rand.Next(0, numberOfImages);
int image2Index = rand.Next(0, numberOfImages);
int image3Index = rand.Next(0, numberOfImages);
//Display the 3 images in the picture boxes
image1PictureBox.Image = picturesImageList.Images[image1Index];
image2PictureBox.Image = picturesImageList.Images[image2Index];
image3PictureBox.Image = picturesImageList.Images[image3Index];
//Check to see if all 3 images match
if ((image1Index == image2Index) && (image2Index == image3Index))
{
//Multiply the users entered amount by 3
winningAmount = amount * 3;
//Display the winning amount
MessageBox.Show("You have won $" winningAmount);
}
//Check to see if 2 of the images match
else if ((image1Index == image2Index) || (image2Index == image3Index) || (image3Index == image1Index))
{
//Multiply the users entered amount by 2
winningAmount = amount * 2;
//Display the winning amount
MessageBox.Show("You have won $" winningAmount);
}
//If none of the images match
else if ((image1Index != image2Index) || (image2Index != image3Index) || (image3Index != image1Index))
{
//Set the winning amount to zero
winningAmount = 0;
//Display that the user has won zero dollars
MessageBox.Show("You have won $0");
}
//Add the users entered amount to the total amount variable
totalAmount = totalAmount amount;
//Add the winning amount to the total winning amount variable
//This amount should be display when the user hits the exit button
totalWinningAmount = totalWinningAmount winningAmount;
}
}
private void exitButton_Click(object sender, EventArgs e)
{
//Message box to show how much money the user has entered and how much they have won
MessageBox.Show("You have entered $" totalAmount ". You have won $" totalWinningAmount);
//Exit the game
this.Close();
}
}
}
CodePudding user response:
The Use of unassigned local variable
error means that you're trying to access the value of a variable that hasn't been assigned yet. Variables declared inside classes will get a default value, but if declared inside methods they do not. Try assigning values to a variables when declaring them inside a method.
NOTE: Variables should always be declared in the least accessible scope as possible.
The The name 'xxx' does not exist in the current context
error means that you're trying to access a variable that isn't accessible from that location in code. A variable declared inside a function is only accessible within that function. Declare them at the class level if you want them to be available to multiple functions within the class.
Also, just as an FYI, since there are only three images, if you fail verification that all three match (first if) and fail verification that two of them match (second if), then you don't need to verify that none of them match as you're doing in the last block. Just assign the variable to 0
to begin with and it's set correctly if the first two conditions fail.
For example:
public partial class Form1 : Form
{
private double totalAmount;
private double totalWinningAmount;
private void spinButton_Click(object sender, EventArgs e)
{
double amount;
if (Double.TryParse(amountTextBox.Text, out amount))
{
Random rnd = new Random();
int img1Index = rnd.Next(0, picturesImageList.Images.Count);
int img2Index = rnd.Next(0, picturesImageList.Images.Count);
int img3Index = rnd.Next(0, picturesImageList.Images.Count);
image1PictureBox.Image = picturesImageList.Images[img1Index];
image2PictureBox.Image = picturesImageList.Images[img2Index];
image3PictureBox.Image = picturesImageList.Images[img3Index];
double winningAmount = 0;
if ((img1Index == image2Index) && (img2Index == img3Index))
{
winningAmount = amount * 3;
}
else if ((img1Index == img2Index) ||
(img2Index == img3Index) ||
(image3Index == image1Index))
{
winningAmount = amount * 2;
}
MessageBox.Show("You have entered a total of $" amount
". You have won a total of $" winningAmount);
totalAmount = amount;
totalWinningAmount = winningAmount;
}
else
{
MessageBox.Show(amountTextBox.Text " is not a valid amount. "
"Please correct it and try again.");
}
}
private void exitButton_Click(object sender, EventArgs e)
{
MessageBox.Show("You have entered $" totalAmount
". You have won $" totalWinningAmount);
this.Close();
}
}