Skip to content

Instantly share code, notes, and snippets.

@nsylvestre
Last active March 12, 2025 14:15
Show Gist options
  • Select an option

  • Save nsylvestre/c8127b15424553b0fcc8d09dda1b7df1 to your computer and use it in GitHub Desktop.

Select an option

Save nsylvestre/c8127b15424553b0fcc8d09dda1b7df1 to your computer and use it in GitHub Desktop.
class Die {
const int NumberOfSides = 6;
public int Id { get; set; }
public int Value { get; set; }
public Die(int id) {
Id = id;
Value = 0;
}
public void Roll(Random rnd) {
Value = rnd.Next(1, NumberOfSides + 1);
}
public static Die operator +(Die d1, Die d2) {
Die d = new Die(0);
d.Value = d1.Value + d2.Value;
return d;
}
public override string ToString() {
return $"{Value} ";
}
}
var setOfDie = from die in Dice
group die by die.Value into g
select new {
Value = g.Key,
Count = g.Count(),
Score = g.Key * g.Count()
} into g2
orderby g2.Value
select g2;
//Alternative
var setOfDie2 = Dice.GroupBy(die => die.Value).Select(die => {
return new {
Value = die.Key,
Count = die.Count(),
Score = die.Key * die.Count()
};
}).OrderBy(Die => Die.Value);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment