feat: Implement player winning state at startup #98
1 changed files with 26 additions and 1 deletions
|
@ -5,6 +5,7 @@ import de.szut.casino.user.UserRepository;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
|
@ -23,7 +24,6 @@ public class BlackJackService {
|
||||||
BlackJackGameEntity game = new BlackJackGameEntity();
|
BlackJackGameEntity game = new BlackJackGameEntity();
|
||||||
game.setUser(user);
|
game.setUser(user);
|
||||||
game.setBet(betAmount);
|
game.setBet(betAmount);
|
||||||
game.setState(getState(game));
|
|
||||||
initializeDeck(game);
|
initializeDeck(game);
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
|
@ -34,6 +34,7 @@ public class BlackJackService {
|
||||||
CardEntity dealerCard = drawCardFromDeck(game);
|
CardEntity dealerCard = drawCardFromDeck(game);
|
||||||
game.getDealerCards().add(dealerCard);
|
game.getDealerCards().add(dealerCard);
|
||||||
|
|
||||||
|
game.setState(getState(game));
|
||||||
user.setBalance(user.getBalance().subtract(betAmount));
|
user.setBalance(user.getBalance().subtract(betAmount));
|
||||||
|
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
|
@ -65,6 +66,30 @@ public class BlackJackService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private BlackJackState getState(BlackJackGameEntity game) {
|
private BlackJackState getState(BlackJackGameEntity game) {
|
||||||
|
int playerHandValue = calculateHandValue(game.getPlayerCards());
|
||||||
|
|
||||||
|
if (playerHandValue == 21) {
|
||||||
|
return BlackJackState.PLAYER_WON;
|
||||||
|
}
|
||||||
|
|
||||||
return BlackJackState.IN_PROGRESS;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue