Revised code. The original has been removed.

Dependencies:   USBDevice mbed

Game.cpp

Committer:
willgeorge
Date:
2015-02-03
Revision:
0:305fa754c01e

File content as of revision 0:305fa754c01e:



//I have removed Library LCD_ST7735 that was in the original post.
//Chris Taylor (taylorza) has reminded me that I was not using his library as I had stated.

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

//There is no calculated payback percentage (Odds).
//Fruits displayed and payout values for two of a kind are chosen by random numbers.
//LUCKY7 - All payouts are fixed values


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

#include "DisplayN18.h"

 bool spin = false;
 
 AnalogIn ain(P0_15);  //One of the user header pins
 
 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;
 int r5 = 0;
 int r6 = 0;
 
 //Virtual serial port over USB.
 //Used for debug output only
 USBSerial serial;

 DisplayN18 disp;
 
 bool doublepay = false;
 bool win = false;
 
 int cash = 100; //Start with
 int cashpay = 0; //Win or loss
 
 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 = "                          "; //26 spaces

//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)
{
    //Erase current text (-val = position from bottom of LCD)
    //Fruit bars
    disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -58, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
    
    //Erase current text 
    //DOUBLE PAY (May not be used but we will erase that position anyway)
    disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT + 73, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
        
    while(spin) {
        
        r1 = (rand() % 6);  //Fruit 1
        r2 = (rand() % 6);  //Fruit 2
        r3 = (rand() % 6);  //Fruit 3
        r4 = (rand() % 50); //Double Pay if 34,13,7
        r5 = (rand() % 2);  //LOSE! 1 or 2 dollars
        r6 = (rand() % 4) +1;  //cashpay on two of a kind. (+1 for no zero wanted)
        
        //Use if wanted to show the random number values
        serial.printf(" %d %d %d %d %d %d\r\n",r1,r2,r3,r4,r5,r6);
                        
        msg1 = bar[r1];
        msg2 = bar[r2];
        msg3 = bar[r3];
        
        char buf[27] = "";
        strncat(buf, msg1, sizeof buf);
        strncat(buf, msg2, sizeof buf - strlen(buf));
        strncat(buf, msg3, sizeof buf - strlen(buf));
    
        //Erase current text then draw new text centered
        //Fruit bars
        disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -58, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
        disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
            strlen(buf) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 58, buf, DisplayN18::GREEN, DisplayN18::BLACK);
        
        //Robot button is Not pressed
        if(pbRobot) {
            //YOUR PLAY
            disp.drawString(0, 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[27] = "";
            sprintf(buf, "YOUR PLAY - Cash %d ", cash);
            
            //Erase current text then draw new text centered
            //YOUR PLAY
            disp.drawString(0, 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);
            
            //Erase current text
            //WIN/LOSS Cash at hand
            disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -16, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
            
            spin = false;
            
            //Show fruit bars left to right one at a time
            //1 second interval
            for(int i =0; i <= 2; i++)
            {  
                msg1 = bar[r1];
                msg2 = bar[r2];
                msg3 = bar[r3];
                char buf[27] = "";
                
                if(i == 0) {
                    strncat(buf, msg1, sizeof buf);    
                }
                if(i == 1) {
                    strncat(buf, msg1, sizeof buf);
                    strncat(buf, msg2, sizeof buf - strlen(buf));  
                }
                if(i == 2) {
                    strncat(buf, msg1, sizeof buf);
                    strncat(buf, msg2, sizeof buf - strlen(buf));
                    strncat(buf, msg3, sizeof buf - strlen(buf));     
                }
                
                //Erase current text then draw new text centered
                //Fruit bars
                disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -58, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
                disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
                    strlen(buf) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 58, buf, DisplayN18::RED, DisplayN18::BLACK);
                    
                wait(1);
                
                //Erase current text then draw new text centered
                //Fruit bars
                disp.drawString(0, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT -58, szErase, DisplayN18::BLACK, DisplayN18::BLACK);
                disp.drawString(DisplayN18::WIDTH / 2 - (DisplayN18::CHAR_WIDTH + DisplayN18::CHAR_SPACING) *
                    strlen(buf) / 2, DisplayN18::HEIGHT-DisplayN18::CHAR_HEIGHT - 58, buf, DisplayN18::GREEN, DisplayN18::BLACK);                
            } //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 + r6;
                cashpay = r6;
                win = true;
            }
            if ((msg2 == bar[2]) && (msg3 == bar[2]))
            {
                cash = cash + r6;
                cashpay = r6;
                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 + r6;
                cashpay = r6;
                win = true;
            }
            if ((msg2 == bar[3]) && (msg3 == bar[3]))
            {
                cash = cash + r6;
                cashpay = r6;
                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 + r6;
                cashpay = r6;
                win = true;
            }
            if ((msg2 == bar[5]) && (msg3 == bar[5]))
            {
                cash = cash + r6;
                cashpay = r6;
                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 + r6;
                cashpay = r6;
                win = true;
            }
            if ((msg2 == bar[1]) && (msg3 == bar[1]))
            {
                cash = cash + r6;
                cashpay = r6;
                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 + r6;
                cashpay = r6;
                win = true;
            }
            if ((msg2 == bar[0]) && (msg3 == bar[0]))
            {
                cash = cash + r6;
                cashpay = r6;
                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 + 8;
                cashpay = 8;
                win = true;
            }
            //
            
            if(win)
            {                
                if(r4 == 35 || r4 == 13 || r4 == 7) {
                    sprintf(buf, "DOUBLE PAY %d  %d ", cashpay * 2, cash + cashpay);
                }
                else {
                    sprintf(buf, "WIN! Pay: %d Cash: %d ", cashpay, cash);
                }   
            }
            //
            
            if(!win)
            {
                if(r5 == 0) {
                    cash = cash - 1;
                    sprintf(buf, "LOSE! 1 dollar - %d ", cash);
                }
                else if(r5 == 1) {
                    cash = cash - 2;
                    sprintf(buf, "LOSE! 2 dollars - %d ", cash);
                }
                
            }
            //
            
            //Erase current text then draw new text centered
            //WIN/LOSS Cash at hand
            disp.drawString(0, 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 fruit bars
void pbSpaceship_hit_interrupt (void) {
    led1 = 1;
    led2 = 0;
    
    spin = true;
    reels();
}
//

int main() {
    
    //For read_u16() - See: http://developer.mbed.org/handbook/AnalogIn
    srand(ain.read_u16());
    
    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);
     
    disp.clear();
    wait(.5);
    
    //Splash
    char buf[27] = "";
    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(.1);
    } //while
}
//