我正在製作一款足球遊戲。爲了獲得達陣,你必須獲得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
這是很多if/else if的 –
把這個問題減少到可以理解的地步。 – Hristo
我知道大聲笑.. – Donavon