2015-09-01 79 views
0

我正在製作一款足球遊戲。爲了獲得達陣,你必須獲得100碼。我有public static int yardsLeft = 100;。它經歷一個循環,獲得每個通過的許多碼。一旦yardsLeft = 0,它將退出。我試圖弄清楚如何讓最後獲得的場地(達陣)達到100碼。
c# - 計算 - RNG遊戲

我有這樣的:

if (Program.yardsLeft >= 80) 
{ 
    if (Program.chance >= 1 && Program.chance <= 5) 
     Program.gained = Program.r.Next(80, (100 + 1)); 

    else if (Program.chance >= 6 && Program.chance <= 12) 
     Program.gained = Program.r.Next(60, (79 + 1)); 

    else if (Program.chance >= 13 && Program.chance <= 24) 
     Program.gained = Program.r.Next(20, (59 + 1)); 

    else if (Program.chance >= 25 && Program.chance <= 100) 
     Program.gained = Program.r.Next(1, (19 + 1)); 
} 
else if (Program.yardsLeft <= 79 && Program.yardsLeft >= 60) 
{ 
    if (Program.chance >= 1 && Program.chance <= 10) 
     Program.gained = Program.r.Next(60, 80); 

    else if (Program.chance >= 11 && Program.chance <= 30) 
     Program.gained = Program.r.Next(20, 60); 

    else if (Program.chance >= 31 && Program.chance <= 100) 
     Program.gained = Program.r.Next(1, 20); 
} 
else if (Program.yardsLeft <= 59 && Program.yardsLeft >= 40) 
{ 
    if (Program.chance >= 1 && Program.chance <= 10) 
     Program.gained = Program.r.Next(40, 60); 

    else if (Program.chance >= 11 && Program.chance <= 30) 
     Program.gained = Program.r.Next(20, 40); 

    else if (Program.chance >= 31 && Program.chance <= 100) 
     Program.gained = Program.r.Next(1, 20); 
} 
else if (Program.yardsLeft <= 39 && Program.yardsLeft >= 20) 
{ 
    if (Program.chance >= 1 && Program.chance <= 10) 
     Program.gained = Program.r.Next(30, 40); 

    else if (Program.chance >= 11 && Program.chance <= 30) 
     Program.gained = Program.r.Next(10, 30); 

    else if (Program.chance >= 31 && Program.chance <= 100) 
     Program.gained = Program.r.Next(1, 10); 
} 
else if (Program.yardsLeft <= 19) 
{ 
    if (Program.chance >= 1 && Program.chance <= 10) 
     Program.gained = Program.r.Next(15, 20); 

    if (Program.chance >= 11 && Program.chance <= 30) 
     Program.gained = Program.r.Next(10, 15); 

    if (Program.chance >= 31 && Program.chance <= 100) 
     Program.gained = Program.r.Next(1, 10); 
} 

首先它會檢查yardsLeft超過80,如果是,它必須獲得這麼多碼(100)的機會,但如果第一次通過,它不會超過20碼(繼續下一個檢查else if (Program.yardsLeft <= 79 && Program.yardsLeft >= 60)它將會直接回到else if (Program.yardsLeft <= 79 && Program.yardsLeft >= 60)並再次添加一些導致達到100的東西,意味着在最後,結果將通過100。

示例

if (yardsLeft is 80 or more) 
    { 
     // generate number somewhere through 60-80 (not ever exceed 100) 
     // generate number somehwere between 20-60 (not ever exceeding 100 
     // generator number somewehere between 1-20 (not ever exceeding 100) 
    } 
    else if (yardsLeft is between 60-20) 
    { 
     // generate number somewhere through 40-60 (not ever exceed 100) 
     // generate number somehwere between 20-40 (not ever exceeding 100 
     // generator number somewehere between 1-20 (not ever exceeding 100) 
    } 

最終結果CAN NOT超過100.這就是我想說的。每次循環的時間,我希望它產生一些數字,在年底,將不會超過100

+0

這是很多if/else if的 –

+2

把這個問題減少到可以理解的地步。 – Hristo

+0

我知道大聲笑.. – Donavon

回答

1

一個簡單的解決辦法是隻需將這個塊代碼後剩下:

Program.gained = Math.Min(Program.gained, Program.yardsLeft); 

這意味着,如果所獲得的碼比去0所需的碼數大,它將被設置爲左側的碼數。所以,當你以後(大概)由Program.gained遞減Program.yardsLeft,它不會小於0


基礎知識:Math.Min返回最小你給它的參數。此聲明將Program.gained設置爲基於較早代碼中給出的值的新值。這會將Program.gained的值「夾」到yardsLeft以下。這裏有一個簡單的用法:

int yardsYouCanRun = 112; 
int yardsYouTraveled = Math.Min(yardsYouCanRun, 100); 
// yardsYouTraveled is Math.Min(112, 100), which is 100. 

int yardsICanRun = 15; 
int yardsITraveled = Math.Min(yardsICanRun, 100); 
// yardsITraveled is Math.Min(15, 100), which is 15. 

這代表您追蹤你可以從零運行,但希望將其限制在100,因爲這是你所能運行的最大距離碼數的情況。兩者中的最小值是距離。正如你所看到的,你可以跑全程,但是我只能跑15碼,所以你得到了100碼的達陣,但我只能在15碼線。這是將「yardsXCanRun」的值「夾緊」在100碼以下。

這與前面的代碼行相同,但是您不需要運行100碼,您只能運行Program.yardsLeft

+0

謝謝。這確實奏效,但是你能否解釋這是如何工作的?我儘可能地學習,並且我非常致力於學習。 – Donavon

+1

@Donavon編輯擴大,希望它有幫助。 – 31eee384

0

我覺得你是想是這樣的:

if (Program.yardsLeft >= 80) 
    { 
     if (Program.chance >= 1 && Program.chance <= 5) 
      Program.gained = Program.r.Next(80, (100 + 1)); 

     else if (Program.chance >= 6 && Program.chance <= 12) 
      Program.gained = Program.r.Next(60, (79 + 1)); 

     else if (Program.chance >= 13 && Program.chance <= 24) 
      Program.gained = Program.r.Next(20, (59 + 1)); 

     else if (Program.chance >= 25 && Program.chance <= 100) 
      Program.gained = Program.r.Next(1, (19 + 1)); 
    } 
    else if (Program.yardsLeft <= 79 && Program.yardsLeft >= 60) 
    { 
     if (Program.chance >= 1 && Program.chance <= 10) 
      Program.gained = Program.r.Next(60, 80); 

     else if (Program.chance >= 11 && Program.chance <= 30) 
      Program.gained = Program.r.Next(20, 60); 

     else if (Program.chance >= 31 && Program.chance <= 100) 
      Program.gained = Program.r.Next(1, 20); 
    } 
    else if (Program.yardsLeft <= 59 && Program.yardsLeft >= 40) 
    { 
     if (Program.chance >= 1 && Program.chance <= 10) 
      Program.gained = Program.r.Next(40, 60); 

     else if (Program.chance >= 11 && Program.chance <= 30) 
      Program.gained = Program.r.Next(20, 40); 

     else if (Program.chance >= 31 && Program.chance <= 100) 
      Program.gained = Program.r.Next(1, 20); 
    } 
    else if (Program.yardsLeft <= 39 && Program.yardsLeft >= 20) 
    { 
     if (Program.chance >= 1 && Program.chance <= 10) 
      Program.gained = Program.r.Next(30, 40); 

     else if (Program.chance >= 11 && Program.chance <= 30) 
      Program.gained = Program.r.Next(10, 30); 

     else if (Program.chance >= 31 && Program.chance <= 100) 
      Program.gained = Program.r.Next(1, 10); 
    } 
    else if (Program.yardsLeft <= 19 && Program.yardsLeft !>= 100) 
    { 
     if (Program.chance >= 1 && Program.chance <= 10) 
     Program.gained = Program.r.Next(15, 20); 

    if (Program.chance >= 11 && Program.chance <= 30) 
     Program.gained = Program.r.Next(10, 15); 

    if (Program.chance >= 31 && Program.chance <= 100) 
     Program.gained = Program.r.Next(1, 10); 
    } 
    else 
    {return;}//return the value if over 100 

只要你沒有把一個參數檢查最後剩下的碼數是否超過100 elseif

另一種做你在這裏的方法是通過使用.yardsLeft使得隨機動態只允許你達到100,而不會更多。

if (Program.chance >= 31 && Program.chance <= 100) 
     Program.gained = Program.r.Next(1, program.yardsLeft);