Reputation: 369
So I have a Shuffle method that shuffles a deck of cards. Each card has a dealt field that will be either true/false depending on if it has been dealt or not.
The shuffle method sets each cards dealt field to false. I'm trying to stop it from setting certain cards to false, depending on if the card is currently in the dealers/players hand.
public void shuffle(){
Collections.shuffle(deck);//Shuffles the deck
for(Card card : deck){//For each card in the deck
if(!Game.playersHand.contains(card) || !Game.dealersHand.contains(card)){//If card is not in dealers/players hand
card.setDealt(false);//card is no longer dealt
}//end if
}//end for
}//end shuffle
The above if statement does not work. It will mark every card as false, regardless.
If I do:
public void shuffle(){
Collections.shuffle(deck);
for(Card card : deck){
if(!Game.playersHand.contains(card)){
card.setDealt(false);
}
}
}//end shuffle
It will prevent the card's dealt field in the playersHand from being set to false.
I'm not sure why my || operand is not working in this case.
Upvotes: 0
Views: 62
Reputation: 21
By De Morgan's law, (!Game.playersHand.contains(card) || !Game.dealersHand.contains(card)) is equivalent to !(Game.playersHand.contains(card) && Game.dealersHand.contains(card)).
The truth table for this (shown below) is only false when both elements are true; In a mutually exclusive case like this, this means it always evaluates to true.
Truth table for: (!Game.playersHand.contains(card) || !Game.dealersHand.contains(card)) is:
A...B...!A...!B...(!A||!B)
F...F....T....T......T
F...T....T....F......T
T...F....F....T......T
T...T....F....F......F
The truth table for !(Game.playersHand.contains(card) || Game.dealersHand.contains(card)) is:
A...B...(A||B)...!(A||B)
F...F......F........T
F...T......T........F
T...F......T........F
T...T......T........F
Looks like that is what you want to use.
Upvotes: 0
Reputation: 23329
The ||
operator is working fine, it could be that you misunderstood it
boolean x= !true || !false;
System.out.println(x); // prints true
So apparently && is what you looking for
boolean x= !true && !false;
System.out.println(x); // prints false
So in your case this would be
if(!Game.playersHand.contains(card) && !Game.dealersHand.contains(card)){
...
}
It would be easier if you negate the whole expression
if(!(Game.playersHand.contains(card) || Game.dealersHand.contains(card))){
...
}
Upvotes: 1
Reputation: 49803
It is working fine; but it is not the expression you want.
Suppose the card is in the player's hand; presumably it is NOT also in the dealer's hand. And visa-versa. That is: every card is not in some hand.
From your comment, it sounds like you want a and (&&
): the card should be in neither the player's nor the dealer's hand.
Upvotes: 0