mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-21 23:00:43 -08:00
Update player with split fields, stub out playSplit, and playSplit tests
This commit is contained in:
@@ -136,7 +136,6 @@ public class Game {
|
||||
* actions. On a hit, prints "RECEIVED A [x] HIT? "
|
||||
*
|
||||
* @param player
|
||||
* @param deck
|
||||
*/
|
||||
protected void play(Player player) {
|
||||
String action = userIo.prompt("PLAYER " + player.getPlayerNumber() + " ");
|
||||
@@ -156,10 +155,8 @@ public class Game {
|
||||
player.dealCard(deck.deal());
|
||||
return;
|
||||
} else if(player.getHand().size() == 2 && action.equalsIgnoreCase("/")) { // SPLIT
|
||||
if(player.getHand().get(0).equals(player.getHand().get(1))){
|
||||
// TODO split = split into two hands that play separately. only allowed for pairs
|
||||
// TODO implement player.split that takes one card from 'hand' and adds it to a new 'splitHand' field.
|
||||
// TODO determine if the original code allowed re-splitting, splitting on aces, or doubling down on a split and if it requires cards
|
||||
if(player.getHand().get(0).equals(player.getHand().get(1))){
|
||||
playSplit(player);
|
||||
} else {
|
||||
userIo.println("SPLITTING NOT ALLOWED");
|
||||
action = userIo.prompt("PLAYER " + player.getPlayerNumber() + " ");
|
||||
@@ -174,6 +171,18 @@ public class Game {
|
||||
}
|
||||
|
||||
}
|
||||
/**
|
||||
* Splits the players hand and deals a card to each hand then prompts the user to
|
||||
* hit (H), stay (S), or double down (D), and then performs those actions.
|
||||
*
|
||||
* @param player
|
||||
*/
|
||||
protected void playSplit(Player player) {
|
||||
player.split();
|
||||
//TODO: Deal cards, and prompt user action
|
||||
//TODO Uncomment playSplit tests and adjust as needed
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculates the value of a hand. When the hand contains aces, it will
|
||||
|
||||
@@ -4,9 +4,11 @@ public class Player {
|
||||
|
||||
private int playerNumber; // e.g. playerNumber = 1 means "this is Player 1"
|
||||
private double currentBet;
|
||||
private double insuranceBet;
|
||||
private double splitBet;
|
||||
private double total;
|
||||
private LinkedList<Card> hand;
|
||||
// TODO we'll need to decide how to deal with a split hand or doubled down bet.
|
||||
private LinkedList<Card> splitHand;
|
||||
|
||||
/**
|
||||
* Represents a player in the game with cards, bets, total and a playerNumber.
|
||||
@@ -14,11 +16,14 @@ public class Player {
|
||||
public Player(int playerNumber) {
|
||||
this.playerNumber = playerNumber;
|
||||
currentBet = 0;
|
||||
insuranceBet = 0;
|
||||
splitBet = 0;
|
||||
total = 0;
|
||||
hand = new LinkedList<>();
|
||||
splitHand = new LinkedList<>();
|
||||
}
|
||||
|
||||
public void setPlayerNumber(int playerNumber) { //TODO: Is this needed if set in constructor?
|
||||
public void setPlayerNumber(int playerNumber) {
|
||||
this.playerNumber = playerNumber;
|
||||
}
|
||||
|
||||
@@ -34,6 +39,22 @@ public class Player {
|
||||
return this.currentBet;
|
||||
}
|
||||
|
||||
public double getSplitBet() {
|
||||
return splitBet;
|
||||
}
|
||||
|
||||
public void setSplitBet(double splitBet) {
|
||||
this.splitBet = splitBet;
|
||||
}
|
||||
|
||||
public double getInsuranceBet() {
|
||||
return insuranceBet;
|
||||
}
|
||||
|
||||
public void setInsuranceBet(double insuranceBet) {
|
||||
this.insuranceBet = insuranceBet;
|
||||
}
|
||||
|
||||
/**
|
||||
* RecordWin adds 'currentBet' to 'total' and then sets 'currentBet' to zero
|
||||
*/
|
||||
@@ -60,13 +81,28 @@ public class Player {
|
||||
public void dealCard(Card card) {
|
||||
hand.add(card);
|
||||
}
|
||||
|
||||
public void dealSplitHandCard(Card card) {
|
||||
splitHand.add(card);
|
||||
}
|
||||
/**
|
||||
* Removes first card from hand to adds it to split hand
|
||||
*/
|
||||
public void split() {
|
||||
splitHand.add(hand.pop());
|
||||
}
|
||||
|
||||
// resetHand resets 'hand' to an empty list
|
||||
// resetHand resets 'hand' & 'splitHand' to empty lists
|
||||
public void resetHand() {
|
||||
this.hand = new LinkedList<>();
|
||||
this.splitHand = new LinkedList<>();
|
||||
}
|
||||
|
||||
public LinkedList<Card> getHand() {
|
||||
return this.hand;
|
||||
}
|
||||
|
||||
public LinkedList<Card> getSplitHand() {
|
||||
return this.splitHand;
|
||||
}
|
||||
}
|
||||
@@ -79,7 +79,7 @@ public class GameTest {
|
||||
// Given
|
||||
Player player = new Player(1);
|
||||
player.dealCard(new Card(10, Card.Suit.HEARTS));
|
||||
player.dealCard(new Card(9, Card.Suit.SPADES));
|
||||
player.dealCard(new Card(10, Card.Suit.SPADES));
|
||||
|
||||
givenInput("H\nH\nH\n",
|
||||
new Card(1, Card.Suit.SPADES), // 20
|
||||
@@ -321,5 +321,88 @@ public class GameTest {
|
||||
assertEquals(0, result);
|
||||
}
|
||||
|
||||
//@Test
|
||||
@DisplayName("playSplit() should end on STAY")
|
||||
public void playSplitEndOnStay(){
|
||||
// Given
|
||||
Player player = new Player(1);
|
||||
player.dealCard(new Card(1, Card.Suit.CLUBS));
|
||||
player.dealCard(new Card(1, Card.Suit.SPADES));
|
||||
givenInput("S\nS\n");
|
||||
|
||||
// When
|
||||
game.playSplit(player);
|
||||
|
||||
// Then
|
||||
assertTrue(out.toString().contains("FIRST HAND RECEIVES"));
|
||||
assertTrue(out.toString().contains("SECOND HAND RECEIVES"));
|
||||
assertEquals("PLAYER 1 ? ", out.toString());
|
||||
}
|
||||
|
||||
//@Test
|
||||
@DisplayName("playSplit() should allow HIT until BUST")
|
||||
public void playSplitHitUntilBust() {
|
||||
// Given
|
||||
Player player = new Player(1);
|
||||
player.dealCard(new Card(10, Card.Suit.HEARTS));
|
||||
player.dealCard(new Card(10, Card.Suit.SPADES));
|
||||
|
||||
givenInput("H\nH\n",
|
||||
new Card(12, Card.Suit.SPADES), // 20
|
||||
new Card(12, Card.Suit.HEARTS), // Split hand 20
|
||||
new Card(12, Card.Suit.DIAMONDS), // 30
|
||||
new Card(12, Card.Suit.CLUBS)); // Split hand 30
|
||||
|
||||
// When
|
||||
game.playSplit(player);
|
||||
|
||||
// Then
|
||||
assertTrue(out.toString().contains("BUSTED"));
|
||||
}
|
||||
|
||||
//@Test
|
||||
@DisplayName("playSplit should allow double down")
|
||||
public void playSplitDoubleDown(){
|
||||
// Given
|
||||
Player player = new Player(1);
|
||||
player.setCurrentBet(100);
|
||||
player.dealCard(new Card(9, Card.Suit.HEARTS));
|
||||
player.dealCard(new Card(9, Card.Suit.SPADES));
|
||||
|
||||
givenInput("D\nD\n",
|
||||
new Card(6, Card.Suit.HEARTS),
|
||||
new Card(7, Card.Suit.HEARTS),
|
||||
new Card(6, Card.Suit.CLUBS),
|
||||
new Card(7, Card.Suit.CLUBS));
|
||||
|
||||
// When
|
||||
game.playSplit(player);
|
||||
|
||||
// Then
|
||||
assertTrue(player.getCurrentBet() == 200);
|
||||
assertTrue(player.getSplitBet() == 200);
|
||||
assertTrue(player.getHand().size() == 3);
|
||||
assertTrue(player.getSplitHand().size() == 3);
|
||||
}
|
||||
|
||||
//@Test
|
||||
@DisplayName("playSplit should NOT allow re-splitting")
|
||||
public void playSplitDoubleDownLate(){
|
||||
// Given
|
||||
Player player = new Player(1);
|
||||
player.setCurrentBet(100);
|
||||
player.dealCard(new Card(1, Card.Suit.HEARTS));
|
||||
player.dealCard(new Card(1, Card.Suit.SPADES));
|
||||
|
||||
givenInput("\\\nS\nS\n",
|
||||
new Card(13, Card.Suit.HEARTS),
|
||||
new Card(13, Card.Suit.SPADES));
|
||||
|
||||
// When
|
||||
game.playSplit(player);
|
||||
|
||||
// Then
|
||||
assertTrue(out.toString().contains("TYPE H, S OR D, PLEASE"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user