public List <CardData> GetStraight(List <CardData> gameHand) { //TODO: Strongest if (gameHand != null) { gameHand = gameHand.SortBySuits(); gameHand = gameHand.SortByRank(); } var groupedList = hand.cardDatas.GroupBy(c => c.rank).Select(group => new { key = group.Key, list = group.ToList() }); groupedList = groupedList.OrderBy(g => g.key); List <List <CardData> > rankList = new List <List <CardData> >(); //converting grouped ienumerable to generic list because crash (???) foreach (var group in groupedList) { List <CardData> list = new List <CardData>(group.list); list = list.SortBySuits(); list = list.SortByRank(); rankList.Add(list); } List <CardData> tempHand = new List <CardData>(); int w = 0; while (w < rankList.Count - 5) //while count of ranks is enough to make a straight (5) { if (tempHand.Count == 0) //making sure { int x = 0; int invalidIndex = 0; while (x < 5) { if (x > 0) { if (rankList[w + x][0].rank != rankList[w + x - 1][0].rank + 1) { //break out of loop and save index (to w) so it will start from there invalidIndex = w + x; break; } } tempHand.Add(rankList[w + x][0]); x++; } if (invalidIndex > 0) //not a straight, go to next loop and change current index to the last invalid one { w = invalidIndex; continue; } if (tempHand.IsStraight()) { if (gameHand == null || HandEvaluator.CompareStraight(tempHand, gameHand)) { return(tempHand); } else if (tempHand[4].rank == gameHand[4].rank) //if straight has same head { //try changing head if straight lose int y = 1; int yMax = rankList[w + x - 1].Count; while (y < yMax) { tempHand[4] = rankList[w + x - 1][y]; if (gameHand == null || HandEvaluator.CompareStraight(tempHand, gameHand)) { return(tempHand); } y++; } } } } tempHand.Clear(); w++; } return(null); }
public List <CardData> GetStraight(List <CardData> gameHand) { gameHand = gameHand.SortBySuits(); gameHand = gameHand.SortByRank(); var groupedList = playerCards.GroupBy(c => c.rank).Select(group => new { key = group.Key, list = group.ToList() }); groupedList = groupedList.OrderBy(g => g.key); List <List <CardData> > rankList = new List <List <CardData> >(); //converting grouped ienumerable to generic list because crash (???) foreach (var group in groupedList) { List <CardData> list = new List <CardData>(group.list); list = list.SortBySuits(); list = list.SortByRank(); rankList.Add(list); } List <CardData> tempHand = new List <CardData>(); int w = 0; while (w < rankList.Count - 5) { if (tempHand.Count == 0) //making sure { int x = 0; while (x < 5) { tempHand.Add(rankList[w + x][0]); x++; } if (tempHand.IsStraight()) { if (HandEvaluator.CompareStraight(tempHand, gameHand)) { return(tempHand); } else if (tempHand[4].rank == gameHand[4].rank) //if straight has same head { //try changing head if straight lose int y = 1; int yMax = rankList[w + x - 1].Count; while (y < yMax) { tempHand[4] = rankList[w + x - 1][y]; if (HandEvaluator.CompareStraight(tempHand, gameHand)) { return(tempHand); } y++; } } } } tempHand.Clear(); w++; } return(null); }