Compare commits

...

4 commits

Author SHA1 Message Date
4b23f3c67f Merge pull request 'feat: add deck to blackjack game instead of random cards' (!96) from feat/deck into main
All checks were successful
Release / Release (push) Successful in 50s
Reviewed-on: #96
Reviewed-by: Jan K9f <jan@kjan.email>
2025-03-27 10:54:49 +00:00
4f6add087b Merge pull request 'docs: add optional watchexec command to README.md' (!95) from add-some-docs into main
Reviewed-on: #95
Reviewed-by: Phan Huy Tran <ptran@noreply.localhost>
2025-03-27 10:42:27 +00:00
Phan Huy Tran
55daca72c0 feat: add deck to blackjack game instead of random cards
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 1m24s
2025-03-27 11:40:50 +01:00
71cda97dab
docs: add optional watchexec command to README.md
All checks were successful
CI / Get Changed Files (pull_request) Successful in 25s
CI / Checkstyle Main (pull_request) Has been skipped
CI / eslint (pull_request) Has been skipped
CI / prettier (pull_request) Has been skipped
CI / test-build (pull_request) Has been skipped
2025-03-27 10:47:46 +01:00
3 changed files with 33 additions and 9 deletions

View file

@ -67,6 +67,11 @@ cd backend
./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:
- API endpoint: http://localhost:8080
- Swagger documentation: http://localhost:8080/swagger
@ -164,4 +169,4 @@ References:
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

View file

@ -36,6 +36,10 @@ public class BlackJackGameEntity {
private String state;
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)
@JsonManagedReference
private List<CardEntity> playerCards = new ArrayList<>();

View file

@ -24,13 +24,14 @@ public class BlackJackService {
game.setUser(user);
game.setBet(betAmount);
game.setState("IN_PROGRESS");
initializeDeck(game);
for (int i = 0; i < 2; i++) {
CardEntity playerCard = createRandomCard(game);
CardEntity playerCard = drawCardFromDeck(game);
game.getPlayerCards().add(playerCard);
}
CardEntity dealerCard = createRandomCard(game);
CardEntity dealerCard = drawCardFromDeck(game);
game.getDealerCards().add(dealerCard);
user.setBalance(user.getBalance().subtract(betAmount));
@ -41,11 +42,25 @@ public class BlackJackService {
return game;
}
private CardEntity createRandomCard(BlackJackGameEntity game) {
CardEntity card = new CardEntity();
card.setGame(game);
card.setSuit(Suit.values()[random.nextInt(Suit.values().length)]);
card.setRank(Rank.values()[random.nextInt(Rank.values().length)]);
return card;
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);
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();
}
}