Re: Help setting values to cards in an array
Try debugging your code by adding println() statements to show execution progress and how variable values are changing. For example:
Add a: System.out.println("var=" + var);
after every line where a variable is changed by an assignment statement or read into.
Print out the value returned by getFace() and the values looked at in faces
I dont see how getCardValue() can return zero. It always adds one to value on line 10
Re: Help setting values to cards in an array
I'm still stuck at this point... I think I'm putting the wrong argument into the getHandValue() method. I'm not sure what to put in there..
Re: Help setting values to cards in an array
A method named getHandValue() should probably get a hand for an argument.
Where and how is a hand defined? Is there a Hand class? What values would a Hand object contain?
Re: Help setting values to cards in an array
I put print statements in the getCardValue() method and nothing printed.
Re: Help setting values to cards in an array
Is the method called from anywhere in the code?
If it is not called, then none of its code would execute.
Re: Help setting values to cards in an array
Quote:
Originally Posted by
Norm
A method named getHandValue() should probably get a hand for an argument.
Where and how is a hand defined? Is there a Hand class? What values would a Hand object contain?
This is my main args:
Code:
public static void main( String args[] )
{
DeckOfCards myDeckOfCards = new DeckOfCards();
myDeckOfCards.shuffle(); // place Cards in random order
Card[] hand = new Card[ 2 ]; // store two cards
// get first two cards
for ( int i = 0; i < 2; i++ )
{
hand[ i ] = myDeckOfCards.dealCard(); // get next card
System.out.println( hand[ i ] );
} // end for
myDeckOfCards.getHandValue(hand);
Re: Help setting values to cards in an array
That looks like getHandValue shoud be called. Does the code compile without errors? It shouldn't execute if there are compiler errors.
Re: Help setting values to cards in an array
Where would I call the getCardValue() method? Inside the getHandValue() method? Where inside would I put it?
Re: Help setting values to cards in an array
Quote:
Originally Posted by
Norm
That looks like getHandValue shoud be called. Does the code compile without errors? It shouldn't execute if there are compiler errors.
There are no errors right now. The total is just coming out as 0. But I haven't called my getCardValue() method anywhere yet.
Re: Help setting values to cards in an array
If you held a hand of cards, how would you manually sum up their values?
Re: Help setting values to cards in an array
Quote:
Originally Posted by
Norm
If you held a hand of cards, how would you manually sum up their values?
Find out their face values, then add them up. I know I call the getcardValue() method first, but do I call it in the beginning of getHandValue() or just in my main args? Also what was the point in creating a card object in getHandValue()? I haven't used it anywhere.
Re: Help setting values to cards in an array
You call the getCardValue() method when you want the value of a card.
Quote:
what was the point in creating a card object in getHandValue()?
I have no idea why you'd do that.
Re: Help setting values to cards in an array
Okay, so I tried calling the getCardValue() method on line 24, there isn't any errors, but now the total is always coming out as 12. Why?! :( I can't see anything wrong with the code. Can you see what I'm doing wrong and point it out?
Code:
public int getCardValue(Card card) {
int val;
val = 0;
for (int i = 0; i > faces.length; i++) {
if (faces[i].equals(card.getFace())) {
val = i;
break;
}
}
value = val + 1; //adds 1 since the array starts at 0
return value;
}
public void getHandValue(Card hand[]) {
int HandTotal;
boolean ace;
HandTotal = 0;
ace = false;
for (int i = 0; i < hand.length; i++) {
int cardValue;
cardValue = getCardValue(hand[i]);
if (cardValue > 10) {
cardValue = 10; //sets Jack, Queen and King to 10
}
if (cardValue == 1) {
ace = true;
}
HandTotal = HandTotal + cardValue;
}
if (ace == true && HandTotal + 10 <= 21) {
HandTotal = HandTotal + 10;
}
System.out.printf("Hand total: %d\n", HandTotal);
}
Re: Help setting values to cards in an array
Try debugging your code by adding println() statements to show execution progress and how variable values are changing. For example:
Add a: System.out.println("var=" + var);
after every line where a variable is changed by an assignment statement or read into.
I'm done for today.
Re: Help setting values to cards in an array
Okay well thanks so much for your help! It means a lot.