I am coding a solitary game of Elevens where the goal is to use up all the cards by making sums of 11 with 2 number cards or a set of J, Q, K with 3 cards. The layout is a row of JButtons representing cards. The problem I'm encountering is deleting the used JButtons when the deck has no more cards. I can only seem to make the JButtons fade as if I setEnabled(false) even if I call the method remove() on the JButton. Help greatly appreciated.
Here's my relevant code:
The odd thing is if I had the layout of 9 cards and 1 remaining card in the deck, and I match 2 number cards to make 11, the layout will decrease to 8, removing 1 of the JButtons, but when I continue to finish off the game with no more cards in the deck, the cards used to create a sum of 11 do not remove themselves like before. It's not a problem of the deal() method because I've tested it and I'm sure it returns null after the 52nd card.
public class CardButton extends JButton
{
private PlayingCard theCard;
private int position;
private Layout layout;
public CardButton(PlayingCard card, int pos, Layout lay)
{
super(card.faceIcon);
theCard = card;
setIcon(card.faceIcon);
position = pos;
layout = lay;
}
public Card getCard()
{
return theCard;
}
public void setCard(PlayingCard card)
{
if(card==null){ // I'm trying to get this to work. I have a deal method in my Deck class that returns null if the cardCount is past 52 in which I pass to the parameter of this method.
layout.remove(layout.getLay()[position]);
}else{
PlayingCard temp = card;
setIcon(temp.faceIcon);
theCard.setValue(temp.getValue());
theCard.setFace(temp.getFace());
theCard.setSuit(temp.getSuit());
}
}
}
