我已经定义了一种方法来计算获胜几率并返回支付给玩家的金额。我最初写这个是完成这项任务的最明显的方法,但我真的很想知道重构它以最小化计算机正在执行的工作量的最佳方法。这是我的功能:
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;
}