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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. // Given - set up data
  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 - invoke method
  13. boolean actual = rule.isBusted(player);
  14. // Then - verify result
  15. Assert.assertFalse(actual);
  16. }
  17. @Test
  18. public void testIsBusted_whenScoreIsOver21(){
  19. // Given - set up data
  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 - invoke 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 - setup 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 - invoke method
  42. Player winner = rule.getWinner(dealer, player);
  43. //Then - verify result
  44. Assert.assertEquals(dealer, winner);
  45. }
  46. @Test
  47. public void testGetWinner_whenPlayerAndDealerAreBusted(){
  48. //Given - setup 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.addCard(new Cards(Cards.Rank.DEUCE, Cards.Suit.CLUBS));
  53. Player dealer = new Player("Dealer");
  54. dealer.addCard(new Cards(Cards.Rank.QUEEN, Cards.Suit.DIAMONDS));
  55. dealer.addCard(new Cards(Cards.Rank.JACK, Cards.Suit.DIAMONDS));
  56. dealer.addCard(new Cards(Cards.Rank.DEUCE, Cards.Suit.DIAMONDS));
  57. BlackJackRule rule = new BlackJackRule();
  58. //When - invoke method
  59. Player winner = rule.getWinner(dealer, player);
  60. //Then - verify result
  61. Assert.assertNull(winner);
  62. }
  63. @Test
  64. public void testGetWinner_whenPlayerHandIsHigherThanDealer(){
  65. //Given - setup data
  66. Player player = new Player("Player 1");
  67. player.addCard(new Cards(Cards.Rank.QUEEN, Cards.Suit.CLUBS));
  68. player.addCard(new Cards(Cards.Rank.JACK, Cards.Suit.CLUBS));
  69. Player dealer = new Player("Dealer");
  70. dealer.addCard(new Cards(Cards.Rank.QUEEN, Cards.Suit.DIAMONDS));
  71. dealer.addCard(new Cards(Cards.Rank.NINE, Cards.Suit.DIAMONDS));
  72. BlackJackRule rule = new BlackJackRule();
  73. //When - invoke method
  74. Player winner = rule.getWinner(dealer, player);
  75. //Then - verify result
  76. Assert.assertEquals(player, winner);
  77. }
  78. @Test
  79. public void testGetWinner_whenDealerHandIsHigherThanPlayer(){
  80. //Given - setup data
  81. Player player = new Player("Player 1");
  82. player.addCard(new Cards(Cards.Rank.QUEEN, Cards.Suit.CLUBS));
  83. player.addCard(new Cards(Cards.Rank.JACK, Cards.Suit.CLUBS));
  84. Player dealer = new Player("Dealer");
  85. dealer.addCard(new Cards(Cards.Rank.QUEEN, Cards.Suit.DIAMONDS));
  86. dealer.addCard(new Cards(Cards.Rank.ACE, Cards.Suit.DIAMONDS));
  87. BlackJackRule rule = new BlackJackRule();
  88. //When - invoke method
  89. Player winner = rule.getWinner(dealer, player);
  90. //Then - verify result
  91. Assert.assertEquals(dealer, winner);
  92. }
  93. @Test
  94. public void testGetWinner_whenDealerIsTheSameAsPlayer(){
  95. //Given - setup data
  96. Player player = new Player("Player 1");
  97. player.addCard(new Cards(Cards.Rank.QUEEN, Cards.Suit.CLUBS));
  98. player.addCard(new Cards(Cards.Rank.JACK, Cards.Suit.CLUBS));
  99. Player dealer = new Player("Dealer");
  100. dealer.addCard(new Cards(Cards.Rank.QUEEN, Cards.Suit.DIAMONDS));
  101. dealer.addCard(new Cards(Cards.Rank.JACK, Cards.Suit.DIAMONDS));
  102. BlackJackRule rule = new BlackJackRule();
  103. //When - invoke method
  104. Player winner = rule.getWinner(dealer, player);
  105. //Then - verify result
  106. Assert.assertNull(winner);
  107. }
  108. }