feat(hand): enforce limits on plays, discards and index validation
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
package exceptions
|
||||||
|
|
||||||
|
class InvalidCardIndexException(message: String = "Invalid card index.") extends Exception(message)
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
package exceptions
|
||||||
|
|
||||||
|
class InvalidJokerIndexException(message: String = "Invalid joker index.") extends Exception(message)
|
||||||
3
src/main/scala/exceptions/InvalidPlaySizeException.scala
Normal file
3
src/main/scala/exceptions/InvalidPlaySizeException.scala
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
package exceptions
|
||||||
|
|
||||||
|
class InvalidPlaySizeException(message: String = "Must play between 1 and 5 cards.") extends Exception(message)
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package exeptions
|
package exceptions
|
||||||
|
|
||||||
class TooManyCardsException(message: String = "Cannot add more than 8 cards to the hand.")
|
class TooManyCardsException(message: String = "Cannot add more than 8 cards to the hand.")
|
||||||
extends Exception(message)
|
extends Exception(message)
|
||||||
3
src/main/scala/exceptions/TooManyDiscardsException.scala
Normal file
3
src/main/scala/exceptions/TooManyDiscardsException.scala
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
package exceptions
|
||||||
|
|
||||||
|
class TooManyDiscardsException(message: String = "Cannot discard more than 3 times.") extends Exception(message)
|
||||||
4
src/main/scala/exceptions/TooManyJokersException.scala
Normal file
4
src/main/scala/exceptions/TooManyJokersException.scala
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
package exceptions
|
||||||
|
|
||||||
|
class TooManyJokersException(message: String = "Cannot add more than 2 jokers to the hand.")
|
||||||
|
extends Exception(message)
|
||||||
3
src/main/scala/exceptions/TooManyPlaysException.scala
Normal file
3
src/main/scala/exceptions/TooManyPlaysException.scala
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
package exceptions
|
||||||
|
|
||||||
|
class TooManyPlaysException(message: String = "Cannot play more than 3 times.") extends Exception(message)
|
||||||
@@ -2,8 +2,9 @@ package hand
|
|||||||
|
|
||||||
import cards.Card
|
import cards.Card
|
||||||
import jokers.Joker
|
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.
|
* Represents a Hand in Balatro, containing Cards and Jokers.
|
||||||
* Allows adding/removing cards and jokers by index, and playing cards.
|
* Allows adding/removing cards and jokers by index, and playing cards.
|
||||||
@@ -11,6 +12,8 @@ import scala.collection.mutable.ListBuffer
|
|||||||
class Hand {
|
class Hand {
|
||||||
private val cards = ListBuffer[Card]()
|
private val cards = ListBuffer[Card]()
|
||||||
private val jokers = ListBuffer[Joker]()
|
private val jokers = ListBuffer[Joker]()
|
||||||
|
private var playCount = 0
|
||||||
|
private var discardCount = 0
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a Card to the hand.
|
* Adds a Card to the hand.
|
||||||
@@ -19,6 +22,9 @@ class Hand {
|
|||||||
* @param card The Card object to add.
|
* @param card The Card object to add.
|
||||||
*/
|
*/
|
||||||
def addCard(card: Card): Unit = {
|
def addCard(card: Card): Unit = {
|
||||||
|
if (cards.length >= 8){
|
||||||
|
throw new TooManyCardsException()
|
||||||
|
}
|
||||||
cards += card
|
cards += card
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,8 +40,7 @@ class Hand {
|
|||||||
if (index >= 0 && index < cards.length) {
|
if (index >= 0 && index < cards.length) {
|
||||||
cards.remove(index)
|
cards.remove(index)
|
||||||
} else {
|
} else {
|
||||||
// As per requirements, we don't need to strictly handle invalid indices for the final submission.
|
throw new InvalidCardIndexException(s"Index $index out of bounds for cards (size: ${cards.length})")
|
||||||
println(s"Warning: Attempted to remove card at invalid index $index. Hand size: ${cards.length}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,6 +51,9 @@ class Hand {
|
|||||||
* @param joker The Joker object to add.
|
* @param joker The Joker object to add.
|
||||||
*/
|
*/
|
||||||
def addJoker(joker: Joker): Unit = {
|
def addJoker(joker: Joker): Unit = {
|
||||||
|
if (jokers.length >=2){
|
||||||
|
throw new TooManyJokersException()
|
||||||
|
}
|
||||||
jokers += joker
|
jokers += joker
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,8 +69,7 @@ class Hand {
|
|||||||
if (index >= 0 && index < jokers.length) {
|
if (index >= 0 && index < jokers.length) {
|
||||||
jokers.remove(index)
|
jokers.remove(index)
|
||||||
} else {
|
} else {
|
||||||
// As per requirements, we don't need to strictly handle invalid indices for the final submission.
|
throw new InvalidJokerIndexException(s"Index $index out of bounds for jokers (size: ${jokers.length})")
|
||||||
println(s"Warning: Attempted to remove joker at invalid index $index. Joker count: ${jokers.length}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -76,14 +83,44 @@ class Hand {
|
|||||||
* @return A list containing the Card objects that were played (and thus removed).
|
* @return A list containing the Card objects that were played (and thus removed).
|
||||||
*/
|
*/
|
||||||
def playCards(indices: List[Int]): List[Card] = {
|
def playCards(indices: List[Int]): List[Card] = {
|
||||||
// Assumes indices are valid and within bounds as per requirements.
|
if (playCount >= 3) throw new TooManyPlaysException()
|
||||||
val cardsToPlay = indices.map(index => cards(index)).toList
|
if (indices.size < 1 || indices.size > 5) throw new InvalidPlaySizeException()
|
||||||
// Removes the selected cards from the hand.
|
|
||||||
|
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
|
cards --= cardsToPlay
|
||||||
// Returns the list of cards that were played.
|
playCount += 1
|
||||||
cardsToPlay
|
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.
|
* Provides an immutable view of the cards currently in the hand.
|
||||||
* Useful for displaying the hand or passing it to other components
|
* Useful for displaying the hand or passing it to other components
|
||||||
@@ -107,6 +144,14 @@ class Hand {
|
|||||||
*/
|
*/
|
||||||
def setCards(newCards: List[Card]): Unit = {
|
def setCards(newCards: List[Card]): Unit = {
|
||||||
cards.clear()
|
cards.clear()
|
||||||
|
try{
|
||||||
|
newCards.foreach(addCard)
|
||||||
|
} catch {
|
||||||
|
case e: TooManyCardsException =>
|
||||||
|
println(s"Failed to set cards: ${e.getMessage}")
|
||||||
|
cards.clear()
|
||||||
|
throw e
|
||||||
|
}
|
||||||
cards ++= newCards
|
cards ++= newCards
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -117,7 +162,18 @@ class Hand {
|
|||||||
* @param newJokers A list of Joker objects to set as the jokers in hand.
|
* @param newJokers A list of Joker objects to set as the jokers in hand.
|
||||||
*/
|
*/
|
||||||
def setJokers(newJokers: List[Joker]): Unit = {
|
def setJokers(newJokers: List[Joker]): Unit = {
|
||||||
|
|
||||||
jokers.clear()
|
jokers.clear()
|
||||||
|
try{
|
||||||
|
newJokers.foreach(addJoker)
|
||||||
|
|
||||||
|
}catch{
|
||||||
|
case e: TooManyJokersException=>
|
||||||
|
println(s"Failed to set jokers: ${e.getMessage}")
|
||||||
|
jokers.clear()
|
||||||
|
throw e
|
||||||
|
}
|
||||||
|
|
||||||
jokers ++= newJokers
|
jokers ++= newJokers
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user