public List <CardData> GetFlush(List <CardData> gameHand) { var groupedList = hand.cardDatas.GroupBy(c => c.suit).Select(group => new { key = group.Key, list = group.ToList() }); groupedList = groupedList.OrderBy(g => g.key); foreach (var group in groupedList) { if (group.list.Count >= 5) { if (group.list.Count > 5) //more than 5 of the same suit { List <CardData> sortedList = group.list.SortBySuits(); List <CardData> tempHand = new List <CardData>(); for (int i = 0; i < 5; i++) { tempHand.Add(sortedList[i]); } if (gameHand == null || HandEvaluator.CompareFlush(tempHand, gameHand)) //if weakest flush wins against hand { return(tempHand); } else if (gameHand.IsFlush() && gameHand[0].suit == group.list[0].suit) //check if game hand is also flush of the same suit { int w = 5; while (w < sortedList.Count) { Debug.Log("w: " + w); tempHand[4] = sortedList[w]; //change flush head if (gameHand == null || HandEvaluator.CompareFlush(tempHand, gameHand)) { return(tempHand); } w++; } } } else //exactly 5 { Debug.Log("exactly 5"); if (gameHand == null || HandEvaluator.CompareFlush(group.list, gameHand)) { return(group.list); } } } } return(null); }