public void SolveSecondPuzzle() { var sut = new RecursiveCombat(PuzzleData.PUZZLE_DATA); sut.Play(); sut.CalculatePointsForWinner(); Assert.Equal(32665, sut.WinnerPoints); }
public void PlayRecursiveGameCorrectly() { var sut = new RecursiveCombat(PuzzleData.SAMPLE_DATA); sut.Play(); sut.CalculatePointsForWinner(); Assert.Equal(291, sut.WinnerPoints); }
public Player Play() { while (Player1.Hand.Count > 0 && Player2.Hand.Count > 0) { int player1HandHash = HashHand(Player1.Hand); int player2HandHash = HashHand(Player2.Hand); // Avoid infinite recursion if (player1History.Contains(player1HandHash) && player2History.Contains(player2HandHash)) { return(Player1); } // Add the current state player1History.Add(player1HandHash); player2History.Add(player2HandHash); int player1Card = Player1.Hand.Dequeue(); int player2Card = Player2.Hand.Dequeue(); Player winner; // Recursive game if (player1Card <= Player1.Hand.Count && player2Card <= Player2.Hand.Count) { RecursiveCombat nextGame = new RecursiveCombat(new Player(Player1, player1Card), new Player(Player2, player2Card)); winner = nextGame.Play(); } else { winner = player1Card > player2Card ? Player1 : Player2; } if (winner.Id == Player1.Id) { Player1.Hand.Enqueue(player1Card); Player1.Hand.Enqueue(player2Card); } else { Player2.Hand.Enqueue(player2Card); Player2.Hand.Enqueue(player1Card); } } return(Player1.Hand.Count == 0 ? Player2 : Player1); }
public void PreventInfiniteRecursion() { const string decks = @"Player 1: 43 19 Player 2: 2 29 14"; var sut = new RecursiveCombat(decks); sut.Play(); sut.CalculatePointsForWinner(); Assert.Equal(105, sut.WinnerPoints); }