I'm quite happy with my code so far but I often make efficiency errors that I don't see when writing code. Have I missed anything?
#include <stdio.h>
#include <unistd.h>
int main ()
{
char end = 'N';
int credit = 5, bet = 0, userInput = 0, cont = 0, victory;
do{
while (cont == 0){
// Introduction of credit system to user
printf("\e[1;1H\e[2J");
printf("Your current credits are %d.\n", credit);
printf("How many credits would you like to bet?\n");
scanf("%d", &userInput);
printf("\e[1;1H\e[2J");
// Checking for valid credit input and adjusting credit/bet value according to input
if (userInput <= credit && userInput >= 1){
bet = userInput;
credit = credit - bet;
cont = 1;
printf("You have bet %d credits. Good luck!\n", bet);
sleep(2);
break;
}
if (userInput == 0){
printf("Invalid, bet must be a minimum of 1 credit.\n");
sleep(2);
cont = 0;
}
else{
printf("Invalid, input exceeded your credit value of %d.\n", credit);
sleep(2);
cont = 0;
}
// End of check
}
// Introduction countdown to execute after valid bet is placed
printf("\e[1;1H\e[2J");
printf("Ready?\n");
sleep(1);
printf("\e[1;1H\e[2J");
printf("3...\n");
sleep(1);
printf("\e[1;1H\e[2J");
printf("2...\n");
sleep(1);
printf("\e[1;1H\e[2J");
printf("1...\n");
sleep(1);
printf("\e[1;1H\e[2J");
printf("Spin!\n");
printf("\e[1;H\e[2J");
// Declaring var names for reel and loop
int firstReel, secondReel, thirdReel, loop = 0;
// Loop to print cycle reel 10 times
while(loop < 10){
sleep(1);
// Generating three random numbers
srand(time(NULL));
int rndOne = rand () %4;
int rndTwo = rand () %4;
int rndThree = rand () %4;
// Assigning random numbers to clearer var names
firstReel = rndOne;
secondReel = rndTwo;
thirdReel = rndThree;
// Switch statements for each reel
switch(firstReel){
case 0:
printf("Bell ");
break;
case 1:
printf("Cherry ");
break;
case 2:
printf("Orange ");
break;
case 3:
printf("Horseshoe ");
break;
}
switch(secondReel){
case 0:
printf("Bell ");
break;
case 1:
printf("Cherry ");
break;
case 2:
printf("Orange ");
break;
case 3:
printf("Horseshoe ");
break;
}
switch(thirdReel){
case 0:
printf("Bell\n");
break;
case 1:
printf("Cherry\n");
break;
case 2:
printf("Orange\n");
break;
case 3:
printf("Horseshoe\n");
break;
}
// End of switch statements for each reel
loop++;
// Clears the previous printed combination
if (loop < 10)
printf("\e[1;1H\e[2J");
else
{
break;
}
}
// Win/lose conditions
if (loop > 10 && firstReel == secondReel || secondReel == thirdReel){
printf("Congratulations! You win! ");
victory = 1;
}
else
{
printf("Sorry, you lose. ");
victory = 0;
}
// Payout calculations according to victory value
if (victory == 1){
credit = credit + (bet * 2);
printf("Your credit is now %d.\n", credit);
}
else
{
if (credit == 0){
printf("Your credit is now 0. Game over.\n");
return 0;
}
else
{
printf("Your credit is now %d.\n", credit);
}
}
// Option for user to terminate the game
printf("Would you like to cash out? (Y/N)\n");
// Takes user input and cancels if Y
scanf(" %c", &end);
if (end == 'Y'){
printf("\e[1;1H\e[2J");
printf("Your final credit score is: %d.\n", credit);
sleep(3);
return 0;
}
else
{
end = 'N';
}
cont = 0;
} while (end == 'N');
return 0;
}