feat(blackjack): add split functionality to the game
All checks were successful
All checks were successful
This commit is contained in:
parent
4a7c54eab8
commit
3ef5530e77
7 changed files with 410 additions and 276 deletions
|
@ -94,6 +94,23 @@ public class BlackJackGameController {
|
|||
return ResponseEntity.ok(blackJackService.doubleDown(game));
|
||||
}
|
||||
|
||||
@PostMapping("/blackjack/{id}/split")
|
||||
public ResponseEntity<Object> split(@PathVariable Long id, @RequestHeader("Authorization") String token) {
|
||||
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||
|
||||
if (optionalUser.isEmpty()) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
UserEntity user = optionalUser.get();
|
||||
BlackJackGameEntity game = blackJackService.getBlackJackGame(id);
|
||||
if (game == null || !Objects.equals(game.getUserId(), user.getId())) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(blackJackService.split(game));
|
||||
}
|
||||
|
||||
@PostMapping("/blackjack/start")
|
||||
public ResponseEntity<Object> createBlackJackGame(@RequestBody @Valid CreateBlackJackGameDto createBlackJackGameDto, @RequestHeader("Authorization") String token) {
|
||||
Optional<UserEntity> optionalUser = userService.getCurrentUser(token);
|
||||
|
|
|
@ -51,4 +51,15 @@ public class BlackJackGameEntity {
|
|||
@JsonManagedReference
|
||||
@SQLRestriction("card_type = 'DEALER'")
|
||||
private List<CardEntity> dealerCards = new ArrayList<>();
|
||||
|
||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@JsonManagedReference
|
||||
@SQLRestriction("card_type = 'PLAYER_SPLIT'")
|
||||
private List<CardEntity> playerSplitCards = new ArrayList<>();
|
||||
|
||||
@Column(name = "split_bet")
|
||||
private BigDecimal splitBet;
|
||||
|
||||
@Column(name = "is_split")
|
||||
private boolean isSplit;
|
||||
}
|
||||
|
|
|
@ -1,245 +1,318 @@
|
|||
package de.szut.casino.blackjack;
|
||||
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import de.szut.casino.user.UserRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
|
||||
@Service
|
||||
public class BlackJackService {
|
||||
private final BlackJackGameRepository blackJackGameRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final Random random = new Random();
|
||||
|
||||
public BlackJackService(BlackJackGameRepository blackJackGameRepository, UserRepository userRepository) {
|
||||
this.blackJackGameRepository = blackJackGameRepository;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public BlackJackGameEntity getBlackJackGame(Long id) {
|
||||
return blackJackGameRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BlackJackGameEntity createBlackJackGame(UserEntity user, BigDecimal betAmount) {
|
||||
BlackJackGameEntity game = new BlackJackGameEntity();
|
||||
game.setUser(user);
|
||||
game.setBet(betAmount);
|
||||
|
||||
initializeDeck(game);
|
||||
dealInitialCards(game);
|
||||
|
||||
game.setState(getState(game));
|
||||
deductBetFromBalance(user, betAmount);
|
||||
|
||||
return blackJackGameRepository.save(game);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BlackJackGameEntity hit(BlackJackGameEntity game) {
|
||||
game = refreshGameState(game);
|
||||
|
||||
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
||||
return game;
|
||||
}
|
||||
|
||||
dealCardToPlayer(game);
|
||||
updateGameStateAndBalance(game);
|
||||
|
||||
return blackJackGameRepository.save(game);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BlackJackGameEntity stand(BlackJackGameEntity game) {
|
||||
game = refreshGameState(game);
|
||||
|
||||
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
||||
return game;
|
||||
}
|
||||
|
||||
dealCardsToDealerUntilMinimumScore(game);
|
||||
determineWinnerAndUpdateBalance(game);
|
||||
|
||||
return blackJackGameRepository.save(game);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BlackJackGameEntity doubleDown(BlackJackGameEntity game) {
|
||||
game = refreshGameState(game);
|
||||
|
||||
if (game.getState() != BlackJackState.IN_PROGRESS || game.getPlayerCards().size() != 2) {
|
||||
return game;
|
||||
}
|
||||
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
BigDecimal additionalBet = game.getBet();
|
||||
|
||||
if (user.getBalance().compareTo(additionalBet) < 0) {
|
||||
return game;
|
||||
}
|
||||
|
||||
deductBetFromBalance(user, additionalBet);
|
||||
game.setBet(game.getBet().add(additionalBet));
|
||||
|
||||
dealCardToPlayer(game);
|
||||
updateGameStateAndBalance(game);
|
||||
|
||||
if (game.getState() == BlackJackState.IN_PROGRESS) {
|
||||
return stand(game);
|
||||
}
|
||||
|
||||
return game;
|
||||
}
|
||||
|
||||
private BlackJackGameEntity refreshGameState(BlackJackGameEntity game) {
|
||||
return blackJackGameRepository.findById(game.getId()).orElse(game);
|
||||
}
|
||||
|
||||
private UserEntity getUserWithFreshData(UserEntity user) {
|
||||
return userRepository.findById(user.getId()).orElse(user);
|
||||
}
|
||||
|
||||
private void dealInitialCards(BlackJackGameEntity game) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
dealCardToPlayer(game);
|
||||
}
|
||||
|
||||
dealCardToDealer(game);
|
||||
}
|
||||
|
||||
private void dealCardToPlayer(BlackJackGameEntity game) {
|
||||
CardEntity card = drawCardFromDeck(game);
|
||||
card.setCardType(CardType.PLAYER);
|
||||
game.getPlayerCards().add(card);
|
||||
}
|
||||
|
||||
private void dealCardToDealer(BlackJackGameEntity game) {
|
||||
CardEntity card = drawCardFromDeck(game);
|
||||
card.setCardType(CardType.DEALER);
|
||||
game.getDealerCards().add(card);
|
||||
}
|
||||
|
||||
private void dealCardsToDealerUntilMinimumScore(BlackJackGameEntity game) {
|
||||
while (calculateHandValue(game.getDealerCards()) < 17) {
|
||||
dealCardToDealer(game);
|
||||
}
|
||||
}
|
||||
|
||||
private void updateGameStateAndBalance(BlackJackGameEntity game) {
|
||||
game.setState(getState(game));
|
||||
|
||||
if (game.getState() == BlackJackState.PLAYER_WON) {
|
||||
updateUserBalance(game, true);
|
||||
} else if (game.getState() == BlackJackState.PLAYER_LOST) {
|
||||
updateUserBalance(game, false);
|
||||
}
|
||||
}
|
||||
|
||||
private void determineWinnerAndUpdateBalance(BlackJackGameEntity game) {
|
||||
int playerValue = calculateHandValue(game.getPlayerCards());
|
||||
int dealerValue = calculateHandValue(game.getDealerCards());
|
||||
|
||||
if (dealerValue > 21 || playerValue > dealerValue) {
|
||||
game.setState(BlackJackState.PLAYER_WON);
|
||||
updateUserBalance(game, true);
|
||||
} else if (playerValue < dealerValue) {
|
||||
game.setState(BlackJackState.PLAYER_LOST);
|
||||
updateUserBalance(game, false);
|
||||
} else {
|
||||
game.setState(BlackJackState.DRAW);
|
||||
updateUserBalance(game, false); // For draw, player gets their bet back
|
||||
}
|
||||
}
|
||||
|
||||
private void deductBetFromBalance(UserEntity user, BigDecimal betAmount) {
|
||||
user.setBalance(user.getBalance().subtract(betAmount));
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
private void updateUserBalance(BlackJackGameEntity game, boolean isWin) {
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
BigDecimal betAmount = game.getBet();
|
||||
BigDecimal balance = user.getBalance();
|
||||
|
||||
if (isWin) {
|
||||
balance = balance.add(betAmount.multiply(BigDecimal.valueOf(2)));
|
||||
} else if (game.getState() == BlackJackState.DRAW) {
|
||||
balance = balance.add(betAmount);
|
||||
}
|
||||
|
||||
user.setBalance(balance);
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
private void initializeDeck(BlackJackGameEntity game) {
|
||||
for (Suit suit : Suit.values()) {
|
||||
for (Rank rank : Rank.values()) {
|
||||
CardEntity card = new CardEntity();
|
||||
card.setGame(game);
|
||||
card.setSuit(suit);
|
||||
card.setRank(rank);
|
||||
card.setCardType(CardType.DECK);
|
||||
game.getDeck().add(card);
|
||||
}
|
||||
}
|
||||
|
||||
java.util.Collections.shuffle(game.getDeck(), random);
|
||||
}
|
||||
|
||||
private CardEntity drawCardFromDeck(BlackJackGameEntity game) {
|
||||
if (game.getDeck().isEmpty()) {
|
||||
throw new IllegalStateException("Deck is empty");
|
||||
}
|
||||
|
||||
return game.getDeck().removeFirst();
|
||||
}
|
||||
|
||||
private BlackJackState getState(BlackJackGameEntity game) {
|
||||
int playerHandValue = calculateHandValue(game.getPlayerCards());
|
||||
|
||||
if (playerHandValue == 21) {
|
||||
CardEntity hole = drawCardFromDeck(game);
|
||||
hole.setCardType(CardType.DEALER);
|
||||
game.getDealerCards().add(hole);
|
||||
|
||||
int dealerHandValue = calculateHandValue(game.getDealerCards());
|
||||
|
||||
if (dealerHandValue == 21) {
|
||||
return BlackJackState.DRAW;
|
||||
} else {
|
||||
BigDecimal blackjackWinnings = game.getBet().multiply(new BigDecimal("1.5"));
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
user.setBalance(user.getBalance().add(blackjackWinnings));
|
||||
return BlackJackState.PLAYER_BLACKJACK;
|
||||
}
|
||||
} else if (playerHandValue > 21) {
|
||||
return BlackJackState.PLAYER_LOST;
|
||||
}
|
||||
|
||||
return BlackJackState.IN_PROGRESS;
|
||||
}
|
||||
|
||||
private int calculateHandValue(List<CardEntity> hand) {
|
||||
int sum = 0;
|
||||
int aceCount = 0;
|
||||
for (CardEntity card : hand) {
|
||||
sum += card.getRank().getValue();
|
||||
if (card.getRank() == Rank.ACE) {
|
||||
aceCount++;
|
||||
}
|
||||
}
|
||||
|
||||
while (sum > 21 && aceCount > 0) {
|
||||
sum -= 10;
|
||||
aceCount--;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
package de.szut.casino.blackjack;
|
||||
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import de.szut.casino.user.UserRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
|
||||
@Service
|
||||
public class BlackJackService {
|
||||
private final BlackJackGameRepository blackJackGameRepository;
|
||||
private final UserRepository userRepository;
|
||||
private final Random random = new Random();
|
||||
|
||||
public BlackJackService(BlackJackGameRepository blackJackGameRepository, UserRepository userRepository) {
|
||||
this.blackJackGameRepository = blackJackGameRepository;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
||||
public BlackJackGameEntity getBlackJackGame(Long id) {
|
||||
return blackJackGameRepository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BlackJackGameEntity createBlackJackGame(UserEntity user, BigDecimal betAmount) {
|
||||
BlackJackGameEntity game = new BlackJackGameEntity();
|
||||
game.setUser(user);
|
||||
game.setBet(betAmount);
|
||||
|
||||
initializeDeck(game);
|
||||
dealInitialCards(game);
|
||||
|
||||
game.setState(getState(game));
|
||||
deductBetFromBalance(user, betAmount);
|
||||
|
||||
return blackJackGameRepository.save(game);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BlackJackGameEntity hit(BlackJackGameEntity game) {
|
||||
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
||||
return game;
|
||||
}
|
||||
|
||||
if (game.isSplit()) {
|
||||
dealCardToPlayer(game);
|
||||
} else {
|
||||
dealCardToPlayer(game);
|
||||
}
|
||||
|
||||
updateGameStateAndBalance(game);
|
||||
|
||||
return blackJackGameRepository.save(game);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BlackJackGameEntity stand(BlackJackGameEntity game) {
|
||||
if (game.getState() != BlackJackState.IN_PROGRESS) {
|
||||
return game;
|
||||
}
|
||||
|
||||
dealCardsToDealerUntilMinimumScore(game);
|
||||
determineWinnerAndUpdateBalance(game);
|
||||
|
||||
return blackJackGameRepository.save(game);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BlackJackGameEntity doubleDown(BlackJackGameEntity game) {
|
||||
if (game.getState() != BlackJackState.IN_PROGRESS || game.getPlayerCards().size() != 2) {
|
||||
return game;
|
||||
}
|
||||
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
BigDecimal additionalBet = game.getBet();
|
||||
|
||||
if (user.getBalance().compareTo(additionalBet) < 0) {
|
||||
return game;
|
||||
}
|
||||
|
||||
deductBetFromBalance(user, additionalBet);
|
||||
game.setBet(game.getBet().add(additionalBet));
|
||||
|
||||
dealCardToPlayer(game);
|
||||
updateGameStateAndBalance(game);
|
||||
|
||||
if (game.getState() == BlackJackState.IN_PROGRESS) {
|
||||
return stand(game);
|
||||
}
|
||||
|
||||
return game;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public BlackJackGameEntity split(BlackJackGameEntity game) {
|
||||
if (game.getState() != BlackJackState.IN_PROGRESS ||
|
||||
game.getPlayerCards().size() != 2 ||
|
||||
game.isSplit() ||
|
||||
!game.getPlayerCards().get(0).getRank().equals(game.getPlayerCards().get(1).getRank())) {
|
||||
return game;
|
||||
}
|
||||
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
BigDecimal splitBet = game.getBet();
|
||||
|
||||
if (user.getBalance().compareTo(splitBet) < 0) {
|
||||
return game;
|
||||
}
|
||||
|
||||
deductBetFromBalance(user, splitBet);
|
||||
game.setSplitBet(splitBet);
|
||||
game.setSplit(true);
|
||||
|
||||
CardEntity card = game.getPlayerCards().remove(1);
|
||||
card.setCardType(CardType.PLAYER_SPLIT);
|
||||
game.getPlayerSplitCards().add(card);
|
||||
|
||||
dealCardToPlayer(game);
|
||||
dealCardToSplitHand(game);
|
||||
|
||||
return blackJackGameRepository.save(game);
|
||||
}
|
||||
|
||||
private BlackJackGameEntity refreshGameState(BlackJackGameEntity game) {
|
||||
return blackJackGameRepository.findById(game.getId()).orElse(game);
|
||||
}
|
||||
|
||||
private UserEntity getUserWithFreshData(UserEntity user) {
|
||||
return userRepository.findById(user.getId()).orElse(user);
|
||||
}
|
||||
|
||||
private void dealInitialCards(BlackJackGameEntity game) {
|
||||
for (int i = 0; i < 2; i++) {
|
||||
dealCardToPlayer(game);
|
||||
}
|
||||
|
||||
dealCardToDealer(game);
|
||||
}
|
||||
|
||||
private void dealCardToPlayer(BlackJackGameEntity game) {
|
||||
CardEntity card = drawCardFromDeck(game);
|
||||
card.setCardType(CardType.PLAYER);
|
||||
game.getPlayerCards().add(card);
|
||||
}
|
||||
|
||||
private void dealCardToDealer(BlackJackGameEntity game) {
|
||||
CardEntity card = drawCardFromDeck(game);
|
||||
card.setCardType(CardType.DEALER);
|
||||
game.getDealerCards().add(card);
|
||||
}
|
||||
|
||||
private void dealCardsToDealerUntilMinimumScore(BlackJackGameEntity game) {
|
||||
while (calculateHandValue(game.getDealerCards()) < 17) {
|
||||
dealCardToDealer(game);
|
||||
}
|
||||
}
|
||||
|
||||
private void dealCardToSplitHand(BlackJackGameEntity game) {
|
||||
CardEntity card = drawCardFromDeck(game);
|
||||
card.setCardType(CardType.PLAYER_SPLIT);
|
||||
game.getPlayerSplitCards().add(card);
|
||||
}
|
||||
|
||||
private void updateGameStateAndBalance(BlackJackGameEntity game) {
|
||||
if (game.isSplit()) {
|
||||
int mainHandValue = calculateHandValue(game.getPlayerCards());
|
||||
int splitHandValue = calculateHandValue(game.getPlayerSplitCards());
|
||||
|
||||
if (mainHandValue > 21 && splitHandValue > 21) {
|
||||
game.setState(BlackJackState.PLAYER_LOST);
|
||||
updateUserBalance(game, false);
|
||||
} else if (mainHandValue <= 21 && splitHandValue <= 21) {
|
||||
game.setState(BlackJackState.IN_PROGRESS);
|
||||
} else {
|
||||
game.setState(BlackJackState.IN_PROGRESS);
|
||||
}
|
||||
} else {
|
||||
game.setState(getState(game));
|
||||
|
||||
if (game.getState() == BlackJackState.PLAYER_WON) {
|
||||
updateUserBalance(game, true);
|
||||
} else if (game.getState() == BlackJackState.PLAYER_LOST) {
|
||||
updateUserBalance(game, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void determineWinnerAndUpdateBalance(BlackJackGameEntity game) {
|
||||
int playerValue = calculateHandValue(game.getPlayerCards());
|
||||
int dealerValue = calculateHandValue(game.getDealerCards());
|
||||
|
||||
if (dealerValue > 21 || playerValue > dealerValue) {
|
||||
game.setState(BlackJackState.PLAYER_WON);
|
||||
updateUserBalance(game, true);
|
||||
} else if (playerValue < dealerValue) {
|
||||
game.setState(BlackJackState.PLAYER_LOST);
|
||||
updateUserBalance(game, false);
|
||||
} else {
|
||||
game.setState(BlackJackState.DRAW);
|
||||
updateUserBalance(game, false);
|
||||
}
|
||||
}
|
||||
|
||||
private void deductBetFromBalance(UserEntity user, BigDecimal betAmount) {
|
||||
user.setBalance(user.getBalance().subtract(betAmount));
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
private void updateUserBalance(BlackJackGameEntity game, boolean isWin) {
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
BigDecimal totalBet = game.getBet();
|
||||
BigDecimal balance = user.getBalance();
|
||||
|
||||
if (game.isSplit()) {
|
||||
totalBet = totalBet.add(game.getSplitBet());
|
||||
|
||||
if (isWin) {
|
||||
int mainHandValue = calculateHandValue(game.getPlayerCards());
|
||||
int splitHandValue = calculateHandValue(game.getPlayerSplitCards());
|
||||
int dealerValue = calculateHandValue(game.getDealerCards());
|
||||
|
||||
if (mainHandValue <= 21 && (dealerValue > 21 || mainHandValue > dealerValue)) {
|
||||
balance = balance.add(game.getBet().multiply(BigDecimal.valueOf(2)));
|
||||
} else if (mainHandValue == dealerValue) {
|
||||
balance = balance.add(game.getBet());
|
||||
}
|
||||
|
||||
if (splitHandValue <= 21 && (dealerValue > 21 || splitHandValue > dealerValue)) {
|
||||
balance = balance.add(game.getSplitBet().multiply(BigDecimal.valueOf(2)));
|
||||
} else if (splitHandValue == dealerValue) {
|
||||
balance = balance.add(game.getSplitBet());
|
||||
}
|
||||
} else if (game.getState() == BlackJackState.DRAW) {
|
||||
balance = balance.add(totalBet);
|
||||
}
|
||||
} else {
|
||||
if (isWin) {
|
||||
balance = balance.add(totalBet.multiply(BigDecimal.valueOf(2)));
|
||||
} else if (game.getState() == BlackJackState.DRAW) {
|
||||
balance = balance.add(totalBet);
|
||||
}
|
||||
}
|
||||
|
||||
user.setBalance(balance);
|
||||
userRepository.save(user);
|
||||
}
|
||||
|
||||
private void initializeDeck(BlackJackGameEntity game) {
|
||||
for (Suit suit : Suit.values()) {
|
||||
for (Rank rank : Rank.values()) {
|
||||
CardEntity card = new CardEntity();
|
||||
card.setGame(game);
|
||||
card.setSuit(suit);
|
||||
card.setRank(rank);
|
||||
card.setCardType(CardType.DECK);
|
||||
game.getDeck().add(card);
|
||||
}
|
||||
}
|
||||
|
||||
java.util.Collections.shuffle(game.getDeck(), random);
|
||||
}
|
||||
|
||||
private CardEntity drawCardFromDeck(BlackJackGameEntity game) {
|
||||
if (game.getDeck().isEmpty()) {
|
||||
throw new IllegalStateException("Deck is empty");
|
||||
}
|
||||
|
||||
return game.getDeck().removeFirst();
|
||||
}
|
||||
|
||||
private BlackJackState getState(BlackJackGameEntity game) {
|
||||
int playerHandValue = calculateHandValue(game.getPlayerCards());
|
||||
|
||||
if (playerHandValue == 21) {
|
||||
CardEntity hole = drawCardFromDeck(game);
|
||||
hole.setCardType(CardType.DEALER);
|
||||
game.getDealerCards().add(hole);
|
||||
|
||||
int dealerHandValue = calculateHandValue(game.getDealerCards());
|
||||
|
||||
if (dealerHandValue == 21) {
|
||||
return BlackJackState.DRAW;
|
||||
} else {
|
||||
BigDecimal blackjackWinnings = game.getBet().multiply(new BigDecimal("1.5"));
|
||||
UserEntity user = getUserWithFreshData(game.getUser());
|
||||
user.setBalance(user.getBalance().add(blackjackWinnings));
|
||||
return BlackJackState.PLAYER_BLACKJACK;
|
||||
}
|
||||
} else if (playerHandValue > 21) {
|
||||
return BlackJackState.PLAYER_LOST;
|
||||
}
|
||||
|
||||
return BlackJackState.IN_PROGRESS;
|
||||
}
|
||||
|
||||
private int calculateHandValue(List<CardEntity> hand) {
|
||||
int sum = 0;
|
||||
int aceCount = 0;
|
||||
for (CardEntity card : hand) {
|
||||
sum += card.getRank().getValue();
|
||||
if (card.getRank() == Rank.ACE) {
|
||||
aceCount++;
|
||||
}
|
||||
}
|
||||
|
||||
while (sum > 21 && aceCount > 0) {
|
||||
sum -= 10;
|
||||
aceCount--;
|
||||
}
|
||||
|
||||
return sum;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,5 +36,5 @@ public class CardEntity {
|
|||
}
|
||||
|
||||
enum CardType {
|
||||
DECK, PLAYER, DEALER
|
||||
DECK, PLAYER, DEALER, PLAYER_SPLIT
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue