mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-21 23:00:43 -08:00
Initial playSplitHand implementation
This commit is contained in:
@@ -157,6 +157,7 @@ public class Game {
|
||||
} else if(player.getHand().size() == 2 && action.equalsIgnoreCase("/")) { // SPLIT
|
||||
if(player.getHand().get(0).equals(player.getHand().get(1))){
|
||||
playSplit(player);
|
||||
return;
|
||||
} else {
|
||||
userIo.println("SPLITTING NOT ALLOWED");
|
||||
action = userIo.prompt("PLAYER " + player.getPlayerNumber() + " ");
|
||||
@@ -178,8 +179,75 @@ public class Game {
|
||||
* @param player
|
||||
*/
|
||||
protected void playSplit(Player player) {
|
||||
// TODO refactor to avoid so much logic duplication
|
||||
|
||||
player.split();
|
||||
//TODO: Deal cards, and prompt user action
|
||||
// DEAL CARDS
|
||||
Card card = deck.deal();
|
||||
player.dealCard(card);
|
||||
if(card.getValue() == 1 || card.getValue() == 8) {
|
||||
userIo.println("FIRST HAND RECEIVES AN " + card.toString());
|
||||
} else {
|
||||
userIo.println("FIRST HAND RECEIVES A " + card.toString());
|
||||
}
|
||||
card = deck.deal();
|
||||
player.dealSplitHandCard(card);
|
||||
if(card.getValue() == 1 || card.getValue() == 8) {
|
||||
userIo.println("SECOND HAND RECEIVES AN " + card.toString());
|
||||
} else {
|
||||
userIo.println("SECOND HAND RECEIVES A " + card.toString());
|
||||
}
|
||||
|
||||
// Play hand 1
|
||||
String action = userIo.prompt("HAND 1");
|
||||
while(true){
|
||||
if(action.equalsIgnoreCase("H")){ // HIT
|
||||
Card c = deck.deal();
|
||||
player.dealCard(c);
|
||||
if(scoreHand(player.getHand()) > 21){
|
||||
userIo.println("...BUSTED");
|
||||
break;
|
||||
}
|
||||
action = userIo.prompt("RECEIVED A " + c.toString() + " HIT");
|
||||
} else if(action.equalsIgnoreCase("S")){ // STAY
|
||||
break;
|
||||
} else if(player.getHand().size() == 2 && action.equalsIgnoreCase("D")) { // DOUBLE DOWN
|
||||
player.setCurrentBet(player.getCurrentBet() * 2);
|
||||
player.dealCard(deck.deal());
|
||||
break;
|
||||
} else {
|
||||
if(player.getHand().size() > 2) {
|
||||
action = userIo.prompt("TYPE H, OR S, PLEASE");
|
||||
} else {
|
||||
action = userIo.prompt("TYPE H, S OR D, PLEASE");
|
||||
}
|
||||
}
|
||||
}
|
||||
// Play hand 2
|
||||
action = userIo.prompt("HAND 2");
|
||||
while(true){
|
||||
if(action.equalsIgnoreCase("H")){ // HIT
|
||||
Card c = deck.deal();
|
||||
player.dealSplitHandCard(card);
|
||||
if(scoreHand(player.getSplitHand()) > 21){
|
||||
userIo.println("...BUSTED");
|
||||
break;
|
||||
}
|
||||
action = userIo.prompt("RECEIVED A " + c.toString() + " HIT");
|
||||
} else if(action.equalsIgnoreCase("S")){ // STAY
|
||||
break;
|
||||
} else if(player.getSplitHand().size() == 2 && action.equalsIgnoreCase("D")) { // DOUBLE DOWN
|
||||
player.setSplitBet(player.getSplitBet() * 2);
|
||||
player.dealSplitHandCard(card);
|
||||
break;
|
||||
} else {
|
||||
if(player.getSplitHand().size() > 2) {
|
||||
action = userIo.prompt("TYPE H, OR S, PLEASE");
|
||||
} else {
|
||||
action = userIo.prompt("TYPE H, S OR D, PLEASE");
|
||||
}
|
||||
}
|
||||
}
|
||||
//TODO Uncomment playSplit tests and adjust as needed
|
||||
}
|
||||
|
||||
|
||||
@@ -89,6 +89,7 @@ public class Player {
|
||||
* Removes first card from hand to adds it to split hand
|
||||
*/
|
||||
public void split() {
|
||||
this.splitBet = this.currentBet;
|
||||
splitHand.add(hand.pop());
|
||||
}
|
||||
|
||||
|
||||
@@ -321,7 +321,7 @@ public class GameTest {
|
||||
assertEquals(0, result);
|
||||
}
|
||||
|
||||
//@Test
|
||||
@Test
|
||||
@DisplayName("playSplit() should end on STAY")
|
||||
public void playSplitEndOnStay(){
|
||||
// Given
|
||||
@@ -336,10 +336,9 @@ public class GameTest {
|
||||
// Then
|
||||
assertTrue(out.toString().contains("FIRST HAND RECEIVES"));
|
||||
assertTrue(out.toString().contains("SECOND HAND RECEIVES"));
|
||||
assertEquals("PLAYER 1 ? ", out.toString());
|
||||
}
|
||||
|
||||
//@Test
|
||||
@Test
|
||||
@DisplayName("playSplit() should allow HIT until BUST")
|
||||
public void playSplitHitUntilBust() {
|
||||
// Given
|
||||
@@ -360,7 +359,7 @@ public class GameTest {
|
||||
assertTrue(out.toString().contains("BUSTED"));
|
||||
}
|
||||
|
||||
//@Test
|
||||
@Test
|
||||
@DisplayName("playSplit should allow double down")
|
||||
public void playSplitDoubleDown(){
|
||||
// Given
|
||||
@@ -385,7 +384,7 @@ public class GameTest {
|
||||
assertTrue(player.getSplitHand().size() == 3);
|
||||
}
|
||||
|
||||
//@Test
|
||||
@Test
|
||||
@DisplayName("playSplit should NOT allow re-splitting")
|
||||
public void playSplitDoubleDownLate(){
|
||||
// Given
|
||||
@@ -394,7 +393,7 @@ public class GameTest {
|
||||
player.dealCard(new Card(1, Card.Suit.HEARTS));
|
||||
player.dealCard(new Card(1, Card.Suit.SPADES));
|
||||
|
||||
givenInput("\\\nS\nS\n",
|
||||
givenInput("/\nS\nS\n",
|
||||
new Card(13, Card.Suit.HEARTS),
|
||||
new Card(13, Card.Suit.SPADES));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user