feat(hand): enforce limits on plays, discards and index validation

This commit is contained in:
2025-05-26 01:27:43 -04:00
parent a93a377972
commit 9885cf071b
8 changed files with 85 additions and 10 deletions

View File

@@ -0,0 +1,3 @@
package exceptions
class InvalidCardIndexException(message: String = "Invalid card index.") extends Exception(message)

View File

@@ -0,0 +1,3 @@
package exceptions
class InvalidJokerIndexException(message: String = "Invalid joker index.") extends Exception(message)

View File

@@ -0,0 +1,3 @@
package exceptions
class InvalidPlaySizeException(message: String = "Must play between 1 and 5 cards.") extends Exception(message)

View File

@@ -1,4 +1,4 @@
package exeptions
package exceptions
class TooManyCardsException(message: String = "Cannot add more than 8 cards to the hand.")
extends Exception(message)

View File

@@ -0,0 +1,3 @@
package exceptions
class TooManyDiscardsException(message: String = "Cannot discard more than 3 times.") extends Exception(message)

View File

@@ -0,0 +1,4 @@
package exceptions
class TooManyJokersException(message: String = "Cannot add more than 2 jokers to the hand.")
extends Exception(message)

View File

@@ -0,0 +1,3 @@
package exceptions
class TooManyPlaysException(message: String = "Cannot play more than 3 times.") extends Exception(message)

View File

@@ -2,8 +2,9 @@ package hand
import cards.Card
import jokers.Joker
import scala.collection.mutable.ListBuffer
import scala.collection.mutable.ListBuffer
import exceptions.{InvalidCardIndexException, InvalidJokerIndexException, InvalidPlaySizeException, TooManyCardsException, TooManyDiscardsException, TooManyJokersException, TooManyPlaysException}
/**
* Represents a Hand in Balatro, containing Cards and Jokers.
* Allows adding/removing cards and jokers by index, and playing cards.
@@ -11,6 +12,8 @@ import scala.collection.mutable.ListBuffer
class Hand {
private val cards = ListBuffer[Card]()
private val jokers = ListBuffer[Joker]()
private var playCount = 0
private var discardCount = 0
/**
* Adds a Card to the hand.
@@ -19,6 +22,9 @@ class Hand {
* @param card The Card object to add.
*/
def addCard(card: Card): Unit = {
if (cards.length >= 8){
throw new TooManyCardsException()
}
cards += card
}
@@ -34,8 +40,7 @@ class Hand {
if (index >= 0 && index < cards.length) {
cards.remove(index)
} else {
// As per requirements, we don't need to strictly handle invalid indices for the final submission.
println(s"Warning: Attempted to remove card at invalid index $index. Hand size: ${cards.length}")
throw new InvalidCardIndexException(s"Index $index out of bounds for cards (size: ${cards.length})")
}
}
@@ -46,6 +51,9 @@ class Hand {
* @param joker The Joker object to add.
*/
def addJoker(joker: Joker): Unit = {
if (jokers.length >=2){
throw new TooManyJokersException()
}
jokers += joker
}
@@ -61,8 +69,7 @@ class Hand {
if (index >= 0 && index < jokers.length) {
jokers.remove(index)
} else {
// As per requirements, we don't need to strictly handle invalid indices for the final submission.
println(s"Warning: Attempted to remove joker at invalid index $index. Joker count: ${jokers.length}")
throw new InvalidJokerIndexException(s"Index $index out of bounds for jokers (size: ${jokers.length})")
}
}
@@ -76,14 +83,44 @@ class Hand {
* @return A list containing the Card objects that were played (and thus removed).
*/
def playCards(indices: List[Int]): List[Card] = {
// Assumes indices are valid and within bounds as per requirements.
val cardsToPlay = indices.map(index => cards(index)).toList
// Removes the selected cards from the hand.
if (playCount >= 3) throw new TooManyPlaysException()
if (indices.size < 1 || indices.size > 5) throw new InvalidPlaySizeException()
if (!indices.forall(i => i >= 0 && i < cards.length)) {
throw new InvalidCardIndexException("Some indices are out of range.")
}
val cardsToPlay = indices.map(cards)
cards --= cardsToPlay
// Returns the list of cards that were played.
playCount += 1
cardsToPlay
}
/**
* Discards cards from the hand based on a list of indices.
* Validates that the number of discards is between 1 and 5,
* and that the operation has not been performed more than 3 times.
* Also checks that all indices are valid and in range.
*
* @param indices A list of zero-based indices of the cards to discard.
* @return A list of Card objects that were discarded.
* @throws TooManyDiscardsException If more than 3 discards have been attempted.
* @throws InvalidPlaySizeException If fewer than 1 or more than 5 cards are discarded.
* @throws InvalidCardIndexException If any index is out of range.
*/
def discardCards(indices: List[Int]): List[Card] = {
if (discardCount >= 3) throw new TooManyDiscardsException()
if (indices.size < 1 || indices.size > 5) throw new InvalidPlaySizeException("Must discard between 1 and 5 cards.")
if (!indices.forall(i => i >= 0 && i < cards.length)) {
throw new InvalidCardIndexException("Some indices are out of range.")
}
val discarded = indices.map(cards)
cards --= discarded
discardCount += 1
discarded
}
/**
* Provides an immutable view of the cards currently in the hand.
* Useful for displaying the hand or passing it to other components
@@ -107,6 +144,14 @@ class Hand {
*/
def setCards(newCards: List[Card]): Unit = {
cards.clear()
try{
newCards.foreach(addCard)
} catch {
case e: TooManyCardsException =>
println(s"Failed to set cards: ${e.getMessage}")
cards.clear()
throw e
}
cards ++= newCards
}
@@ -117,7 +162,18 @@ class Hand {
* @param newJokers A list of Joker objects to set as the jokers in hand.
*/
def setJokers(newJokers: List[Joker]): Unit = {
jokers.clear()
try{
newJokers.foreach(addJoker)
}catch{
case e: TooManyJokersException=>
println(s"Failed to set jokers: ${e.getMessage}")
jokers.clear()
throw e
}
jokers ++= newJokers
}
/**