簡體   English   中英

為什么我的代碼無法計算貪婪游戲中的分數?

[英]Why is my code failing to calculate a score in a game of Greed?

問題

貪婪是一種用五個六面骰子玩的骰子游戲。 如果您選擇接受,您的任務就是根據這些規則進行投擲得分。 您將始終獲得一個包含五個六面骰子值的數組。


  • 三個 1 => 1000 點
  • 三個 6 => 600 分
  • 三個 5 => 500 分
  • 三個 4 => 400 分
  • 三個 3 => 300 分
  • 三個 2 => 200 分
  • 一1 => 100分
  • 一個 5 => 50 點

每個骰子只能計算一次。 例如,一個給定的“5”只能算作三元組的一部分(貢獻 500 分)或單獨算作 50 分,但不能同時算作兩者。


評分示例:

扔 | 分數

5 1 3 4 1 | 250:50(對於第 5 個)+ 2 * 100(對於第 1 個)

1 1 1 3 1 | 1100:1000(三個 1)+ 100(另外一個 1)

2 4 4 5 4 | 450:400(三個 4)+ 50(5)

我的代碼

int score(const int dice[5]) {   
    int ones=0;
    int twos=0;
    int threes=0;
    int fours=0;
    int fives=0;
    int sixes=0;
    int total = 0;
      
    //counting roll
    for (int i=0; i < 6; i++)
    {
        if (dice[i] == 1){
            ones++;
        }
        else if(dice[i] == 2){
            twos++;
        }
        else if(dice[i] == 3){
            threes++;
        }
        else if(dice[i] == 4){
            fours++;
        }
        else if(dice[i] == 5){
            fives++;
        }
        else if(dice[i] == 6){
            sixes++;
        }
    }
      
    //adding value to roll
    if(ones == 1){
        total = total + 100;
    }
    else if (ones == 2){
        total = total + 200;
    }
    else if (ones == 3){
        total = total + 1000;
    }
    else if(ones == 4){
        total = total + 1100;
    }
    else if (ones == 5){
        total = total + 1200;
    }
    else if(ones == 6){
        total = total + 2000;
    }
      
    if(twos == 3){
        total = total + 200;
    }
    else if (twos == 6){
        total = total + 400;
    }
      
    if(threes == 3){
        total = total + 300;
    }
    else if (threes == 6){
        total = total + 600;
    }
      
    if(fours == 3){
        total = total + 400;
    }
    else if(fours == 6){
        total = total + 800;
    }
      
    if(fives == 1){
        total = total + 50;
    }
    else if (fives == 2){
        total = total + 100;
    }
    else if(fives == 3){
        total = total + 500;
    }
    else if (fives == 4){
        total = total + 550;
    }
    else if (fives == 5){
        total = total + 600;
    }
    else if (fives == 6){
        total = total + 1000;
    }
      
    if(sixes == 3){
        total = total + 600;
    }
    else if(sixes == 6){
        total = total + 1200;
    }
      
    return total;
}

我的錯誤:

在此處輸入圖像描述


我的問題

我在這段代碼中缺少什么? 我以為我考慮了每一種可能性。

你的 function 原型

int score(const int dice[5])

以及您自己的描述,指出投擲了五 ( 5 ) 個骰子。

但是在循環中

for (int i=0; i < 6; i++)

您訪問只有五個元素 ( dice[5] ) 的數組的第六個元素。 通過這種方式,行為是未定義的,這意味着您可能會計算錯誤的投擲次數和錯誤的總數。

出於同樣的原因,檢查像

else if(ones == 6){
    total = total + 2000;
}

是無用的,因為您永遠不會擁有“六種”。


這是您的代碼的可能更正(和優化)版本:

int score(const int dice[5]) {
    int diceCounts[6] = { 0 };
    int total = 0;
  
    //counting roll
    //the numbers of throws of 'n' is contained into diceCounts[n-1]
    for (int i=0; i<5; i++) 
    {
        if(dice[i]>=0 && dice[i] <= 6)
      
      ++diceCounts[dice[i]-1];
    }
  
    //adding value to roll
    if(diceCounts[0] >= 3){ //1
        total += 1000
        diceCounts[0] -= 3;
    }
    total += diceCounts[0] * 100;
 
    for (int i=1; i<6; i++)
    {
        if(diceCounts[i] >= 3){ //2...6
            total += i*100
            diceCounts[i] -= 3;
        }
    }
    
    total += diceCounts[4] * 50;
  
  return total;  
}

建議:

  • 創建一個包含六個元素的數組,其中包含每個數字“n”的拋出次數(包含在數組的第n-1個元素中)
  • 對於每個數字,檢查它是否至少出現 3 次。 在這種情況下,相應地更新總數 * 單獨管理案例 1。 所有其他號碼提供x*100獎金
  • 將出現次數減去 3。 這對於案例 5 很有用,在這種情況下,您會將提醒乘以 50(單個 5 的獎勵)。

你沒有處理所有的情況......如果你有 4 個 3s 和一個其他的東西,它應該算作 300 但它算作 0 因為你沒有 4 個 3s 的情況(它應該與 3 個 3s 相同)

4s也一樣。

我可能會把它寫成一個循環,它在使用它時刪除計數,然后處理另一個循環中的任何剩余部分,直到循環找不到分數,但是如果你添加你錯過的案例並在索引 4 處停止迭代,這將起作用這是 5 元素數組的第 5 個元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM