feat: validate game state on hit
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 1m45s

This commit is contained in:
Phan Huy Tran 2025-03-27 14:05:13 +01:00
parent d7690e630e
commit 4764c12909
3 changed files with 8 additions and 2 deletions

View file

@ -7,6 +7,6 @@ Content-Type: application/json
}
###
POST http://localhost:8080/blackjack/103/hit
POST http://localhost:8080/blackjack/202/hit
Authorization: Bearer {{token}}

View file

@ -40,6 +40,12 @@ public class BlackJackGameController {
return ResponseEntity.notFound().build();
}
if (game.getState() != BlackJackState.IN_PROGRESS) {
Map<String, String> errorResponse = new HashMap<>();
errorResponse.put("error", "Invalid state");
return ResponseEntity.badRequest().body(errorResponse);
}
return ResponseEntity.ok(blackJackService.hit(game));
}

View file

@ -94,7 +94,7 @@ public class BlackJackService {
return BlackJackState.IN_PROGRESS;
}
public int calculateHandValue(List<CardEntity> hand) {
private int calculateHandValue(List<CardEntity> hand) {
int sum = 0;
int aceCount = 0;
for (CardEntity card : hand) {