I just want to know how efficient is my code and is there any better way to do the same program in C? Can the if statements are replaced with something else or can I merge the two loops together to get roll1 and roll2 within a single loop?
Also if there is any more tricks or things that I could have added to the program to make it more interesting. Also after reading my code, what mistake you think I am making while writing the code or what can I improve to write more efficient programs, things I should keep in mind.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <windows.h>
#include <time.h>
int main()
{
srand(time(NULL));
int i, roll1=0, roll2=0, NumberOfRolls, RandomNUM1[50], RandomNUM2[50];
char nextGuess;
puts("Welcome to the Dice Roll Game");
puts("How many times do you want to roll a dice?");
scanf("%d", &NumberOfRolls);
for( i=0; i<NumberOfRolls; i++ ) {
RandomNUM1[i] = ( rand()%6 ) + 1;
roll1 += RandomNUM1[i];
}
printf("\nYou Got %d in your first roll!\n", roll1);
Sleep(3000);
printf("\nLet's see if you can guess the value of next roll.\n");
printf("Will it be Higher/Lower or the same? (press H/L/S)\n");
scanf(" %c", &nextGuess);
nextGuess = toupper(nextGuess);
for( i=0; i<NumberOfRolls; i++ ) {
RandomNUM2[i] = ( rand()%6 ) + 1;
roll2 += RandomNUM2[i];
}
if(nextGuess=='H'){
if(roll1<roll2){
printf("\nYou are such a player, you guessed it right! It's %d", roll2);
}
else if(roll1>roll2){
printf("\nUh-Oh! Bad Luck! First roll was higher, It's %d", roll2);
}
else if(roll1==roll2){
printf("\nUh-Oh! Bad Luck! Both the rolls are same, It's %d", roll2);
}
}
if(nextGuess=='L'){
if(roll1>roll2){
printf("\nYou are such a player, you guessed it right! It's %d", roll2);
}
else if(roll1<roll2){
printf("\nUh-Oh! Bad Luck! First roll was lower, It's %d", roll2);
}
else if(roll1==roll2){
printf("\nUh-Oh! Bad Luck! Both the rolls are same, It's %d", roll2);
}
}
if(nextGuess=='S'){
if(roll1==roll2){
printf("\nYou are such a player, you guessed it right! It's %d", roll2);
}
else if(roll1>roll2){
printf("\nUh-Oh! Bad Luck! First roll was higher, It's %d", roll2);
}
else if(roll1<roll2){
printf("\nUh-Oh! Bad Luck! Second roll is higher, It's %d", roll2);
}
}
if( (nextGuess!='H') && (nextGuess!='L') && (nextGuess!='S') ){
printf("\nYour input is invalid. Please enter H/L/S");
}
return 0;
}