Jump to content

C# Slot Machine script


Gwendalin

Recommended Posts

Ok so I like programming casino games. I have made slot machines, video poker, 21, and a few others for a private Ragnarok server, and Eclipse. I figured I'd give it a go and make a script to be used in an engine that uses C# or i guess .net. Currently this slot machine does work as a stand alone game... i use windows forms so it is ugly but functional.

Be nice to me... i haven't scripted anything in like 4 or more years, and this is the first c# program I've completed on my own. I'm sure it could be optimized better, and some things are probably unneeded. (I don't know when or why you use properties instead of just a standard variable, but my book said you should use them so i did.) Half way through putting in all the stupid pay out labels, and trying to line them up i realized i should have used a table, but by then i was to far in! It is also really ugly, as I am not very creative.

The ultimate goal for this is so someone could add a slot machine to their game. They should be able to copy the code, and change the parts that use windows form items to their engine's UI... like the buttons, and labels. This slot machine is based on a real world slot machine, so as long as the random generator works correctly it should be fairly hard to actually win all the time. I set it up so you can change the bet amounts, the names of the stops of the chambers, and the totals using the variables at the top. To change the name of the stops you just need to do a find/replace all.

Here is the full project. The game exe is in the zip file too.

http://s000.tinyupload.com/?file_id=08413739405804981570

here is the code... This just contains the script for the actual workings of the slot machine. i am not displaying the code for the windows form.

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 Slots
{
    public partial class Form1 : Form
    {
        private static Random rVal;

        //Sets value for each bet button
        private const int bet1 = 5;
        private const int bet2 = 10;
        private const int bet3 = 15;

        //values for each reel. Can be called anything just do a find/replace all on each value. 
        //Currently set for a real slot, change values to make slot harder or easier
        private string[] reel1Values = { "2 bar", "2 bar", "2 bar", "blank", "blank", "3 bar", "3 bar", "blank", "blank", "blank", "white 7", "white 7", "white 7", "white 7", "white 7", "white 7",
                                           "blank", "blank", "blank", "1 bar", "1 bar", "1 bar", "blank", "blank", "blank", "blue 7", "blue 7", "blue 7", "blue 7", "blue 7", "blue 7", "blank",
                                           "blank", "blank", "2 bar", "2 bar", "blank", "blank", "3 bar", "blank", "blank", "blank", "blank", "blank", "red 7", "blank", "blank", "blank",
                                           "blank", "blank", "3 bar", "3 bar", "3 bar", "blank", "blank", "2 bar", "2 bar", "blank", "blank", "1 bar", "1 bar", "1 bar", "blank", "blank" };

        private string[] reel2Values = { "2 bar", "2 bar", "blank", "blank", "3 bar", "3 bar", "blank", "blank", "blank", "white 7", "blank", "blank", "blank", "1 bar", "1 bar", "1 bar", "1 bar",
                                           "blank", "blank", "blank", "blue 7", "blue 7", "blue 7", "blue 7", "blue 7", "blue 7", "blue 7", "blank", "blank", "blank", "2 bar", "2 bar", "blank",
                                           "blank", "3 bar", "3 bar", "blank", "blank", "blank", "blank", "blank", "red 7", "red 7", "red 7", "blank", "blank", "blank", "blank", "blank",
                                           "3 bar", "3 bar", "3 bar", "blank", "blank", "2 bar", "2 bar", "blank", "blank", "1 bar", "1 bar", "1 bar", "1 bar", "blank", "blank" };

        private string[] reel3Values = { "2 bar", "2 bar", "2 bar", "blank", "blank", "3 bar", "blank", "blank", "blank", "white 7", "white 7", "white 7", "white 7", "white 7", "white 7", "white 7",
                                           "blank", "blank", "blank", "1 bar", "1 bar", "1 bar", "1 bar", "1 bar", "blank", "blank", "blank", "blue 7", "blank", "blank", "blank", "2 bar",
                                           "2 bar", "2 bar", "blank", "blank", "3 bar", "blank", "blank", "blank", "blank", "blank", "red 7", "blank", "blank", "blank", "blank", "blank",
                                           "3 bar", "3 bar", "3 bar", "blank", "blank", "2 bar", "2 bar", "2 bar", "blank", "blank", "1 bar", "1 bar", "1 bar", "1 bar", "blank", "blank" };

        //How much you win based on winning combinations
        private int[] winValues = { 0, 1, 2, 5, 10, 20, 25, 40, 50, 80, 150, 200, 1199, 2400, 5000, 2400, 4800, 10000 };


        private int bet; //How much has been bet. 
        private int total; //Total amount of credits
        private int c1SpinVal; //Random number for chamber 1
        private int c2SpinVal; //Random number for chamber 2
        private int c3SpinVal; //Random number for chamber 3
        private int c1CheckVal; //Value assigned to each stop on reel 1
        private int c2CheckVal; //Value assigned to each stop on reel 2
        private int c3CheckVal; //Value assigned to each stop on reel 3

        public int Total
        {
            get { return total; }
            set { total = value; }
        }
        public int Bet
        {
            get { return bet; }
            set { bet = value; }
        }
        public int Bet1
        {
            get { return bet1; }
        }
        public int Bet2
        {
            get { return bet2; }
        }
        public int Bet3
        {
            get { return bet3; }
        }
        public int C1SpinVal
        {
            get { return c1SpinVal; }
            set { c1SpinVal = value; }
        }
        public int C2SpinVal
        {
            get { return c2SpinVal; }
            set { c2SpinVal = value; }
        }
        public int C3SpinVal
        {
            get { return c3SpinVal; }
            set { c3SpinVal = value; }
        }
        public int C1CheckVal
        {
            get { return c1CheckVal; }
            set { c1CheckVal = value; }
        }
        public int C2CheckVal
        {
            get { return c2CheckVal; }
            set { c2CheckVal = value; }
        }
        public int C3CheckVal
        {
            get { return c3CheckVal; }
            set { c3CheckVal = value; }
        }


        public Form1()
        {
            InitializeComponent();
                        
            Total = 1000; // Set total credits. Should be read from a characters items. 
            lblPaid.Text = "Winner Paid: 0"; // Sets winiing amount to 0
            btnBet1.Text = "Bet: " + Bet1; // Sets button's bet dispaly to bet1
            btnBet2.Text = "Bet: " + Bet2; // sets button's bet display to bet2
            btnBet3.Text = "Bet: " + Bet3; // sets buttons bet display to bet 3
            Reset(); // resets other lables to 0, and starts a new random number seed

            //puts a rand value into the slot chambers when starting the game
            C1SpinVal = rVal.Next(64); //gets random number
            SetFont(); // changes the font color of the chamber based on stop value
            txtboxChamber1.Text = reel1Values[C1SpinVal]; //puts in the stop value into the chamber

            C2SpinVal = rVal.Next(64);
            SetFont();
            txtboxChamber2.Text = reel2Values[C2SpinVal];

            C3SpinVal = rVal.Next(64);
            SetFont();
            txtboxChamber3.Text = reel3Values[C3SpinVal];                        
        }

        private void btnBet1_Click_1(object sender, EventArgs e)
        {
            Bet = Bet1; //sets the bet amount to the value of bet1 
            lblCredits.Text = "Total Credits: " + (Total - Bet); // Shows how many credits will remain after the bet
            lblBet.Text = "Bet: " + Bet.ToString(); // shows how much you are betting
            lblStatus.Visible = false; // hides the winning or losing status
        }

        private void btnBet2_Click_1(object sender, EventArgs e)
        {
            Bet = Bet2;
            lblCredits.Text = "Total Credits: " + (Total - Bet);
            lblBet.Text = "Bet: " + Bet.ToString();
            lblStatus.Visible = false;        
        }

        private void btnBet3_Click(object sender, EventArgs e)
        {
            Bet = Bet3;
            lblCredits.Text = "Total Credits: " + (Total - Bet);
            lblBet.Text = "Bet: " + Bet.ToString();
            lblStatus.Visible = false;
        }

        private void btnSpin_Click(object sender, EventArgs e)
        {
            if (Bet > 0) // only lets you spin if you bet
            {
                Total -= Bet; //reduce credits by bet amount

                C1SpinVal = rVal.Next(64); //gets random number
                SetFont(); // changes the font color of the chamber based on stop value
                txtboxChamber1.Text = reel1Values[C1SpinVal]; //puts in the stop value into the chamber
                C1CheckVal = GetCheckVal(reel1Values[C1SpinVal]); // sets value to each stop 
                
                C2SpinVal = rVal.Next(64);
                SetFont();
                txtboxChamber2.Text = reel2Values[C2SpinVal];
                C2CheckVal = GetCheckVal(reel2Values[C2SpinVal]);

                C3SpinVal = rVal.Next(64);
                SetFont();
                txtboxChamber3.Text = reel3Values[C3SpinVal];
                C3CheckVal = GetCheckVal(reel3Values[C3SpinVal]);

                CheckWin(); // checks if stop values are a winning combination
            }
        }

        private int GetCheckVal(string value)
        {
            int check;

            switch (value) // switch case based on stop value from the reelvalue array
            {
                case "blank":
                    check = 1; //if value in reelvalue arry = blank assign stop value 1
                    return check;
                case "1 bar":
                    check = 2;
                    return check;
                case "2 bar":
                    check = 3;
                    return check;
                case "3 bar":
                    check = 4;
                    return check;
                case "blue 7":
                    check = 5;
                    return check;
                case "white 7":
                    check = 6;
                    return check;
                case "red 7":
                    check = 7;
                    return check;
            }
            return 0;
        }

        private void CheckWin()
        {
            int win = 0; // there are 13 possible winning combinations. Assign a win for each combination
            int winningTotal = 0; // Total amount won

            if (C1CheckVal == 1 && C2CheckVal == 1 && C3CheckVal == 1) { win = 1; } // blank, blank, blank
            if ((C1CheckVal == 4 || C1CheckVal == 5) && (C2CheckVal == 4 || C2CheckVal == 5) && (C3CheckVal == 4 || C3CheckVal == 5)) { win = 2; } // any 3 blue
            if ((C1CheckVal == 3 || C1CheckVal == 6) && (C2CheckVal == 3 || C2CheckVal == 6) && (C3CheckVal == 3 || C3CheckVal == 6)) { win = 2; } // any 3 white
            if ((C1CheckVal == 2 || C1CheckVal == 7) && (C2CheckVal == 2 || C2CheckVal == 7) && (C3CheckVal == 2 || C3CheckVal == 7)) { win = 2; } // any 3 red
            if ((C1CheckVal == 2 || C1CheckVal == 3 || C1CheckVal == 4) && (C2CheckVal == 2 || C2CheckVal == 3 || C2CheckVal == 4) && (C3CheckVal == 2 || C3CheckVal == 3 || C3CheckVal == 4)) { win = 3; }  //any 3 bars
            if (C1CheckVal == 2 && C2CheckVal == 2 && C3CheckVal == 2) { win = 4; } // 1 bar, 1bar, 1bar
            if ((C1CheckVal == 2 || C1CheckVal == 7) && (C2CheckVal == 3 || C2CheckVal == 6) && (C3CheckVal == 4 || C3CheckVal == 5)) { win = 5; } // any red, any white, any blue
            if (C1CheckVal == 3 && C2CheckVal == 3 && C3CheckVal == 3) { win = 6; } // 2 bar, 2 bar, 2 bar
            if (C1CheckVal == 4 && C2CheckVal == 4 && C3CheckVal == 4) { win = 7; } // 3 bar, 3 bar, 3 bar
            if (C1CheckVal == 2 && C2CheckVal == 3 && C3CheckVal == 4) { win = 8; } // 1 bar, 2 bar, 3 bar
            if ((C1CheckVal == 5 || C1CheckVal == 6 || C1CheckVal == 7) && (C2CheckVal == 5 || C2CheckVal == 6 || C2CheckVal == 7) && (C3CheckVal == 5 || C3CheckVal == 6 || C3CheckVal == 7)) { win = 9; } // any 3 7's
            if (C1CheckVal == 5 && C2CheckVal == 5 && C3CheckVal == 5) { win = 10; } // blue 7, blue 7, blue 7
            if (C1CheckVal == 6 && C2CheckVal == 6 && C3CheckVal == 6) { win = 11; } // white 7, white 7, white 7
            if (C1CheckVal == 7 && C2CheckVal == 7 && C3CheckVal == 7) { win = 12; } // red 7, red 7, red 7
            if (C1CheckVal == 7 && C2CheckVal == 6 && C3CheckVal == 5) { win = 13; } // red 7, white 7, blue 7

            // Top two winning combinations have differnt pay ammount. this checks for the top two win combinations
            if (win == 12)
            {
                if (Bet == Bet1)
                {
                    winningTotal = winValues[12] * Bet1; // set winning total to value of win * bet
                    lblStatus.Text = "Big Winner"; // set status lable to winner
                    lblStatus.Visible = true; // show status
                }
                if (Bet == Bet2)
                {
                    winningTotal = winValues[13] * Bet2;
                    lblStatus.Text = "Big Winner";
                    lblStatus.Visible = true;
                }
                if (Bet == Bet3)
                {

                    winningTotal = winValues[14] * Bet3;
                    lblStatus.Text = "Big Winner";
                    lblStatus.Visible = true;
                }
            }
            else if (win == 13)
            {
                if (Bet == Bet1)
                {
                    winningTotal = winValues[15] * Bet1;
                    lblStatus.Text = "Big Winner";
                    lblStatus.Visible = true;
                }
                if (Bet == Bet2)
                {
                    winningTotal = winValues[16] * Bet2;
                    lblStatus.Text = "Big Winner";
                    lblStatus.Visible = true;
                }
                if (Bet == Bet3)
                {
                    winningTotal = winValues[17] * Bet3;
                    lblStatus.Text = "Big Winner";
                    lblStatus.Visible = true;
                }
            }
            else
            {
                winningTotal = winValues[win] * Bet;
                lblStatus.Text = "Winner";
                lblStatus.Visible = true;
            }

            if (win == 0)
            {
                lblStatus.Text = "Lose";
                lblStatus.Visible = true;
            }

            lblPaid.Text = "Winner Paid: " + winningTotal; // show how much was won
            Total = Total + winningTotal; // add winning total to total credits
            Reset(); // reset bet lable, get new random number seed
        }

        // sets font color of the chamber values
        private void SetFont()
        {
            if (reel1Values[C1SpinVal] == "blank") { txtboxChamber1.ForeColor = Color.Yellow; }
            if (reel1Values[C1SpinVal] == "3 bar" || reel1Values[C1SpinVal] == "blue 7") { txtboxChamber1.ForeColor = Color.Blue; }
            if (reel1Values[C1SpinVal] == "2 bar" || reel1Values[C1SpinVal] == "white 7") { txtboxChamber1.ForeColor = Color.White; }
            if (reel1Values[C1SpinVal] == "1 bar" || reel1Values[C1SpinVal] == "red 7") { txtboxChamber1.ForeColor = Color.Red; }
            if (reel1Values[C1SpinVal] == "3 bar" || reel1Values[C1SpinVal] == "blue 7") { txtboxChamber1.ForeColor = Color.Blue; }

            if (reel2Values[C2SpinVal] == "blank") { txtboxChamber2.ForeColor = Color.Yellow; }
            if (reel2Values[C2SpinVal] == "3 bar" || reel2Values[C2SpinVal] == "blue 7") { txtboxChamber2.ForeColor = Color.Blue; }
            if (reel2Values[C2SpinVal] == "2 bar" || reel2Values[C2SpinVal] == "white 7") { txtboxChamber2.ForeColor = Color.White; }
            if (reel2Values[C2SpinVal] == "1 bar" || reel2Values[C2SpinVal] == "red 7") { txtboxChamber2.ForeColor = Color.Red; }
            if (reel2Values[C2SpinVal] == "3 bar" || reel2Values[C2SpinVal] == "blue 7") { txtboxChamber2.ForeColor = Color.Blue; }

            if (reel3Values[C3SpinVal] == "blank") { txtboxChamber3.ForeColor = Color.Yellow; }
            if (reel3Values[C3SpinVal] == "3 bar" || reel3Values[C3SpinVal] == "blue 7") { txtboxChamber3.ForeColor = Color.Blue; }
            if (reel3Values[C3SpinVal] == "2 bar" || reel3Values[C3SpinVal] == "white 7") { txtboxChamber3.ForeColor = Color.White; }
            if (reel3Values[C3SpinVal] == "1 bar" || reel3Values[C3SpinVal] == "red 7") { txtboxChamber3.ForeColor = Color.Red; }
            if (reel3Values[C3SpinVal] == "3 bar" || reel3Values[C3SpinVal] == "blue 7") { txtboxChamber3.ForeColor = Color.Blue; }
        }

        private void Reset()
        {
            rVal = new Random(); // gets new random number seed
            Bet = 0; // sets bet amout to 0
            lblBet.Text = "Bet: " + Bet; // sets bet lable to bet ammount
            lblCredits.Text = "Total Credits: " + Total.ToString(); //shows amount of credits available
        }
    }
}

Here is my incredibly ugly slot machine game!  :P

slot_zps2l6blgvl.png

Link to comment
Share on other sites

cool... next i'm going to make video poker and blackjack.

I got super close to making a multiplayer poker game in Ragnarok, but our server died before i could finish it.

I have some other gambling games in mind too.

Link to comment
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
×
×
  • Create New...