public void HandPointCalculator_GetAllPoints_Zero() { Hand hand = new Hand(); hand.Cards = new List <Card>() { new Card(CardSuit.Diamond, CardFace.Two), new Card(CardSuit.Heart, CardFace.Four), new Card(CardSuit.Spade, CardFace.Six), new Card(CardSuit.Spade, CardFace.Eight), }; HandPointCalculator pointCalc = new HandPointCalculator(hand, new Card(CardSuit.Club, CardFace.Ten)); Assert.AreEqual(0, pointCalc.GetAllPoints()); }
public void HandPointCalculator_GetAllPoints_ComboWithoutStraights() { Hand hand = new Hand(); hand.Cards = new List <Card>() { new Card(CardSuit.Spade, CardFace.Two), new Card(CardSuit.Diamond, CardFace.Three), new Card(CardSuit.Spade, CardFace.Queen), new Card(CardSuit.Spade, CardFace.Three), }; HandPointCalculator pointCalc = new HandPointCalculator(hand, new Card(CardSuit.Diamond, CardFace.Two)); Assert.AreEqual(12, pointCalc.GetAllPoints()); }
public void HandPointCalculator_GetAllPoints_ComboWithStraights() { Hand hand = new Hand(); hand.Cards = new List <Card>() { new Card(CardSuit.Diamond, CardFace.Six), new Card(CardSuit.Heart, CardFace.Jack), new Card(CardSuit.Spade, CardFace.Nine), new Card(CardSuit.Spade, CardFace.Eight), }; HandPointCalculator pointCalc = new HandPointCalculator(hand, new Card(CardSuit.Heart, CardFace.Seven)); Assert.AreEqual(9, pointCalc.GetAllPoints()); }
static void PerformGameAction(GameEngine gameEngine) { bool cardSelected = false; int currentPlayerIndex = -1; Hand playerHand = null; int selectedIndex = -1; int score = 0; DisplayScores(gameEngine); switch (gameEngine.GetCurrentAction()) { case PlayerAction.Deal: Console.WriteLine("{0}... Press ENTER to deal cards...", GetCurrentPlayersName(gameEngine)); Console.ReadLine(); Console.WriteLine(); gameEngine.DealCards(); ShowPlayerHand(gameEngine, 0); ShowPlayerHand(gameEngine, 1); Console.WriteLine(); break; case PlayerAction.CreateCrib: currentPlayerIndex = gameEngine.GetCurrentPlayers()[0]; playerHand = gameEngine.GetPlayerHand(currentPlayerIndex); while (!cardSelected) { ShowPlayerHand(gameEngine, currentPlayerIndex, true); Console.Write("{0}... Pick a card for the crib: ", GetCurrentPlayersName(gameEngine)); if (!int.TryParse(Console.ReadLine(), out selectedIndex) || selectedIndex < 0 || selectedIndex >= playerHand.Cards.Count) { Console.WriteLine("Invalid card index. Please select a number between 0 and {0}", playerHand.Cards.Count - 1); } else { cardSelected = true; } Console.WriteLine(); } gameEngine.AddToCrib(currentPlayerIndex, playerHand.Cards[selectedIndex]); break; case PlayerAction.PlayOrPass: currentPlayerIndex = gameEngine.GetCurrentPlayers()[0]; playerHand = gameEngine.GetPlayerHand(currentPlayerIndex); Console.WriteLine("Played Card Total: {0}", gameEngine.GetSumOfPlayedCards()); Console.Write("Starter Card:"); ShowCard(gameEngine.GetStarterCard()); Console.WriteLine(); ShowPlayerHand(gameEngine, currentPlayerIndex, true); Console.Write("{0}... Pick a card to play or press 9 to pass: "******"Invalid card index. Please select a number between 0 and {0} or select 9 to pass", playerHand.Cards.Count - 1); } else { try { if (selectedIndex == 9) { Console.WriteLine("Player Passed"); gameEngine.PlayerPass(currentPlayerIndex); } else { Console.Write("Played "); ShowCard(playerHand.Cards[selectedIndex]); Console.WriteLine(); gameEngine.PlayCard(currentPlayerIndex, playerHand.Cards[selectedIndex]); } } catch (Exception e) // TODO: Review. Should these be exceptions? If yes, should I create custom exceptions? { Console.WriteLine("ERROR: Invalid Selection. {0}", e.Message); } } Console.WriteLine(); break; case PlayerAction.ScoreHands: currentPlayerIndex = gameEngine.GetCurrentPlayers()[0]; playerHand = gameEngine.GetPlayerHand(currentPlayerIndex); ShowPlayerHand(gameEngine, currentPlayerIndex); Console.Write("Starter Card: "); ShowCard(gameEngine.GetStarterCard()); Console.WriteLine(); HandPointCalculator handPoints = new HandPointCalculator(playerHand, gameEngine.GetStarterCard()); score = handPoints.GetAllPoints(); Console.WriteLine("Hand Score: {0}", score); gameEngine.IsProvidedScoreCorrectForHand(currentPlayerIndex, score); Console.WriteLine(); break; case PlayerAction.ScoreCrib: currentPlayerIndex = gameEngine.GetCurrentPlayers()[0]; playerHand = gameEngine.GetCrib(); Console.Write("Crib: "); ShowHand(gameEngine, gameEngine.GetCrib()); Console.Write("Starter Card: "); ShowCard(gameEngine.GetStarterCard()); Console.WriteLine(); HandPointCalculator cribPoints = new HandPointCalculator(playerHand, gameEngine.GetStarterCard()); score = cribPoints.GetAllPoints(); Console.WriteLine("Crib Score: {0}", score); gameEngine.IsProvidedScoreCorrectForCrib(score); Console.WriteLine(); break; case PlayerAction.DeclareWinner: Console.WriteLine("{0} is the WINNER!", gameEngine.GetPlayerName(gameEngine.GetWinningPlayer())); gameDone = true; break; default: throw new ApplicationException("Unexpected PlayerAction"); } }
public async Task DoNextAction() { int currentPlayer = gameEngine.GetCurrentPlayers()[0]; PointCalculator pointCalculator; switch (gameEngine.GetCurrentAction()) { case PlayerAction.ScoreHands: // TODO: If human, allow them to provide score pointCalculator = new HandPointCalculator(gameEngine.GetPlayerHand(currentPlayer), gameEngine.GetStarterCard()); gameEngine.IsProvidedScoreCorrectForHand(currentPlayer, pointCalculator.GetAllPoints()); return; case PlayerAction.ScoreCrib: // TODO: If human, allow them to provide score pointCalculator = new HandPointCalculator(gameEngine.GetCrib(), gameEngine.GetStarterCard()); gameEngine.IsProvidedScoreCorrectForCrib(pointCalculator.GetAllPoints()); IList <RoundScore> roundScores = new List <RoundScore>(); for (int i = 0; i < gameEngine.GetNumberOfPlayers(); i++) { // TODO: Should be able to refactor so that I can use the hand/crib // scores from earlier calculations HandPointCalculator handPointCalculator = new HandPointCalculator(gameEngine.GetPlayerHand(i), gameEngine.GetStarterCard()); int handScore = handPointCalculator.GetAllPoints(); int cribScore = 0; if (i == gameEngine.GetDealer()) { HandPointCalculator cribPointCalculator = new HandPointCalculator(gameEngine.GetCrib(), gameEngine.GetStarterCard()); cribScore = cribPointCalculator.GetAllPoints(); } roundScores.Add(new RoundScore(gameEngine.GetPlayerName(i), handScore, cribScore, gameEngine.GetPlayerScore(i) - handScore - cribScore)); } EndOfRound endOfRoundForm = new EndOfRound(roundScores); endOfRoundForm.Owner = this; endOfRoundForm.ShowDialog(); return; case PlayerAction.DeclareWinner: string message; if (gameEngine.GetWinningPlayer() == 0) { message = "Computer won. Better luck next time."; } else { message = string.Format("Congratulations {0}! You won!", gameEngine.GetPlayerName(gameEngine.GetWinningPlayer())); } DeclareWinner declareWinnerForm = new DeclareWinner(message); declareWinnerForm.Owner = this; declareWinnerForm.ShowDialog(); gameEngine.StartNextGame(); return; } if (currentPlayer == 0) { Thread.Sleep(500); switch (gameEngine.GetCurrentAction()) { case PlayerAction.Deal: gameEngine.DealCards(); break; case PlayerAction.CreateCrib: gameEngine.AddToCrib(0, SelectCardForCrib()); break; case PlayerAction.PlayOrPass: Card selectedCard = SelectCardForPlay(); if (selectedCard == null) { gameEngine.PlayerPass(0); } else { gameEngine.PlayCard(0, selectedCard); } break; default: textBoxMessage.Text = "Computer doesn't know what to do... yet"; break; } } }