DIY java CrazyEights problem with drawing cards not adding to displayed hand

I've found every time there was a problem I typed something wrong, and that was the source of my error.  This time, I think though, the error is in the code, and not my fault.

When I find the answer, I will edit this post and display the fix at the bottom.

Here are the two methods that are not working together.  The game plays fine, but the hand will not display the drawn cards that are added to myhand when 'showStatus()' is called.

update:
Two fixes.  It is NOT what the author has. The Boolean isValidPlay(); is the problem.  I will test some more and find out why.  Amazing help from my genius sibling.  I try everything before asking for help, but this time I was dumbfounded.  Still haven't figured out why isValidPlay(); was deleting the cards from my hand.
update:
Found the culprit to the disappearing cards.  It was a remove statement in the 'contains()' function of Hands class.   It totally didn't belong there, no idea why I put it there. So what the book has will work now, and is right.  My bad.  Totally my fault.

FIX #1 -->
private void drawMyCard() {

  Card drewCard = deal();
  print();
  print("You drew the:   " + drewCard);
 
  if (isValidPlay(drewCard.toString())) {
  print("You played the  " + drewCard);
  discardMyCard(drewCard);

  }
  else {
   print("No match: added to hand.");
   myhand.add(drewCard);
  }
 }


FIX #2-->
private void drawMyCard() {

  Card drewCard = deal();
  print();
  print("You drew the:   " + drewCard);
  myhand.add(drewCard);
  if(drewCard.getSuit() == discard.getSuit() || 
    drewCard.getRank() == discard.getRank()) {
   discardMyCard(drewCard);
   print( "You played the  " + drewCard);
  }

 }


update:
I was wrong about the deal(); not working. The cards go into the discard pile when you play them, not when they are dealt... duh... Don't drink wine and code.  Also I should have run some tests to see what was going on.   System.out.println(); is an amazing tool to use!
There is also a fix with the 'CrazyEights.deal():'  that I fixed by using the deck.deal(); instead.  I will probably change that so there is only one 'deal()' method in the CrazyEights main class that deals the cards.  She has one, but it does not remove the cards from the deck, or add them to the discardPile for some reason. I will figure that out too and edit the post.


private void showStatus() {
  print();
  print("Computer # of cards =  " + computerHand.size());
  print("My hand =  " + myhand);
  print("Discard:  " + discard);
  if (discard.getRank()=='8') {
   print("Active suit =  " + activeSuit);

  }

private void drawMyCard() {
  Card drewCard = deck.deal();
  print();
  print("You drew the:   " + drewCard);
  myhand.add(drewCard);
  if (isValidPlay(drewCard.toString())) {
  print("You played the  " + drewCard);
  discardMyCard(drewCard);
  }







Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame