I’ve defined a method that calculates the odds of winning and returns the amount payed to the player. I originally had written this the most obvious way of accomplishing this task, but I really would like to know the best way of refactoring this to minimize the amount of work the computer is performing. Here’s my function:
int getPay(int winningOdds, int payOut)
{
winningOdds = rand() % MAXIMUM_ODDS + MINIMUM_ODDS;
if(winningOdds == 1)
{
payOut = 4000;
printf("Jackpot! - %d\n", payOut);
}
else if(winningOdds <= 2)
{
payOut = 1000;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 3)
{
payOut = 500;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 40)
{
payOut = 100;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 50)
{
payOut = 50;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 75)
{
payOut = 25;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 1000)
{
payOut = 10;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 3000)
{
payOut = 5;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 5000)
{
payOut = 3;
printf("Coins won - %d\n", payOut);
}
else if(winningOdds <= 10000)
{
payOut = 1;
printf("Coins won - %d\n", payOut);
}
return payOut;
}
Try this style, you need to fill in the array of
Payout