So here's my code without file i/o.
Code:
/*
Written by: Shane Monks O'Byrne|14337326
One-Armed bandit slot machine
*/
/*header files*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/*slot struct*/
typedef struct{
int one;
int two;
int three;
}slot;
int main(void)
{
/*declaring variables*/
int credit = 10;
int bet;
int diff;
int cont;
int i;
slot play;
/*welcome and bet input*/
printf("Welcome to the ONE-ARMED-BANDIT slot machine\n");
while(credit >= 0)
{
printf("Your available credit is %d\n", credit);
printf("How much do you want to bet?\n");
while(scanf("%d", &bet) != 1)
{
while(getchar() != '\n')
{
printf("Please use numbers only\n");
break;
}
}
/*if bet is invalid, this will loop*/
if(bet < 2 || bet > credit)
{
while(bet < 2 || bet > credit)
{
printf("\nInvalid bet(must be greater than 2 credits and <= available credit), try again:\n");
scanf("%d", &bet);
}
}
credit -= bet; //taking bet away from credits
srand(time(0)); //seeding the random number generator against time
/*Giving each value in play a random number between 1 and 3*/
play.one = rand()%3+1;
play.two = rand()%3+1;
play.three = rand()%3+1;
printf("\n\n");
printf("|| ");
/*Below is printing what the RNG has assigned to each slot*/
if(play.one == 1)
printf("APPLE || ");
if(play.one == 2)
printf("ORANGE || ");
if(play.one == 3)
printf("PEAR || ");
if(play.two == 1)
printf("APPLE || ");
if(play.two == 2)
printf("ORANGE || ");
if(play.two == 3)
printf("PEAR || ");
if(play.three == 1)
printf("APPLE || ");
if(play.three == 2)
printf("ORANGE || ");
if(play.three == 3)
printf("PEAR || ");
printf("\n\n\n");
/*all the different variations*/
/*full house*/
if(play.one == play.two && play.two == play.three)
{
credit = credit + (2 * bet);
printf("** FULL HOUSE **\nYou won %d credits", bet);
}
/*empty house*/
else if(play.one != play.two && play.two != play.three && play.one != play.three)
{
printf("You lost %d credits", bet);
}
/*half house*/
else
{
credit = credit + (1.5 * bet);
printf("** HALF HOUSE **\nYou won %d credits", bet/2);
}
printf("\n\n\n");
printf("Current credit balance = %d\n", credit); //display remaining balance
printf("Play again? (y/n)\n");
cont = getch(); //continue?
/*if user wants to continue*/
if(cont != 'y' && cont != 'n')
{
while(cont != 'y' && cont != 'n')
{
printf("Please enter either 'y' or 'n':\n");
cont = getch();
}
}
if(cont == 'y')
{
if(credit <= 2)//insufficient credit ends game
{
if(credit == 1)
printf("Insufficient credit to continue, you only have 1 credit remaining");
if(credit == 0)
printf("Insufficient credit to continue, You have 0 credits remaining");
break;
}
}
/*if user does not want to continue*/
if(cont == 'n')
{
diff = credit - 10;
if(credit <= 10)
printf("End of game: total amount lost = %d credit(s)", diff);
if(credit > 10)
printf("End of game: total amount won = %d credit(s)", diff);
break;
}
}
return 0;