Compare commits
No commits in common. "v1.19.0" and "v1.18.0" have entirely different histories.
3 changed files with 2 additions and 38 deletions
|
@ -33,8 +33,7 @@ public class BlackJackGameEntity {
|
|||
return user != null ? user.getId() : null;
|
||||
}
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private BlackJackState state;
|
||||
private String state;
|
||||
private BigDecimal bet;
|
||||
|
||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
|
|
|
@ -5,7 +5,6 @@ import de.szut.casino.user.UserRepository;
|
|||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
@Service
|
||||
|
@ -24,6 +23,7 @@ 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,7 +34,6 @@ public class BlackJackService {
|
|||
CardEntity dealerCard = drawCardFromDeck(game);
|
||||
game.getDealerCards().add(dealerCard);
|
||||
|
||||
game.setState(getState(game));
|
||||
user.setBalance(user.getBalance().subtract(betAmount));
|
||||
|
||||
userRepository.save(user);
|
||||
|
@ -64,32 +63,4 @@ 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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
package de.szut.casino.blackjack;
|
||||
|
||||
public enum BlackJackState {
|
||||
PLAYER_WON,
|
||||
IN_PROGRESS
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue