Compare commits
No commits in common. "v1.18.0" and "v1.17.0" have entirely different histories.
3 changed files with 9 additions and 33 deletions
|
@ -67,11 +67,6 @@ cd backend
|
||||||
./gradlew bootRun
|
./gradlew bootRun
|
||||||
```
|
```
|
||||||
|
|
||||||
You may optionally install [watchexec](https://github.com/watchexec/watchexec?tab=readme-ov-file) and use this command to autorecompile the backend on file changes:
|
|
||||||
```bash
|
|
||||||
watchexec -r -e java ./gradlew :bootRun
|
|
||||||
```
|
|
||||||
|
|
||||||
The backend will be available at:
|
The backend will be available at:
|
||||||
- API endpoint: http://localhost:8080
|
- API endpoint: http://localhost:8080
|
||||||
- Swagger documentation: http://localhost:8080/swagger
|
- Swagger documentation: http://localhost:8080/swagger
|
||||||
|
|
|
@ -36,10 +36,6 @@ public class BlackJackGameEntity {
|
||||||
private String state;
|
private String state;
|
||||||
private BigDecimal bet;
|
private BigDecimal bet;
|
||||||
|
|
||||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
|
||||||
@JsonIgnore
|
|
||||||
private List<CardEntity> deck = new ArrayList<>();
|
|
||||||
|
|
||||||
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
|
||||||
@JsonManagedReference
|
@JsonManagedReference
|
||||||
private List<CardEntity> playerCards = new ArrayList<>();
|
private List<CardEntity> playerCards = new ArrayList<>();
|
||||||
|
|
|
@ -24,14 +24,13 @@ public class BlackJackService {
|
||||||
game.setUser(user);
|
game.setUser(user);
|
||||||
game.setBet(betAmount);
|
game.setBet(betAmount);
|
||||||
game.setState("IN_PROGRESS");
|
game.setState("IN_PROGRESS");
|
||||||
initializeDeck(game);
|
|
||||||
|
|
||||||
for (int i = 0; i < 2; i++) {
|
for (int i = 0; i < 2; i++) {
|
||||||
CardEntity playerCard = drawCardFromDeck(game);
|
CardEntity playerCard = createRandomCard(game);
|
||||||
game.getPlayerCards().add(playerCard);
|
game.getPlayerCards().add(playerCard);
|
||||||
}
|
}
|
||||||
|
|
||||||
CardEntity dealerCard = drawCardFromDeck(game);
|
CardEntity dealerCard = createRandomCard(game);
|
||||||
game.getDealerCards().add(dealerCard);
|
game.getDealerCards().add(dealerCard);
|
||||||
|
|
||||||
user.setBalance(user.getBalance().subtract(betAmount));
|
user.setBalance(user.getBalance().subtract(betAmount));
|
||||||
|
@ -42,25 +41,11 @@ public class BlackJackService {
|
||||||
return game;
|
return game;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initializeDeck(BlackJackGameEntity game) {
|
private CardEntity createRandomCard(BlackJackGameEntity game) {
|
||||||
for (Suit suit : Suit.values()) {
|
CardEntity card = new CardEntity();
|
||||||
for (Rank rank : Rank.values()) {
|
card.setGame(game);
|
||||||
CardEntity card = new CardEntity();
|
card.setSuit(Suit.values()[random.nextInt(Suit.values().length)]);
|
||||||
card.setGame(game);
|
card.setRank(Rank.values()[random.nextInt(Rank.values().length)]);
|
||||||
card.setSuit(suit);
|
return card;
|
||||||
card.setRank(rank);
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue