protected void MainCardSelectionAlgorithm() { List <int> allowedCardIndexes = GetAllowedCardIndexes(); MainGameModtroller.PlayDirection direction = _MainGameModtroller.Direction; float cardValueMidpoint = Enum.GetValues(typeof(CardModel.CardValue)).Length / 2.0f; ExtremeAverageCardSplitter splitter = new ExtremeAverageCardSplitter(this, allowedCardIndexes); if (splitter.AverageCardIndexes.Count > 0) { _MainGameModtroller.EndPlayerTurn(splitter.AverageCardIndexes.AllBest((a, b) => a.Length > b.Length, (a, b) => a.Length == b.Length) .Best((a, b) => Math.Abs(Hand.ReadOnlyCards[a[0]].CardValue - cardValueMidpoint) < Math.Abs(Hand.ReadOnlyCards[b[0]].CardValue - cardValueMidpoint))); } else { if (direction == MainGameModtroller.PlayDirection.UP) { if (splitter.ExtremeHighCardIndexes.Count > 0) { _MainGameModtroller.EndPlayerTurn(splitter.ExtremeHighCardIndexes.Best((a, b) => Hand.ReadOnlyCards[a[0]].CardValue < Hand.ReadOnlyCards[b[0]].CardValue)); } else { _MainGameModtroller.EndPlayerTurn(splitter.ExtremeLowCardsIndexes.Best((a, b) => Hand.ReadOnlyCards[a[0]].CardValue > Hand.ReadOnlyCards[b[0]].CardValue)); } } else if (direction == MainGameModtroller.PlayDirection.DOWN) { if (splitter.ExtremeLowCardsIndexes.Count > 0) { _MainGameModtroller.EndPlayerTurn(splitter.ExtremeLowCardsIndexes.Best((a, b) => Hand.ReadOnlyCards[a[0]].CardValue > Hand.ReadOnlyCards[b[0]].CardValue)); } else { _MainGameModtroller.EndPlayerTurn(splitter.ExtremeHighCardIndexes.Best((a, b) => Hand.ReadOnlyCards[a[0]].CardValue < Hand.ReadOnlyCards[b[0]].CardValue)); } } else // if (direction == MainGameModtroller.PlayDirection.UNDECIDED) { _MainGameModtroller.EndPlayerTurn(splitter.ExtremeLowCardsIndexes.Union(splitter.ExtremeHighCardIndexes, new NxUtils.FalseEqualityComparer <int[]>()) .Best((a, b) => Math.Abs(Hand.ReadOnlyCards[a[0]].CardValue - cardValueMidpoint) < Math.Abs(Hand.ReadOnlyCards[b[0]].CardValue - cardValueMidpoint))); } } }
public override void BeginCardSelection() { MainGameModtroller.PlayDirection direction = _MainGameModtroller.Direction; List <int> allowedCardIndexes = GetAllowedCardIndexes(); if (allowedCardIndexes.Count > 0) { var allOtherCards = new List <CardController>(); if (direction != MainGameModtroller.PlayDirection.UNDECIDED) { AbstractPlayerModtroller[] allPlayers = _MainGameModtroller.Players; for (int i = 0, iMax = allPlayers.Length; i < iMax; ++i) { if (!ReferenceEquals(allPlayers[i], this) && allPlayers[i] != null) { for (int j = 0, jMax = allPlayers[i].Hand.ReadOnlyCards.Count; j < jMax; ++j) { allOtherCards.Add(allPlayers[i].Hand.ReadOnlyCards[j]); } } } for (int i = 0, iMax = allowedCardIndexes.Count; i < iMax; ++i) { if ((direction == MainGameModtroller.PlayDirection.UP && !allOtherCards.Exists(c => c.CardValue >= Hand.ReadOnlyCards[allowedCardIndexes[i]].CardValue)) || (direction == MainGameModtroller.PlayDirection.DOWN && !allOtherCards.Exists(c => c.CardValue <= Hand.ReadOnlyCards[allowedCardIndexes[i]].CardValue))) { _MainGameModtroller.EndPlayerTurn(new int[] { allowedCardIndexes[i] }); return; } } } MainCardSelectionAlgorithm(); } else { _MainGameModtroller.EndPlayerTurn(null); } }
protected List <int> GetAllowedCardIndexes() { int lastCardValue = _MainGameModtroller.DiscardPileLastValue; MainGameModtroller.PlayDirection direction = _MainGameModtroller.Direction; ReadOnlyCollection <CardController> handCards = Hand.ReadOnlyCards; List <int> allowedCardIndexes = new List <int>(); if (_MainGameModtroller.MaxDeviationRule && direction != MainGameModtroller.PlayDirection.UNDECIDED) { allowedCardIndexes.AddRange(handCards.AllIndexesSuchThat( c => Mathf.Abs(c.CardValue - lastCardValue) <= _MainGameModtroller.MaxDeviationThreshold)); if (_MainGameModtroller.WildcardRule) { allowedCardIndexes.AddRange(handCards.AllIndexesSuchThat( (c, i) => c.CardValue == _MainGameModtroller.WildCardValue && !allowedCardIndexes.Contains(i))); } } else { for (int i = 0, iMax = handCards.Count; i < iMax; ++i) { allowedCardIndexes.Add(i); } } for (int i = allowedCardIndexes.LastIndex(); i >= 0; --i) // Reverse iterate to not invalidate { if (!((direction == MainGameModtroller.PlayDirection.UNDECIDED) || (_MainGameModtroller.WildcardRule && handCards[allowedCardIndexes[i]].CardValue == _MainGameModtroller.WildCardValue) || (direction == MainGameModtroller.PlayDirection.DOWN && handCards[allowedCardIndexes[i]].CardValue <= lastCardValue) || (direction == MainGameModtroller.PlayDirection.UP && handCards[allowedCardIndexes[i]].CardValue >= lastCardValue))) { allowedCardIndexes.RemoveAt(i); } } return(allowedCardIndexes); }