Hand.kt
Ricky/android/blackjack/app/src/main/java/ca/medontime/blackjack/domain/model/Hand.kt
package ca.medontime.blackjack.domain.model
/**
* Represents a hand of cards held by either player or dealer
*/
data class Hand(
val cards: MutableList<Card> = mutableListOf()
) {
/**
* Add a card to the hand
*/
fun addCard(card: Card) {
cards.add(card)
}
/**
* Clear all cards from the hand
*/
fun clear() {
cards.clear()
}
/**
* Get the total value of the hand according to BlackJack rules
* Automatically adjusts Ace values to prevent busting
*/
fun getTotalValue(): Int {
var total = 0
var aces = 0
// First pass: count all non-Ace cards
for (card in cards) {
if (card.rank == Rank.ACE) {
aces++
} else {
total += card.getValue()
}
}
// Second pass: handle Aces
for (i in 1..aces) {
if (total + 11 <= 21) {
total += 11
} else {
total += 1
}
}
return total
}
/**
* Check if the hand is busted (over 21)
*/
fun isBusted(): Boolean = getTotalValue() > 21
/**
* Check if the hand is a BlackJack (exactly 21 with 2 cards)
*/
fun isBlackJack(): Boolean = cards.size == 2 && getTotalValue() == 21
/**
* Check if the hand is empty
*/
fun isEmpty(): Boolean = cards.isEmpty()
/**
* Get the number of cards in the hand
*/
fun size(): Int = cards.size
}
Articles liés
__init__.py
__init__.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/__init__.py).
Lire l'article →auth.py
auth.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/auth.py).
Lire l'article →routes.py
routes.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/admin/routes.py).
Lire l'article →config.py
config.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/config.py).
Lire l'article →admin_ingest.py
admin_ingest.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_ingest.py).
Lire l'article →admin_stats.py
admin_stats.py — python source code from the Ricky learning materials (Ricky/Cat Project Final/back_web-4/app/routes/admin_stats.py).
Lire l'article →