RETRO SLOT MACHINE Is it allowed? -- OK, I'll do it anyway

The powers to be can delete this post if they wish.
(Not that I have anything to say about it!)

I threw together some code for my Outrageous RETRO just for fun.

I am providing the code here if anyone wants to try it.

Not sure if it is good enough to post on mbed. Besides, I don’t know how :-[
Not sure if the code is in anyway structured as it should be but it seems to work…



//NOTE: For some reason: Sometimes I do not see Portable Devices CRP DISABLED when I reset the RETRO.
//Going to Device Manager and selecting 'Scan for hardware changes' corrects the problem?
//Using WIndows 8.1 Pro

//The RETRO LCD display code used in this application was developed by Chris Taylor (taylorza) <- This man is AMAZING!
//SPI based library for the ST7735 LCD controller
//See http://developer.mbed.org/users/taylorza/code/ for the current release


//I know nothing about payouts on a 'real' slot machine so I just made some up...
//Be creative and make your own!

//Been to Las Vegas many times but always for work related. The most I ever lost there was about $50.00 and went home happy!

 #include "mbed.h"
 #include <stdarg.h>
 #include <stdlib.h>
 #include "USBSerial.h"
 #include <string>

 #include "DisplayN18.h"
 #include "Color565.h"
 #include "font_IBM.h"
 #include "LCD_ST7735.h"

 bool spin = false;
 
 AnalogIn ain(P0_15);
 
 DigitalOut led1(P0_9, 0);
 DigitalOut led2(P0_8, 1);
 DigitalOut Buzzer(P0_18);
 
 DigitalIn up(P0_13, PullUp);
 DigitalIn down(P0_12, PullUp);
 DigitalIn left(P0_14, PullUp);
 DigitalIn right(P0_11, PullUp);

 DigitalIn pbRobot(P0_16, PullUp);
 
 //Falling edge (hit) generates an interrupt
 //Just wanted to try an interrupt
 InterruptIn pbSpaceship(P0_1);
 
 //Random numbers for reels()
 int r1 = 0;
 int r2 = 0;
 int r3 = 0;
 int r4 = 0;
 
 //Virtual serial port over USB.
 //Used for debug output only
 USBSerial serial;

 DisplayN18 disp;
 
 char buf[29] = "";
 
 bool doublepay = false;
 bool win = false;
 
 int cash = 100; //Start with
 int cashpay = 0; //Win or lose
 
 const char * const bar[] = {
 "BAR ",
 "BELL ",
 "ORANGE ",
 "LEMON ",
 "LUCKY7 ",
 "CHERRY " };

 const char* msg1 = "";
 const char* msg2 = "";
 const char* msg3 = "";
 //Screen width using spaces
 const char* szErase = "                            ";

 Ticker toggle;
 
void toggler() {

}
//

//From E-Dice
void soundBuzzer(int Hz)
{
    int ticks = Hz/64;
    int tickCount = 0;
    float frequency = 1/(float)Hz;
    
    while(tickCount < ticks)
    {
        wait(frequency);
        Buzzer = true;
        wait(frequency);
        Buzzer = false;
        tickCount++;
    }
}
//

void reels(void)
{
    disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
        strlen(szErase) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 6, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
    disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
        strlen(szErase) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 68, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
        
    while(spin) {
        
        srand(rand());
        r1 = (rand() % 6);
        r2 = (rand() % 6);
        r3 = (rand() % 6);
        r4 = (rand() % 50);
        
        msg1 = bar[r1];
        msg2 = bar[r2];
        msg3 = bar[r3];    

        //Use Terminal - Test random number values
        //serial.printf(" %d %d %d %d\r\n",r1,r2,r3,r4);
        
        char buf[29] = "";
        strncat(buf, msg1, sizeof buf);
        strncat(buf, msg2, sizeof buf - strlen(buf));
        strncat(buf, msg3, sizeof buf - strlen(buf));
    
        disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
            strlen(szErase) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 68, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
        disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
            strlen(buf) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 68, buf, DisplayN18::GREEN, DisplayN18::BLACK);
        
        //Robot button is Not pressed
        if(pbRobot) {
            disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
                strlen(szErase) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 78, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
        }
        //
        wait(0.1);
        
        //Robot button was pressed
        if(!pbRobot) {
            led2 = 1;
            led1 = 0;
            
            char buf[29] = "";
            sprintf(buf, "YOUR PLAY - Cash %d ", cash);
            
            disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
                strlen(szErase) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 78, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
            disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
                strlen(buf) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 78, buf, DisplayN18::GREEN, DisplayN18::BLACK);
            disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
                strlen(szErase) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 16, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
            
            /* If wanted
            //Display current random number for (r4)
            sprintf(buf," DOUBLE PAY on 34, 13, 7: %d  ", r4);
            disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
                strlen(szErase) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT + 73, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
            disp.drawString(0, DisplayN18::CHAR_HEIGHT + 73, buf, DisplayN18::BLUE, DisplayN18::BLACK);    
            */
            
            spin = false;
            
            //Spin a bit more - House rules
            for(int i =0; i <= 5; i++)
            {
                r1 = (rand() % 6);
                r2 = (rand() % 6);
                r3 = (rand() % 6);
                r4 = (rand() % 50);
                
                msg1 = bar[r1];
                msg2 = bar[r2];
                msg3 = bar[r3];
        
                char buf[29] = "";
                strncat(buf, msg1, sizeof buf);
                strncat(buf, msg2, sizeof buf - strlen(buf));
                strncat(buf, msg3, sizeof buf - strlen(buf));
            
                disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
                    strlen(szErase) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 68, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
                disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
                    strlen(buf) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 68, buf, DisplayN18::GREEN, DisplayN18::BLACK); 
            
                wait(.1);  
            } //for
            
            win = false;

            //bar[2] = ORANGE
            if ((msg1 == bar[2]) && (msg2 == bar[2]) && (msg3 == bar[2]))
            {
                cash = cash + 4;
                cashpay = 4;
                win = true;
            }
            if ((msg1 == bar[2]) && (msg2 == bar[2]))
            {
                cash = cash + 2;
                cashpay = 2;
                win = true;
            }
            if ((msg2 == bar[2]) && (msg3 == bar[2]))
            {
                cash = cash + 2;
                cashpay = 2;
                win = true;
            }
            //
            
            //bar[3] = LEMON
            if ((msg1 == bar[3]) && (msg2 == bar[3]) && (msg3 == bar[3]))
            {
                cash = cash + 4;
                cashpay = 4;
                win = true;
            }
            if ((msg1 == bar[3]) && (msg2 == bar[3]))
            {
                cash = cash + 2;
                cashpay = 2;
                win = true;
            }
            if ((msg2 == bar[3]) && (msg3 == bar[3]))
            {
                cash = cash + 2;
                cashpay = 2;
                win = true;
            }
            //
                       
            //bar[5] = CHERRY
            if ((msg1 == bar[5]) && (msg2 == bar[5]) && (msg3 == bar[5]))
            {
                cash = cash + 6;
                cashpay = 6;
                win = true;
            }
            if ((msg1 == bar[5]) && (msg2 == bar[5]))
            {
                cash = cash + 3;
                cashpay = 3;
                win = true;
            }
            if ((msg2 == bar[5]) && (msg3 == bar[5]))
            {
                cash = cash + 3;
                cashpay = 3;
                win = true;
            }
            //
            
            //bar[1] = BELL
            if ((msg1 == bar[1]) && (msg2 == bar[1]) && (msg3 == bar[1]))
            {
                cash = cash + 6;
                cashpay = 6;
                win = true;
            }
            if ((msg1 == bar[1]) && (msg2 == bar[1]))
            {
                cash = cash + 3;
                cashpay = 3;
                win = true;
            }
            if ((msg2 == bar[1]) && (msg3 == bar[1]))
            {
                cash = cash + 3;
                cashpay = 3;
                win = true;
            }
            //
            
            //bar[0] = BAR
            if ((msg1 == bar[0]) && (msg2 == bar[0]) && (msg3 == bar[0]))
            {
                cash = cash + 10;
                cashpay = 10;
                win = true;
            }
            if ((msg1 == bar[0]) && (msg2 == bar[0]))
            {
                cash = cash + 5;
                cashpay = 5;
                win = true;
            }
            if ((msg2 == bar[0]) && (msg3 == bar[0]))
            {
                cash = cash + 5;
                cashpay = 5;
                win = true;
            }
            //
            
            //bar[4] = LUCKY7
            if ((msg1 == bar[4]) && (msg2 == bar[4]) && (msg3 == bar[4]))
            {
                cash = cash + 20;
                cashpay = 20;
                win = true;
            }
            if ((msg1 == bar[4]) && (msg2 == bar[4]))
            {
                cash = cash + 10;
                cashpay = 10;
                win = true;
            }
            if ((msg2 == bar[4]) && (msg3 == bar[4]))
            {
                cash = cash + 10;
                cashpay = 10;
                win = true;
            }
            //
            
            if(win)
            {
                if(r4 == 35 || r4 == 13 || r4 == 7) {
                    sprintf(buf, "DOUBLE PAY %d  %d ", cash, cash * 2);
                }
                else {
                    sprintf(buf, "WIN! Pay: %d Cash: %d ", cashpay, cash);
                }   
            }
            //
            
            if(!win)
            {
                cash = cash - 2;
                sprintf(buf, "LOSE! 2 dollars - %d ", cash);        
            }
            //
            
            disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
                strlen(szErase) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 16, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
            disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
                strlen(buf) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 16, buf, DisplayN18::GREEN, DisplayN18::BLACK);

            if(win) {
               for(int i = 0; i < 5; i++) {
                    soundBuzzer(800);
                    wait(.1);
                    soundBuzzer(600);
                    wait(.1);
                    soundBuzzer(400);
                    wait(.1);
                }
            }
        } //if
    } //while
}
//

//Spaceship/RETRO button begins random pick of play bars
void pbSpaceship_hit_interrupt (void) {
    led1 = 1;
    led2 = 0;
    
    spin = true;
    srand(rand());
    
    reels();
}
//

int main() {
    
    led1 = 0;
    led2 = 0;
    pbRobot.mode(PullUp);
    //Delay for initial pullup to take effect
    wait(.01);
    
    pbSpaceship.mode(PullUp);
    //Delay for initial pullup to take effect
    wait(.01);
    //Attach address of interrupt handler routine for pushbutton
    pbSpaceship.fall(&pbSpaceship_hit_interrupt);
    
    //toggle.attach(&toggler, 1.0); //Attach toggler. Interval of 1 second.
    
    disp.clear(); 
    char buf[40] = "";
    sprintf(buf,"   OUTRAGEOUS RETRO Slot");
    disp.drawString(0, 0, buf, DisplayN18::GREEN, DisplayN18::BLACK);
    sprintf(buf,"Press RETRO to pull Lever");
    disp.drawString(0, DisplayN18::CHAR_HEIGHT + 3, buf, DisplayN18::RED, DisplayN18::BLACK);
    sprintf(buf,"Press ROBOT Lever up");
    disp.drawString(0, DisplayN18::CHAR_HEIGHT + 13, buf, DisplayN18::RED, DisplayN18::BLACK);
        
    while (true) {  
        wait(.2);     
    } //while
}
//


1 Like

@ willgeorge - Easy to publish on mbed. Just right-click your project in the mbed online editor, and click the publish item, as shown in the pic. Follow the instructions, and you’re good to go!

Then post a link to codeshare, and you’ve covered all your bases.

What about a video for those that do not have a retro? :slight_smile:

@ willgeorge - mbed would be the best place to share. It is so much easier to try the code that way. A follow up post in mbed section of this forum is nice too.

That what others have been doing so far.