HI, I am working on a simple class to combine items of any type... this is for a poker game, this is how it looks:
public static List<List<T>> combinar<T>(List<T> items, int take)
{
List<List<T>> combs = new List<List<T>>();
var stuff = permutar<T>(items, take);
var all = from s in stuff
select new Tuple<List<T>, string>(s, String.Join("", s.OrderBy(c => c).Select(c => c.ToString())));
var strs = all.Select(s => s.Item2).Distinct();
foreach (var str in strs)
{
combs.Add(all.First(a => a.Item2 == str).Item1);
}
return combs;
}
public static List<List<T>> permutar<T>(List<T> list, int take)
{
List<List<T>> combs = new List<List<T>>();
foreach (var item in list)
{
var newlist = list.Where(i => !i.Equals(item)).ToList();
var returnlist = take <= 1 ? new List<List<T>> { new List<T>() } : permutar(newlist, take - 1);
foreach (var l in returnlist)
{
l.Add(item);
}
combs.AddRange(returnlist);
}
return combs;
}
so permutation works perfect.. but I am having some trouble with combination, when T is Card it takes hell lots of time to complete... so my question is how to select distinct lists form the result of permutation???
this is the Card Class:
public class Card : IComparable
{
Suite _Suite;
public Suite Suite
{
get { return _Suite; }
set { _Suite = value; }
}
Grade _Grade;
public Grade Grade
{
get { return _Grade; }
set { _Grade = value; }
}
string _symbol;
public string Symbol
{
//stuff
}
public PictureBox Picture
{
//stuff
}
public override string ToString()
{
return _Grade.ToString() + " " + _Suite.ToString();
}
public int CompareTo(object obj)
{
Card card = (Card)obj;
return card.Grade > this.Grade ? -1 : card.Grade < this.Grade ? 1 : 0;
}
}