public override int GetHashCode() { StringBuilder contentsBuilder = new StringBuilder(); var copy = new HashableQueue <T>(this); while (copy.Count > 0) { contentsBuilder.Append(copy.Dequeue().ToString()); } return(HashCode.Combine(contentsBuilder.ToString())); }
// Return 1 or 2 depending on who wins static int PlayRecursiveGame(HashableQueue <int> Player1Cards, HashableQueue <int> Player2Cards) { HashSet <HashableQueues <int> > gameMemory = new HashSet <HashableQueues <int> >(); while (Player1Cards.Count > 0 && Player2Cards.Count > 0) { HashableQueues <int> set = new HashableQueues <int>() { Queue1 = Player1Cards, Queue2 = Player2Cards }; if (gameMemory.Contains(set)) { return(1); } gameMemory.Add(set); int nextP1 = Player1Cards.Dequeue(); int nextP2 = Player2Cards.Dequeue(); int winner; if (nextP1 <= Player1Cards.Count && nextP2 <= Player2Cards.Count) { winner = PlayRecursiveGame(Player1Cards.Copy(nextP1), Player2Cards.Copy(nextP2)); } else { winner = nextP1 > nextP2 ? 1 : 2; } if (winner == 1) { Player1Cards.Enqueue(nextP1); Player1Cards.Enqueue(nextP2); } else { Player2Cards.Enqueue(nextP2); Player2Cards.Enqueue(nextP1); } } return(Player1Cards.Count > 0 ? 1 : 2); }
public override bool Equals(object obj) { if (obj is HashableQueue <T> other) { if (other.Count != this.Count) { return(false); } var thisCopy = new HashableQueue <T>(this); var otherCopy = new HashableQueue <T>(other); while (thisCopy.Count > 0) { if (!thisCopy.Dequeue().Equals(otherCopy.Dequeue())) { return(false); } } return(true); } return(false); }