fixed failing test, due to wrong assertions or missing messages, as well as rewriting the 'addBonusCards()' method in Game to not use duplicates

This commit is contained in:
Hanno Fleischer
2024-12-13 14:00:54 +01:00
parent e3ec2aaba8
commit 3f07400cf8
3 changed files with 21 additions and 3 deletions

View File

@@ -5,6 +5,8 @@
import pp.mdga.game.card.SwapCard;
import pp.mdga.game.card.TurboCard;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
/**
@@ -134,7 +136,20 @@ public PowerCard draw() {
* @param count the number of cards to add
*/
private void addBonusCards(PowerCard card, int count) {
drawPile.addAll(Collections.nCopies(count, card));
Class<? extends PowerCard> cardClass = card.getClass();
try {
// Get the constructor for the PowerCard class
Constructor<? extends PowerCard> constructor = cardClass.getDeclaredConstructor();
constructor.setAccessible(true); // Necessary if the constructor is not public
for (int i = 0; i < count; i++) {
// Create a new instance of the PowerCard
PowerCard newCard = constructor.newInstance();
this.drawPile.add(newCard);
}
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace(); // Log any issues with reflection
}
}
/**