user2757243
user2757243

Reputation: 81

Counting With C# Using Linq Or NOT

I know this is a newbie question but anyway i apreciate any help ...

Supose i have a Dice

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

And a Result class ...

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; 
   }
}

And a class Bet ...

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

Is there any very simple way (LINQ or not) to COUNT how many times a single Bet ( that can have one , two or three Dices )

appears in single Result that always have three dices ?

and if my List of Bets has more than one Bet , check if any Bet appears in a Result of three dices ?

For instance

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

Upvotes: 1

Views: 297

Answers (3)

Serj-Tm
Serj-Tm

Reputation: 16981

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];
}

Upvotes: 1

gunr2171
gunr2171

Reputation: 17510

I'm going under the assumption that you roll x number of dice and place y number of bets. You then want to compare if any or your bets was a number that was rolled.

First, you should change up how your Bet class is structured.

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

The reason is that one bet relates to one face value. You will then have a list of bets, like this:

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

Add these methods to your Result class:

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
}

The only thing you have to do now is create a List<Bet> object and pass that into the NumberOfCorrectBets method.

This should account for duplicate dice numbers / bet numbers. Meaning if you bet on a 3 and a 3 gets rolled 2 times, you will get 2 for an answer.

Upvotes: 0

artplastika
artplastika

Reputation: 1982

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));
}

Upvotes: 1

Related Questions