Skip to content

Commit af7c7f1

Browse files
feat(exceptions): introduce custom exceptions + minor fixes (#68)
* feat(exceptions): introduce custom exceptions for community cards and poker game logic * feat(tests): update exception assertions to use custom exceptions in community cards and poker round tests * feat(tests): add test for card count validation boundary in HandEvaluator * fix(commands): catch general exceptions when creating a new game * feat(persistence): introduce custom exceptions for file and game loading errors * feat(interfaces): convert LoadGamePort and SaveGamePort to functional interfaces * feat(tests): added minor tests for game actions and state management * docs: update README to reflect command syntax changes for gameID * refactor(tests): remove redundant initial game info retrieval in GameBettingTest --------- Co-authored-by: dev-laky <laky.dev@proton.me>
1 parent 90e900e commit af7c7f1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1292
-425
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -153,20 +153,20 @@ Start with free 10$ and try to climb up the wealth ladder.
153153

154154
### Betting Commands
155155

156-
| Command | Description |
157-
|--------------------------------------------------|-----------------------|
158-
| `poker raise 10 --game=<gameID> --player=<name>` | Raise the bet by 10 |
159-
| `poker call --game=<gameID> --player=<name>` | Call the current bet |
160-
| `poker check --game=<gameID> --player=<name>` | Check (pass the turn) |
161-
| `poker fold --game=<gameID> --player=<name>` | Fold the hand |
162-
| `poker all-in --game=<gameID> --player=<name>` | Go all-in |
156+
| Command | Description |
157+
|----------------------------------------------------|-----------------------|
158+
| `poker raise 10 --gameID=<gameID> --player=<name>` | Raise the bet by 10 |
159+
| `poker call --gameID=<gameID> --player=<name>` | Call the current bet |
160+
| `poker check --gameID=<gameID> --player=<name>` | Check (pass the turn) |
161+
| `poker fold --gameID=<gameID> --player=<name>` | Fold the hand |
162+
| `poker all-in --gameID=<gameID> --player=<name>` | Go all-in |
163163

164164
### Info Commands
165165

166-
| Command | Description |
167-
|----------------------------------------------|-------------------------------|
168-
| `poker game-info --game=<gameID>` | Show current game information |
169-
| `poker hand --game=<gameID> --player=<name>` | Show the hand of the player |
166+
| Command | Description |
167+
|------------------------------------------------|-------------------------------|
168+
| `poker game-info --gameID=<gameID>` | Show current game information |
169+
| `poker hand --gameID=<gameID> --player=<name>` | Show the hand of the player |
170170

171171
[maven]: https://maven.apache.org/
172172

src/main/kotlin/hwr/oop/projects/peakpoker/commands/CreateNewGame.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.github.ajalt.clikt.parameters.options.convert
66
import com.github.ajalt.clikt.parameters.options.help
77
import com.github.ajalt.clikt.parameters.options.option
88
import com.github.ajalt.clikt.parameters.options.required
9-
import hwr.oop.projects.peakpoker.core.exceptions.GameException
109
import hwr.oop.projects.peakpoker.core.game.PokerGame
1110
import hwr.oop.projects.peakpoker.core.player.PokerPlayer
1211
import hwr.oop.projects.peakpoker.persistence.SaveGamePort
@@ -31,7 +30,7 @@ class CreateNewGame(private val saveGamePort: SaveGamePort) :
3130

3231
echo("Game ID: ${gameId.value}")
3332
echo("New game created with players: ${game.players.joinToString(", ") { it.name }}")
34-
} catch (e: GameException) {
33+
} catch (e: Exception) {
3534
echo("Error creating game: ${e.message}")
3635
}
3736
}

src/main/kotlin/hwr/oop/projects/peakpoker/core/card/CommunityCards.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,27 @@
11
package hwr.oop.projects.peakpoker.core.card
22

33
import hwr.oop.projects.peakpoker.core.deck.Deck
4-
import hwr.oop.projects.peakpoker.core.exceptions.DuplicateCardException
5-
import hwr.oop.projects.peakpoker.core.exceptions.InvalidCardConfigurationException
64
import hwr.oop.projects.peakpoker.core.game.RoundPhase
75
import kotlinx.serialization.Serializable
86

97
@Serializable
108
class CommunityCards(
119
private val cards: MutableList<Card> = mutableListOf(),
1210
) : Iterable<Card> by cards {
11+
/**
12+
* Exception thrown when duplicate cards are found in the community cards
13+
*/
14+
class DuplicateCardException(message: String) : IllegalStateException(message)
15+
16+
/**
17+
* Exception thrown when the number of community cards is invalid
18+
*/
19+
class InvalidCardConfigurationException(message: String) : IllegalStateException(message)
20+
21+
/**
22+
* Exception thrown when attempting to deal community cards in an invalid round phase
23+
*/
24+
class InvalidDealPhaseException(message: String) : IllegalStateException(message)
1325

1426
init {
1527
if (cards.isNotEmpty() && cards.size !in listOf(3, 4, 5)) {
@@ -22,7 +34,7 @@ class CommunityCards(
2234

2335
fun dealCommunityCards(roundPhase: RoundPhase, deck: Deck) {
2436
when (roundPhase) {
25-
RoundPhase.PRE_FLOP -> throw IllegalStateException("Cannot deal community cards before the flop")
37+
RoundPhase.PRE_FLOP -> throw InvalidDealPhaseException("Cannot deal community cards before the flop")
2638
RoundPhase.FLOP -> {
2739
cards.addAll(deck.draw(3))
2840
}
@@ -35,7 +47,7 @@ class CommunityCards(
3547
cards.addAll(deck.draw(1))
3648
}
3749

38-
RoundPhase.SHOWDOWN -> throw IllegalStateException("Cannot deal community cards after the showdown")
50+
RoundPhase.SHOWDOWN -> throw InvalidDealPhaseException("Cannot deal community cards after the showdown")
3951
}
4052
}
4153

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,30 @@
11
package hwr.oop.projects.peakpoker.core.card
22

3-
import hwr.oop.projects.peakpoker.core.exceptions.DuplicateCardException
4-
import hwr.oop.projects.peakpoker.core.exceptions.InvalidCardConfigurationException
53
import hwr.oop.projects.peakpoker.core.player.PokerPlayer
64
import kotlinx.serialization.Serializable
75
import kotlinx.serialization.Transient
86

97
@Serializable
108
class HoleCards(
11-
val cards: List<Card>,
12-
@Transient val player: PokerPlayer? = null,
9+
val cards: List<Card>,
10+
@Transient val player: PokerPlayer? = null,
1311
) : Iterable<Card> by cards {
14-
init {
15-
if (cards.isNotEmpty() && cards.size != 2) {
16-
throw InvalidCardConfigurationException("Hole cards must be empty or contain exactly two cards.")
17-
}
18-
if (cards.distinct().size != cards.size) {
19-
throw DuplicateCardException("Hole cards must not contain duplicates.")
12+
/**
13+
* Exception thrown when duplicate cards are found in the hole cards
14+
*/
15+
class DuplicateCardException(message: String) : IllegalStateException(message)
16+
17+
/**
18+
* Exception thrown when the number of hole cards is invalid
19+
*/
20+
class InvalidCardConfigurationException(message: String) : IllegalStateException(message)
21+
22+
init {
23+
if (cards.isNotEmpty() && cards.size != 2) {
24+
throw InvalidCardConfigurationException("Hole cards must be empty or contain exactly two cards.")
25+
}
26+
if (cards.distinct().size != cards.size) {
27+
throw DuplicateCardException("Hole cards must not contain duplicates.")
28+
}
2029
}
21-
}
2230
}

src/main/kotlin/hwr/oop/projects/peakpoker/core/card/Rank.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@ import kotlinx.serialization.Serializable
44

55
@Serializable
66
enum class Rank(
7-
val value: Int,
7+
val value: Int,
88
) {
9-
TWO(1),
10-
THREE(2),
11-
FOUR(3),
12-
FIVE(4),
13-
SIX(5),
14-
SEVEN(6),
15-
EIGHT(7),
16-
NINE(8),
17-
TEN(9),
18-
JACK(10),
19-
QUEEN(11),
20-
KING(12),
21-
ACE(13)
9+
TWO(1),
10+
THREE(2),
11+
FOUR(3),
12+
FIVE(4),
13+
SIX(5),
14+
SEVEN(6),
15+
EIGHT(7),
16+
NINE(8),
17+
TEN(9),
18+
JACK(10),
19+
QUEEN(11),
20+
KING(12),
21+
ACE(13)
2222
}

src/main/kotlin/hwr/oop/projects/peakpoker/core/card/Suit.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import kotlinx.serialization.Serializable
44

55
@Serializable
66
enum class Suit {
7-
HEARTS, DIAMONDS, CLUBS, SPADES
7+
HEARTS, DIAMONDS, CLUBS, SPADES
88
}

src/main/kotlin/hwr/oop/projects/peakpoker/core/deck/Deck.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package hwr.oop.projects.peakpoker.core.deck
33
import hwr.oop.projects.peakpoker.core.card.Card
44
import hwr.oop.projects.peakpoker.core.card.Rank
55
import hwr.oop.projects.peakpoker.core.card.Suit
6-
import hwr.oop.projects.peakpoker.core.exceptions.InsufficientCardsException
76
import kotlinx.serialization.Serializable
87
import kotlinx.serialization.Transient
98

109
@Serializable
1110
class Deck() {
11+
/**
12+
* Thrown when trying to draw more cards than are available in the deck
13+
*/
14+
class InsufficientCardsException(message: String) : IllegalStateException(message)
15+
1216
// Create a list of all possible cards and shuffle it right away
1317
@Transient
1418
private val cards: MutableList<Card> = Suit.entries.flatMap { suit ->

src/main/kotlin/hwr/oop/projects/peakpoker/core/exceptions/BettingException.kt

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/main/kotlin/hwr/oop/projects/peakpoker/core/exceptions/CardException.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/main/kotlin/hwr/oop/projects/peakpoker/core/exceptions/DeckException.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)