Compare commits

...

3 commits

Author SHA1 Message Date
deb5b6525f Merge pull request 'feat: Implement player winning state at startup' (!98) from feat/state-enum into main
All checks were successful
Release / Release (push) Successful in 48s
Reviewed-on: #98
Reviewed-by: Jan K9f <jan@kjan.email>
2025-03-27 11:49:37 +00:00
Phan Huy Tran
90abd13a2c feat: handle player drawing blackjack at the start of the game
All checks were successful
CI / Get Changed Files (pull_request) Successful in 6s
CI / eslint (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
CI / Checkstyle Main (pull_request) Successful in 36s
2025-03-27 12:45:52 +01:00
Phan Huy Tran
caa210a80e refactor: add blackjack state enum 2025-03-27 12:08:57 +01:00
3 changed files with 38 additions and 2 deletions

View file

@ -33,7 +33,8 @@ public class BlackJackGameEntity {
return user != null ? user.getId() : null;
}
private String state;
@Enumerated(EnumType.STRING)
private BlackJackState state;
private BigDecimal bet;
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)

View file

@ -5,6 +5,7 @@ import de.szut.casino.user.UserRepository;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;
import java.util.Random;
@Service
@ -23,7 +24,6 @@ public class BlackJackService {
BlackJackGameEntity game = new BlackJackGameEntity();
game.setUser(user);
game.setBet(betAmount);
game.setState("IN_PROGRESS");
initializeDeck(game);
for (int i = 0; i < 2; i++) {
@ -34,6 +34,7 @@ public class BlackJackService {
CardEntity dealerCard = drawCardFromDeck(game);
game.getDealerCards().add(dealerCard);
game.setState(getState(game));
user.setBalance(user.getBalance().subtract(betAmount));
userRepository.save(user);
@ -63,4 +64,32 @@ public class BlackJackService {
return game.getDeck().removeFirst();
}
private BlackJackState getState(BlackJackGameEntity game) {
int playerHandValue = calculateHandValue(game.getPlayerCards());
if (playerHandValue == 21) {
return BlackJackState.PLAYER_WON;
}
return BlackJackState.IN_PROGRESS;
}
public 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;
}
}

View file

@ -0,0 +1,6 @@
package de.szut.casino.blackjack;
public enum BlackJackState {
PLAYER_WON,
IN_PROGRESS
}