using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; /* Program Name: Slot Machine Simulator * Author: Garritt McCune * Date: 04/21/14 * Purpose: To simulate the function of a slot machine and determent the payout based on the number of matched fruit. */ namespace SlotMachineSimulator { public partial class mainForm : Form { //store the total payout and total inserted money private decimal totalAmountInserted; private decimal totalPayout; public mainForm() { InitializeComponent(); } private void spinButton_Click(object sender, EventArgs e) { //create a variable to store the user's input decimal amountInserted = 0; //begin by validating the user's input if (Decimal.TryParse(amountInsertedTextBox.Text, out amountInserted)) { //create a random object Random rand = new Random(); //get the images for the slots at random int pictureOne = rand.Next(fruitImageList.Images.Count); int pictureTwo = rand.Next(fruitImageList.Images.Count); int pictureThree = rand.Next(fruitImageList.Images.Count); decimal payout; //add to the total amount inserted to the machine totalAmountInserted += amountInserted; //check to see if any of the images match payout = CheckForMatches(pictureOne, pictureTwo, pictureThree, amountInserted); //add up the total amount of payout totalPayout += payout; //set the images according to the random index generated above slotPicture1.Image = fruitImageList.Images[pictureOne]; slotPicture2.Image = fruitImageList.Images[pictureTwo]; slotPicture3.Image = fruitImageList.Images[pictureThree]; MessageBox.Show("Your payout: " + payout.ToString("C") + "."); amountInsertedTextBox.Focus(); amountInsertedTextBox.SelectAll(); } else { //display error and reset focus to the text box MessageBox.Show("Please enter a valid amount.", "Invalid Amount"); amountInsertedTextBox.Focus(); amountInsertedTextBox.SelectAll(); } } private decimal CheckForMatches(decimal slotOne, decimal slotTwo, decimal slotThree, decimal amount) { //set the default payout decimal payout = 0; //check for any matches if (slotOne == slotTwo || slotOne == slotThree) { if (slotOne == slotTwo && slotOne == slotThree) { //if all slots match multiply the amount by three payout = amount * 3; } else { //if only two match multiply the amount by two payout = amount * 2; } } else if (slotTwo == slotOne || slotTwo == slotThree) { if (slotTwo == slotOne && slotTwo == slotThree) { //if all slots match multiply the amount by three payout = amount * 3; } else { //if only two match multiply the amount by two payout = amount * 2; } } else if (slotThree == slotOne || slotThree == slotTwo) { if (slotThree == slotOne && slotThree == slotTwo) { //if all slots match multiply the amount by three payout = amount * 3; } else { //if only two match multiply the amount by two payout = amount * 2; } } else { //if no matches are found, then the user has won nothing payout = 0; } return payout; } private void exitButton_Click(object sender, EventArgs e) { //display the total payout and total amount of money inserted MessageBox.Show(" You inserted a total of " + totalAmountInserted.ToString("C") + ".\n With a total payout of " + totalPayout.ToString("C") + ".", "Slot Machine Stats"); //close the application Close(); } private void exitFileMainMenu_Click(object sender, EventArgs e) { //display the total payout and total amount of money inserted MessageBox.Show(" You inserted a total of " + totalAmountInserted.ToString("C") + ".\n With a total payout of " + totalPayout.ToString("C") + ".", "Slot Machine Stats"); //close the application Close(); } private void resetEditMainMenu_Click(object sender, EventArgs e) { //start by redoing the pictures //create a random object again Random rand = new Random(); //get the images for the slots at random int index = rand.Next(fruitImageList.Images.Count); int index2 = rand.Next(fruitImageList.Images.Count); int index3 = rand.Next(fruitImageList.Images.Count); //set the images according to the random index generated above slotPicture1.Image = fruitImageList.Images[index]; slotPicture2.Image = fruitImageList.Images[index2]; slotPicture3.Image = fruitImageList.Images[index3]; //display the summary MessageBox.Show("You inserted a total of " + totalAmountInserted.ToString("C") + ".\n With a total payout of " + totalPayout.ToString("C") + ".", "Slot Machine Stats"); //reset all the counters totalPayout = 0; totalAmountInserted = 0; //clear the form amountInsertedTextBox.Text = ""; amountInsertedTextBox.Focus(); amountInsertedTextBox.SelectAll(); } } }