deck.Strip -- remove cards

Given a function that identifies cards to remove, remove them from the deck. This can be used to find and strip out cards that are only conditionally appropriate (and aren't appropriate anymore).
This commit is contained in:
Kistaro Windrider 2023-04-01 19:31:50 -07:00
parent b806264154
commit 222d4375ee
Signed by: kistaro
SSH Key Fingerprint: SHA256:TBE2ynfmJqsAf0CP6gsflA0q5X5wD5fVKWPsZ7eVUg8

View File

@ -264,9 +264,33 @@ func (d *Deck[C]) ShuffleTop(frac float64) error {
return d.ShuffleRange(0.0, frac) return d.ShuffleRange(0.0, frac)
} }
// Shufflebottom uses ShuffleRange to shuffle the bottom frac (between 0.0 and // ShuffleBottom uses ShuffleRange to shuffle the bottom frac (between 0.0 and
// 1.0) of the deck. See ShuffleRange and ShufflePart for information on // 1.0) of the deck. See ShuffleRange and ShufflePart for information on
// rounding and warnings. // rounding and warnings.
func (d *Deck[C]) ShuffleBottom(frac float64) error { func (d *Deck[C]) ShuffleBottom(frac float64) error {
return d.ShuffleRange(frac, 1.0) return d.ShuffleRange(frac, 1.0)
} }
// Strip removes all cards from the deck where shouldRemove returns true.
// shouldRemove is provided with each card in the deck and its index.
// It returns how many cards were stripped from the deck.
func (d *Deck[C]) Strip(shouldRemove func(idx int, c Card[C]) bool) int {
stripped := 0
startStripRange := -1
for i := 0; i < len(d.cards); i++ {
if shouldRemove(i, d.cards[i]) {
stripped++
if startStripRange < 0 {
startStripRange = i
}
} else if startStripRange >= 0 {
d.cards = DeleteNFrom(d.cards, startStripRange, i-startStripRange)
i = startStripRange
startStripRange = -1
}
}
if startStripRange >= 0 {
d.cards = d.cards[:startStripRange]
}
return stripped
}