Make a blackjack game, play against the computer-dealer.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package rocks.zipcode;
  2. import org.junit.Assert;
  3. import org.junit.Test;
  4. public class BlackJackRuleTest {
  5. @Test
  6. public void testIsBusted_whenPlayerHas21() {
  7. // setup
  8. Player player = new Player();
  9. player.addCard(new Cards(Cards.Rank.ACE, Cards.Suit.CLUBS));
  10. player.addCard(new Cards(Cards.Rank.JACK, Cards.Suit.CLUBS));
  11. BlackJackRule rule = new BlackJackRule();
  12. // When - invoking method
  13. boolean actual = rule.isBusted(player);
  14. //Then - verify result
  15. Assert.assertFalse(actual);
  16. }
  17. @Test
  18. public void testIsBusted_whenScoreIsOver21() {
  19. // setup
  20. Player player = new Player();
  21. player.addCard(new Cards(Cards.Rank.QUEEN, Cards.Suit.CLUBS));
  22. player.addCard(new Cards(Cards.Rank.JACK, Cards.Suit.CLUBS));
  23. player.addCard(new Cards(Cards.Rank.DEUCE, Cards.Suit.CLUBS));
  24. BlackJackRule rule = new BlackJackRule();
  25. // When - invoking method
  26. boolean actual = rule.isBusted(player);
  27. //Then - verify result
  28. Assert.assertTrue(actual);
  29. }
  30. @Test
  31. public void testGetWinner_whenPlayerIsBustedAndDealerIsNot() {
  32. //Given set up data
  33. Player player = new Player("Player 1");
  34. player.addCard(new Cards(Cards.Rank.QUEEN, Cards.Suit.CLUBS));
  35. player.addCard(new Cards(Cards.Rank.JACK, Cards.Suit.CLUBS));
  36. player.addCard(new Cards(Cards.Rank.DEUCE, Cards.Suit.CLUBS));
  37. Player dealer = new Player("Dealer");
  38. dealer.addCard(new Cards(Cards.Rank.QUEEN, Cards.Suit.DIAMONDS));
  39. dealer.addCard(new Cards(Cards.Rank.JACK, Cards.Suit.DIAMONDS));
  40. BlackJackRule rule = new BlackJackRule();
  41. // When - invoking method
  42. Player winner = rule.getWinner(dealer, player);
  43. //Then - verify result
  44. Assert.assertEquals(winner, dealer);
  45. }
  46. @Test
  47. public void testGetWinner_whenPlayerHandIsHigherThanDealer() {
  48. //Given set up data
  49. Player player = new Player("Player 1");
  50. player.addCard(new Cards(Cards.Rank.QUEEN, Cards.Suit.CLUBS));
  51. player.addCard(new Cards(Cards.Rank.JACK, Cards.Suit.CLUBS));
  52. Player dealer = new Player("Dealer");
  53. dealer.addCard(new Cards(Cards.Rank.QUEEN, Cards.Suit.DIAMONDS));
  54. dealer.addCard(new Cards(Cards.Rank.NINE, Cards.Suit.DIAMONDS));
  55. BlackJackRule rule = new BlackJackRule();
  56. // When - invoking method
  57. Player winner = rule.getWinner(dealer, player);
  58. //Then - verify result
  59. Assert.assertEquals(player, winner);
  60. }
  61. }