簡體   English   中英

使用Linq或不使用C#進行計數

[英]Counting With C# Using Linq Or NOT

我知道這是一個新手問題,但無論如何,我非常感謝您的幫助...

假設我有一個骰子

public class Dice
{
   public int FaceValue { get; set; }
   public Dice(int faceValue)
   {
      this.FaceValue = faceValue; 
   }
}

還有一個Result類...

public class Result
{

   public Dice D1 { get; set; }
   public Dice D2 { get; set; }
   public Dice D3 { get; set; }

   // Always has three dices ...
   public Result(Dice d1,Dice d2,Dice d3)
   {
      D1 = d1;
      D2 = d2;
      D3 = d3; 
   }
}

還有一個班級下注...

public class Bet
{
   // A bet could have one , two , or three dices ....
   public List<Dice> Dices = new List<Dice>();
}

是否有任何非常簡單的方法(是否為LINQ)來計算單個投注(可以有一個,兩個或三個骰子)的次數?

出現在始終有三個骰子的單個結果中?

並且如果我的投注清單有一個以上的投注,請檢查在三個骰子的結果中是否出現任何投注?

例如

Result.D1 = new Dice(1);
Result.D2 = new Dice(4);
Result.D3 = new Dice(1);

{ { new Dice(1), new Dice(4) } } appears 1 time ===> 1

{ { new Dice(1) } } appears 2 times ====> 2

{ { new Dice(4) , new Dice(1) , new Dice(1) } } appears 1 time ====> 1

{ { new Dice(5) , new Dice(2) , new Dice(3) } } doesn't appear ====> 0

{ { new Dice(1) , new Dice(6) , new Dice(6) },
{ new Dice(4) , new Dice(4) , new Dice(4) },
{ new Dice(1) , new Dice(2) , new Dice(3) },
{ new Dice(1) , new Dice(5) , new Dice(5) },
{ new Dice(1) , new Dice(1) , new Dice(4) },
{ new Dice(3) , new Dice(3) , new Dice(3) } } has one bet that is equal so ========> 1
public class Result
{

   public Dice D1 { get; set; }
   public Dice D2 { get; set; }
   public Dice D3 { get; set; }

   // Always has three dices ...
   public Result(Dice d1,Dice d2,Dice d3)
   {
      D1 = d1;
      D2 = d2;
      D3 = d3; 
   }

   public bool Match(IEnumerable<Dice> dice)
   {
        return ...; // Your comparison logic here
   }
}

var bets = new List<Bet>();

foreach(var bet in bets)
{
    var matchCount = bet.Count(x => Result.Match(x.Dices));
}
var dice = ShortForm(new[]{result.D1, result.D2, result.D3});
var betGoodCount = bets.Count(bet => BetInDice(bet, dice));


Dictionary<int, int> ShortForm(IEnumerable<Dice> dice)
{
   return dice
      .GroupBy(die => die.FaceValue)
      .ToDictionary(group => group.Key, group => group.Count);
}
bool BetInDice(Bet bet, Dictionary<int, int> dice)
{
  return ShortForm(bet.Dice)
    .All(pair => dice.ContainsKey(pair.Key) && pair.Value <= dice[pair.Key];
}

我假設您擲出x個骰子數和y個賭注。 然后,您想比較是否有一個或您的下注是一個已下注的數字。

首先,您應該更改Bet類的結構。

public class Bet
{
    public int FaceValue { get; set; }
}

原因是一個bet與一個face value 然后,您將有一個下注列表,如下所示:

List<Bet> bets = new List<Bet>()
{
    new Bet() { FaceValue = 2 },
    new Bet() { FaceValue = 4 },
    //etc
};

將這些方法添加到您的Result類:

private IEnumerable<int> CorrectBets(List<Dice> dice, List<Bet> bets)
{
    //use an linq join on their face values
    return from die in dice
           join bet in bets on die.FaceValue equals bet.FaceValue
           select die.FaceValue;
}

public int NumberOfCorrectBets(List<Bet> bets)
{
    var dice = new List<Dice>() { D1, D2, D3 };
    return CorrectBets(dice, bets).Count(); //this actually gets the count
}

現在唯一要做的就是創建一個List<Bet>對象,並將其傳遞給NumberOfCorrectBets方法。

這應該說明重復的骰子號碼/下注號碼。 意思是,如果您下注33擲出2次,則答案將為2。

暫無
暫無

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

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