Code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
#include "SDL.h"
#include "SDL_ttf.h"
using namespace std;
typedef unsigned short int USHORT;
unsigned short int tokens = 24;
bool quitSlot = false;
SDL_Event event;
SDL_Surface* screen = NULL;
TTF_Font *arialFont = NULL;
SDL_Color textColourBlack = {0, 0, 0};
void applySurface( int xCoord, int yCoord, SDL_Surface* source, SDL_Surface* destination )
{
//Make a temporary rectangle to hold the offsets
SDL_Rect offset;
//Give the offsets to the rectangle
offset.x = xCoord;
offset.y = yCoord;
//Blit the surface
SDL_BlitSurface( source, NULL, destination, &offset );
}
void cleanUpQuit()
{
TTF_Quit();
SDL_Quit();
}
void welcome()
{
SDL_Surface* welcomeLineOne = NULL;
SDL_Surface* welcomeLineTwo = NULL;
SDL_Surface* welcomeLineThree = NULL;
SDL_Surface* welcomeLineFour = NULL;
welcomeLineOne = TTF_RenderText_Solid(arialFont, "Welcome to the most amazing text-based Slot Machine!", textColourBlack);
welcomeLineTwo = TTF_RenderText_Solid(arialFont, "Our fine Slot Machines may or maynot grant you wishes!", textColourBlack);
welcomeLineThree = TTF_RenderText_Solid(arialFont, "Anyways, You have tokens.", textColourBlack);
welcomeLineFour = TTF_RenderText_Solid (arialFont, "Soooo, have fun with these few tokens as you will have none left very, very, very SOON!", textColourBlack);
applySurface ( 0, 50, welcomeLineOne, screen);
applySurface ( 0, 70, welcomeLineTwo, screen);
applySurface ( 0, 90, welcomeLineThree, screen);
applySurface ( 0, 110, welcomeLineFour, screen);
SDL_Flip ( screen);
SDL_FreeSurface ( welcomeLineOne );
SDL_FreeSurface ( welcomeLineTwo );
SDL_FreeSurface ( welcomeLineThree );
SDL_FreeSurface ( welcomeLineFour );
TTF_CloseFont ( arialFont );
}
void outcome(int firstNumber, int secondNumber, int thirdNumber)
{
const USHORT tokensThree = 3;
const USHORT tokensTwo = 2;
if(firstNumber == secondNumber && secondNumber == thirdNumber && firstNumber == thirdNumber)
{
tokens += tokensThree;
cout << endl << "Congrats! You won 3 Tokens!" << endl;
cout << "You have " << tokens << " tokens." << endl;
return;
}
if(firstNumber == secondNumber || secondNumber == thirdNumber || firstNumber == thirdNumber)
{
tokens += tokensTwo;
cout << endl << endl << "Congrats! You won 2 Tokens!" << endl;
cout << "You have " << tokens << " tokens." << endl;
}
else
{
tokens -= tokensThree;
cout << endl << endl << "Sorry! You lost 3 Tokens." << endl;
cout << "You have " << tokens << " tokens." << endl;
}
return;
}
void lever()
{
SDL_Surface* firstImageNumber = NULL;
SDL_Surface* secondImageNumber = NULL;
SDL_Surface* thirdImageNumber = NULL;
SDL_Surface* fourthImageNumber = NULL;
SDL_Surface* fifthImageNumber = NULL;
SDL_Surface* sixthImageNumber = NULL;
firstImageNumber = SDL_LoadBMP ( "Images/1.bmp" );
secondImageNumber = SDL_LoadBMP ( "Images/2.bmp" );
thirdImageNumber = SDL_LoadBMP ( "Images/3.bmp" );
fourthImageNumber = SDL_LoadBMP ( "Images/4.bmp" );
fifthImageNumber = SDL_LoadBMP ( "Images/5.bmp" );
sixthImageNumber = SDL_LoadBMP ( "Images/6.bmp" );
const USHORT highRandom = 6;
string imputYes;
string playYes = ("yes");
string playY = ("y");
USHORT randomNumber;
USHORT randomNumberTwo ;
USHORT randomNumberThree ;
time_t clockTime;
time(&clockTime);
srand((unsigned int) clockTime);
randomNumber = rand() % highRandom + 1;
randomNumberTwo = rand() % highRandom + 1;
randomNumberThree = rand() % highRandom + 1;
cout << endl << "Do you want to pull the Lever?" << endl;
cin >> imputYes;
if (imputYes == playY || imputYes == playYes)
{
if(randomNumber == 1)
{
//Apply image to screen
applySurface ( 180, 100, firstImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( firstImageNumber );
}
if(randomNumber == 2)
{
//Apply image to screen
applySurface ( 180, 100, secondImageNumber, screen);;
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( secondImageNumber );
}
if(randomNumber == 3)
{
//Apply image to screen
applySurface ( 180, 100, thirdImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( thirdImageNumber );
}
if(randomNumber == 4)
{
//Apply image to screen
applySurface ( 180, 100, fourthImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( fourthImageNumber );
}
if(randomNumber == 5)
{
//Apply image to screen
applySurface ( 180, 100, fifthImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( fifthImageNumber );
}
if(randomNumber == 6)
{
//Apply image to screen
applySurface ( 180, 100, sixthImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( sixthImageNumber );
}
if(randomNumberTwo == 1)
{
//Apply image to screen
applySurface ( 180, 140, firstImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( firstImageNumber );
}
if(randomNumberTwo == 2)
{
//Apply image to screen
applySurface ( 180, 140, secondImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( secondImageNumber );
}
if(randomNumberTwo == 3)
{
//Apply image to screen
applySurface ( 180, 140, thirdImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( thirdImageNumber );
}
if(randomNumberTwo == 4)
{
//Apply image to screen
applySurface ( 180, 140, fourthImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( fourthImageNumber );
}
if(randomNumberTwo == 5)
{
//Apply image to screen
applySurface ( 180, 140, fifthImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( fifthImageNumber );
}
if(randomNumberTwo == 6)
{
//Apply image to screen
applySurface ( 180, 140, sixthImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( sixthImageNumber );
}
if(randomNumberThree == 1)
{
//Apply image to screen
applySurface ( 180, 180, firstImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( firstImageNumber );
}
if(randomNumberThree == 2)
{
//Apply image to screen
applySurface ( 180, 180, secondImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( secondImageNumber );
}
if(randomNumberThree == 3)
{
//Apply image to screen
applySurface ( 180, 180, thirdImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( thirdImageNumber );
}
if(randomNumberThree == 4)
{
//Apply image to screen
applySurface ( 180, 180, fourthImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( fourthImageNumber );
}
if(randomNumberThree == 5)
{
//Apply image to screen
applySurface ( 180, 180, fifthImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( fifthImageNumber );
}
if(randomNumberThree == 6)
{
//Apply image to screen
applySurface ( 180, 180, sixthImageNumber, screen);
//Update Screen
SDL_Flip( screen );
//Free the loaded image
SDL_FreeSurface( sixthImageNumber );
}
outcome(randomNumber, randomNumberTwo, randomNumberThree);
}
else
{
cout << "Goodbye!" << endl;
exit(0);
}
}
int main( int argc, char* args[] )
{
SDL_Surface* background = NULL;
SDL_Init( SDL_INIT_EVERYTHING );
TTF_Init();
arialFont = TTF_OpenFont ( "Fonts/arial.ttf", 14);
const USHORT screenHeight = 480;
const USHORT screenWidth = 640;
const USHORT screenBpp = 32;
screen = SDL_SetVideoMode ( screenWidth, screenHeight, screenBpp, SDL_SWSURFACE );
SDL_WM_SetCaption ( "Slot Machine", NULL);
background = SDL_LoadBMP ( "Images/BG.bmp" );
applySurface ( 0, 0, background, screen );
SDL_Flip ( screen );
SDL_FreeSurface ( background );
welcome();
while ( quitSlot == false)
{
while ( SDL_PollEvent (&event));
{
if(event.type == SDL_QUIT )
{
quitSlot = true;
}
}
}
cleanUpQuit();
return 0;
}