Slot Frenzy: A Chance to Win. My slot machine project.
Gagan G Saralaya
1 min read
Wondered how a slot machine works. The glittering and sparkling lights of a slot machine, and the sounds it makes which makes you helpless and addicted to the slot machine. Here is a simple slot machine project I made when I was bored out of my mind in my lab class. But this does not have any of the flashy light and colors and is less addicting. It generates random numbers from one to 3 and has about one in 27 chances to win.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int tries = 0;
int points = 0;
srand(time(NULL)); // seed for random number generation
while (1) {
int num1 = rand() % 3; // generate a random number between 0 and 3
int num2 = rand() % 3;
int num3 = rand() % 3;
printf("Slot machine result: %02d %02d %02d\n", num1, num2, num3);
if (num1 == 0 && num2 == 0 && num3 == 0) {
printf("Congratulations! You won!\n");
points++;
}
tries++;
char response;
printf("Press W to play again or 'q' to quit: ");
scanf(" %c", &response);
if (response == 'q') {
break;
}
}
printf("You played a total of %d times and won %d points.\n", tries, points);
return 0;
}
Output:
0
Subscribe to my newsletter
Read articles from Gagan G Saralaya directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by