1つの方法は、の計数値を含む6要素配列を、その面がいくつあるかを格納することです。 5つのダイスをループし、適切な顔の合計数を増やします。
var rolls = new List<Roll>();
// run as many rolls as you want. e.g.:
rolls.Add(new Roll(5));
var threeOfAKindRolls = rolls.Where(r => r.HasThreeOfAKind());
public class Roll
{
public Roll(int diceCount)
{
// Do your random generation here for the number of dice
DiceResults = new int[0]; // your results.
ResultCounts = new int[6]; // assuming 6 sided die
foreach (var diceResult in DiceResults)
{
ResultCounts[diceResult]++;
}
}
public int[] DiceResults { get; private set; }
public int[] ResultCounts { get; private set; }
public bool HasThreeOfAKind()
{
return ResultCounts.Any(count => count >= 3);
}
}
あなたは結果が結果に他のテストを実行するためにカウント必要がない場合は、このコードが多少短縮することができます。
public Roll(int diceCount)
{
// Do your random generation here for the number of dice
DiceResults = new int[0]; // your results.
}
public bool HasThreeOfAKind()
{
ResultCounts = new int[6]; // assuming 6 sided die
foreach (var diceResult in DiceResults)
{
// Increment and shortcut if the previous value was 2
if((ResultCounts[diceResult]++) == 2) return true;
}
return false;
}
コードを投稿できますか? – Taryn
あなたの宿題をあなたに依頼するのではなく、あなたのコードを現状のまま投稿してください。 –
これまであなたが幸せではないことを試したことはありますか?宿題に関する質問については、これまでに試したことを投稿し、それがうまくいかないように説明し、具体的な質問をする必要があります。あなたの現在のコードを投稿し、そのコードの実際の部分について質問してください。 (そして、勉強のために、あなたは '宿題'タグを含めるべきです)。あなたがコードを書いて欲しいとは思わないのは良いことです。ありがとう。 :) –