Monday, April 2, 2012

CIS 217: Enumerated Data Types &, Structures Assignment [Slot Machine Sim]

Another assignment for Chapter 8, the slot machine simulator was a quick and easy project to have some "fun" with. I may pass it off to my wife to see if she has a problem gambling ;-)

Again, my first step was to add an ImageList control ("imglSlots") to the form, set it's ImageSize property to 128 x 128, and add the images that I was going to use. My form looked just as it did in the book, so no change there.

My second step was to create the Enumerated data-type to hold all of the possible fruit choices:
    public enum Fruit { Apple, Banana, Cherries, 
                        Grapes, Lemon, Lime, 
                        Orange, Pear, Strawberry, 
                        Watermelon }

My third step was to create a Spin() method for each slot, called "Slot1_Spin()", which determines what the result is going to be out of the total possible results, and displays the resulting fruit image from the ImageList control. These were created for each slot, and thrown into the _Click event of the "Spin" button:
        public void Slot1_Spin()
        {
            int index = rand.Next(0, 10);         

            switch (index)
            {
                case 0:
                    Slot1Fruit = Fruit.Apple;
                    pbSlot1.Image = imglSlots.Images[index];
                    break;
                case 1:
                    Slot1Fruit = Fruit.Banana;
                    pbSlot1.Image = imglSlots.Images[index];
                    break;
            }
        }

My fourth step, although probably considered even more simple than creating the above methods, was to create the conditionals to deal with winning the game:
                if (Slot1Fruit == Slot2Fruit)
                {
                    Winnings = 2 * AmtEntered;
                    Total += Winnings;
                    MessageBox.Show("You have won 2x the amount entered (" + Winnings.ToString("c") + ")");
                    Win = true;
                }

My thoughts on this section of the chapter are pretty simple: enumerated data types are very important to learn, which I think the examples given in the book plainly show (creating the "days of the week"). Yes, there are other ways to do it, but enums make the process much simpler. The only irksome thing that I've ever had to deal with while using enums is what is also discussed in the book, being that you cannot directly compare some things to the enum itself, but I'm sure that caveat will be addressed later in the book for those who don't know how to get around it.

*Note: Yes, I skimped on the TextBox and getting the bet info from the user, however I wanted to spend time with my wife and children since I haven't seen them much this past week, and so I took shortcuts where I could without impacting the overall running of the program (unless you're blatantly trying to break it).

Here is the full code for the assignment:
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 SlotMachineSimulation
{
    public enum Fruit { Apple, Banana, Cherries, 
                        Grapes, Lemon, Lime, 
                        Orange, Pear, Strawberry, 
                        Watermelon }
    
    public partial class frmMain : Form
    {
        public Random rand = new Random();
        public Fruit Slot1Fruit, Slot2Fruit, Slot3Fruit;
        public double AmtEntered, Total, Winnings;

        public frmMain()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            MessageBox.Show("You won a total of: $" + Total.ToString());
            this.Close();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            pbSlot1.Image = imglSlots.Images[0];
            pbSlot2.Image = imglSlots.Images[1];
            pbSlot3.Image = imglSlots.Images[2];
        }

        private void btnSpin_Click(object sender, EventArgs e)
        {
            if (txtAmtInserted.Text == "")
            {
                MessageBox.Show("Please place a bet!");
            }
            else
            {
                AmtEntered = double.Parse(txtAmtInserted.Text);
                bool Win = false;

                Slot1_Spin();
                Slot2_Spin();
                Slot3_Spin();

                if (Slot1Fruit == Slot2Fruit)
                {
                    Winnings = 2 * AmtEntered;
                    Total += Winnings;
                    MessageBox.Show("You have won 2x the amount entered (" + Winnings.ToString("c") + ")");
                    Win = true;
                }

                if (Slot2Fruit == Slot3Fruit)
                {
                    Winnings = 2 * AmtEntered;
                    Total += Winnings;
                    MessageBox.Show("You have won 2x the amount entered (" + Winnings.ToString("c") + ")");
                    Win = true;
                }
                if (Slot1Fruit == Slot2Fruit && Slot2Fruit == Slot3Fruit)
                {
                    Winnings = 3 * AmtEntered;
                    Total += Winnings;
                    MessageBox.Show("You have won 3x the amount entered (" + Winnings.ToString("c") + ")");
                    Win = true;
                }
            }
        }


        public void Slot1_Spin()
        {
            int index = rand.Next(0, 10);         

            switch (index)
            {
                case 0:
                    Slot1Fruit = Fruit.Apple;
                    pbSlot1.Image = imglSlots.Images[index];
                    break;
                case 1:
                    Slot1Fruit = Fruit.Banana;
                    pbSlot1.Image = imglSlots.Images[index];
                    break;
                case 2:
                    Slot1Fruit = Fruit.Cherries;
                    pbSlot1.Image = imglSlots.Images[index];
                    break;
                case 3:
                    Slot1Fruit = Fruit.Grapes;
                    pbSlot1.Image = imglSlots.Images[index];
                    break;
                case 4:
                    Slot1Fruit = Fruit.Lemon;
                    pbSlot1.Image = imglSlots.Images[index];
                    break;
                case 5:
                    Slot1Fruit = Fruit.Lime;
                    pbSlot1.Image = imglSlots.Images[index];
                    break;
                case 6:
                    Slot1Fruit = Fruit.Orange;
                    pbSlot1.Image = imglSlots.Images[index];
                    break;
                case 7:
                    Slot1Fruit = Fruit.Pear;
                    pbSlot1.Image = imglSlots.Images[index];
                    break;
                case 8:
                    Slot1Fruit = Fruit.Strawberry;
                    pbSlot1.Image = imglSlots.Images[index];
                    break;
                case 9:
                    Slot1Fruit = Fruit.Watermelon;
                    pbSlot1.Image = imglSlots.Images[index];
                    break;
            }
        }

        public void Slot2_Spin()
        {
            int index = rand.Next(0, 10);

            switch (index)
            {
                case 0:
                    Slot2Fruit = Fruit.Apple;
                    pbSlot2.Image = imglSlots.Images[index];
                    break;
                case 1:
                    Slot2Fruit = Fruit.Banana;
                    pbSlot2.Image = imglSlots.Images[index];
                    break;
                case 2:
                    Slot2Fruit = Fruit.Cherries;
                    pbSlot2.Image = imglSlots.Images[index];
                    break;
                case 3:
                    Slot2Fruit = Fruit.Grapes;
                    pbSlot2.Image = imglSlots.Images[index];
                    break;
                case 4:
                    Slot2Fruit = Fruit.Lemon;
                    pbSlot2.Image = imglSlots.Images[index];
                    break;
                case 5:
                    Slot2Fruit = Fruit.Lime;
                    pbSlot2.Image = imglSlots.Images[index];
                    break;
                case 6:
                    Slot2Fruit = Fruit.Orange;
                    pbSlot2.Image = imglSlots.Images[index];
                    break;
                case 7:
                    Slot2Fruit = Fruit.Pear;
                    pbSlot2.Image = imglSlots.Images[index];
                    break;
                case 8:
                    Slot2Fruit = Fruit.Strawberry;
                    pbSlot2.Image = imglSlots.Images[index];
                    break;
                case 9:
                    Slot2Fruit = Fruit.Watermelon;
                    pbSlot2.Image = imglSlots.Images[index];
                    break;
            }
        }

        public void Slot3_Spin()
        {
            int index = rand.Next(0, 10);

            switch (index)
            {
                case 0:
                    Slot3Fruit = Fruit.Apple;
                    pbSlot3.Image = imglSlots.Images[index];
                    break;
                case 1:
                    Slot3Fruit = Fruit.Banana;
                    pbSlot3.Image = imglSlots.Images[index];
                    break;
                case 2:
                    Slot3Fruit = Fruit.Cherries;
                    pbSlot3.Image = imglSlots.Images[index];
                    break;
                case 3:
                    Slot3Fruit = Fruit.Grapes;
                    pbSlot3.Image = imglSlots.Images[index];
                    break;
                case 4:
                    Slot3Fruit = Fruit.Lemon;
                    pbSlot3.Image = imglSlots.Images[index];
                    break;
                case 5:
                    Slot3Fruit = Fruit.Lime;
                    pbSlot3.Image = imglSlots.Images[index];
                    break;
                case 6:
                    Slot3Fruit = Fruit.Orange;
                    pbSlot3.Image = imglSlots.Images[index];
                    break;
                case 7:
                    Slot3Fruit = Fruit.Pear;
                    pbSlot3.Image = imglSlots.Images[index];
                    break;
                case 8:
                    Slot3Fruit = Fruit.Strawberry;
                    pbSlot3.Image = imglSlots.Images[index];
                    break;
                case 9:
                    Slot3Fruit = Fruit.Watermelon;
                    pbSlot3.Image = imglSlots.Images[index];
                    break;
            }
        }
    }
}

1 comment:

  1. I took a lazy way to the slot machine, using imglists similar to the book freed up my code. I lumped it into a method called Winning(). It looked something like.


    public Form1()
    {
    InitializeComponent();
    }
    double WinningTotal;
    double BettingTotal;

    private void Winning()
    {
    Random rand = new Random();

    int index1 = rand.Next(FruitImageList.Images.Count);
    int index2 = rand.Next(FruitImageList.Images.Count);
    int index3 = rand.Next(FruitImageList.Images.Count);

    pb1.Image = FruitImageList.Images[index1];
    pb2.Image = FruitImageList.Images[index2];
    pb3.Image = FruitImageList.Images[index3];

    double bet;
    double winning;
    bet = int.Parse(tb_Bet.Text);

    if (index1 == index2 && index2 == index3)
    {
    winning = (bet * 3);
    MessageBox.Show("You have won $" + winning);
    WinningTotal += winning;
    BettingTotal += bet;
    }
    else if (index1 == index2)
    {
    winning = (bet * 2);
    MessageBox.Show("You have won $" + winning);
    WinningTotal += winning;
    BettingTotal += bet;
    }
    else if (index1 == index3)
    {
    winning = (bet * 2);
    MessageBox.Show("You have won $" + winning);
    WinningTotal += winning;
    BettingTotal += bet;
    }
    else if (index2 == index3)
    {
    winning = (bet * 2);
    MessageBox.Show("You have won $" + winning);
    WinningTotal += winning;
    BettingTotal += bet;
    }
    else
    {
    winning = (bet * 0);
    MessageBox.Show("You have won $" + winning);
    WinningTotal += winning;
    BettingTotal += bet;
    }
    }

    ReplyDelete