1

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

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;
 }
4

1 回答 1

2

试试这个样式,需要填写数组Payout

struct Payout{
    int winningOdds, payOut;
};

int getPay(int winningOdds, int payOut)
{
    Payout payout[] = {{1,4000},{2,1000}....};
    int numElem = sizeof(payout)/sizeof(payout[0]);

    winningOdds = rand() % MAXIMUM_ODDS + MINIMUM_ODDS;
    for(int i=0;i<numElem ;i++){
        if(winningOdds <=payout[i].winningOdds){
            payOut = payout[i].payout;
            break;
        }
    }
    printf("Coins won - %d\n", payOut);

}
于 2013-02-04T07:19:26.763 回答