|
I'm having a problem with a java for loop. public int Value() { for (int i=0; i < Hand.size(); i++) { System.out.println(i); System.out.println(Hand.size()); totalHandValue = 8; } return totalHandValue; } That is the method and i have used the same loop throughout different parts and classes of my program. But this one for some reason whenever you first run it, it prints 0 2 1 2 0 2 1 2 0 2 1 2 the 0s and 1s should not alternate like that the two is also just to tell you how many cards are in the hand. It is a method of a black jack program to add all the cards together. another strange thing is that when you use this a second time it works the way it is supposed to and prints 0 1 2 3 4 and so on.
So if anyone can help it would be appreciated.
|
"another strange thing is that when you use this a second time it works the way it is supposed to and prints 0 1 2 3 4 and so on. " Shoudn't this method print out (assuming Hand.size()==x) 0 x 1 x 2 x ... x x 8 ?
|
Well yes it does when used
Welcome to Black Jack JACK of Hearts XXXXXXX 4 of Hearts 9 of Diamonds 0 2 1 2 0 2 1 2 0 2 1 2 The value of your hand 8 Hit[H] or Stick[S] h 4 of Hearts 9 of Diamonds 6 of Hearts 0 3 1 3 2 3 The value of your hand 8 Hit[H] or Stick[S] The x is just the number of cards in the hand and it is increased when you Hit. I just have System.out.println(Hand.size()); in the method right now to check it.
|
well, you should give more info on how u use the method. The method outputs what Avidkeystamper said. Maybe the first time Hand.size() == 2 and you call the method several times by mistake
|
I use the method in my code only in those two places so far.
System.out.println ("The value of your hand " + playerHand.Value()); System.out.println ("Hit[H] or Stick[S]"); hOrS = scan.nextLine(); playerHand.addCard(Deck1.deal()); while (hOrS.equalsIgnoreCase("h") == true && hOrS.equalsIgnoreCase("s") == false) { System.out.println(playerHand.ArraytoString()); System.out.println("The value of your hand " + playerHand.Value()); System.out.println("Hit[H] or Stick[S]"); hOrS = scan.nextLine(); playerHand.addCard(Deck1.deal()); }
The method is also to add up the number of cards in the hand which it doesn't do, because the i variable in the for loop alternates when it is called, and when added it adds the first two cards together 5 times until 1 and 0 stop alternating. 0 2 1 2 0 2 1 2 0 2 1 2 the 2 is hand.size(); and the 0 and 1 are i. i shouldn't be alternating variables like that because in a for loop i is incremented at the end of it each time, not added them subtracted, also if it was doing that why doesn't it become a infinite loop.
|
Are you sure that you have copy/pasted the exact code? This kind of error is mainly due to some typing mistake. Also I guess you should just label what's i and whats HandValue() in the debug output so that you're sure that what you're looking at is "i" and not some other variable.
|
Have you tried to assign hand.value() to a local variable, and in the for loop, use i<variable instead of i<hand.value()?
|
On January 27 2009 04:10 Iankill wrote: I use the method in my code only in those two places so far.
System.out.println ("The value of your hand " + playerHand.Value()); System.out.println ("Hit[H] or Stick[S]"); hOrS = scan.nextLine(); playerHand.addCard(Deck1.deal()); while (hOrS.equalsIgnoreCase("h") == true && hOrS.equalsIgnoreCase("s") == false) { System.out.println(playerHand.ArraytoString()); System.out.println("The value of your hand " + playerHand.Value()); System.out.println("Hit[H] or Stick[S]"); hOrS = scan.nextLine(); playerHand.addCard(Deck1.deal()); }
I presume this is the code that outputs the second part of your previous post:
0 2 1 2 The value of your hand 8 Hit[H] or Stick[S] h 4 of Hearts 9 of Diamonds 6 of Hearts
0 3 1 3 2 3 The value of your hand 8 Hit[H] or Stick[S]
The wrong output is in the first part:
Welcome to Black Jack JACK of Hearts XXXXXXX
4 of Hearts 9 of Diamonds
0 2 1 2 0 2 1 2
Can you post this code too.
|
Germany2896 Posts
Replace System.out.println(i); System.out.println(Hand.size()); by System.out.println("i:"+i); System.out.println("size:"+Hand.size());
and then post again what the output should look like and how it looks like. For me it looks like it is doing what it should do.
|
On January 27 2009 04:47 MasterOfChaos wrote: Replace System.out.println(i); System.out.println(Hand.size()); by System.out.println("i:"+i); System.out.println("size:"+Hand.size());
and then post again what the output should look like and how it looks like. For me it looks like it is doing what it should do.
I think his problem is that in the beginning playerHand.Value(); is called not once but three times for some reason.
|
Belgium9942 Posts
On January 27 2009 04:10 Iankill wrote: I use the method in my code only in those two places so far.
System.out.println ("The value of your hand " + playerHand.Value()); System.out.println ("Hit[H] or Stick[S]"); hOrS = scan.nextLine(); playerHand.addCard(Deck1.deal()); while (hOrS.equalsIgnoreCase("h") == true && hOrS.equalsIgnoreCase("s") == false) { System.out.println(playerHand.ArraytoString()); System.out.println("The value of your hand " + playerHand.Value()); System.out.println("Hit[H] or Stick[S]"); hOrS = scan.nextLine(); playerHand.addCard(Deck1.deal()); }
The method is also to add up the number of cards in the hand which it doesn't do, because the i variable in the for loop alternates when it is called, and when added it adds the first two cards together 5 times until 1 and 0 stop alternating. 0 2 1 2 0 2 1 2 0 2 1 2 the 2 is hand.size(); and the 0 and 1 are i. i shouldn't be alternating variables like that because in a for loop i is incremented at the end of it each time, not added them subtracted, also if it was doing that why doesn't it become a infinite loop.
wait
you dont understand why they alternate like that?
everytime you execute the for loop, it prints i, then the handsize which is 2. It terminates after the 1, 2 print because the next value for i (2) is obviously not smaller than handsize
you should look at your own code lol
|
Um, all that method is doing at all is printing the numbers from 0 to however large your hand size is. There is no point in the method where you're actually doing anything with WHAT the cards are. I'm assuming that there's some sort of array or ArrayList where the cards are kept. In that case, shouldn't the method look more like:
public int value(){
for(int i = 0; i < Hand.size(); i++) totalHandValue += Hand[i]; return totalHandValue; }
|
On January 27 2009 04:47 MasterOfChaos wrote: Replace System.out.println(i); System.out.println(Hand.size()); by System.out.println("i:"+i); System.out.println("size:"+Hand.size());
and then post again what the output should look like and how it looks like. For me it looks like it is doing what it should do. i tried this and it gave me this + Show Spoiler +Welcome to Black Jack 2 of Spades XXXXXXX 7 of Hearts 5 of Hearts i:0 size:2 i:1 size:2 i:0 size:2 i:1 size:2 i:0 size:2 i:1 size:2 The value of your hand 8 Hit[H] or Stick[S]
I am also using a ArrayList if that helps anyone. Also here is the code of the driver class
+ Show Spoiler +import java.util.*; public class BlackJackDriver { public static void main(String[] args) { boolean playGame = true, blackJack = false; card flipcard; String hOrS; Scanner scan = new Scanner(System.in); System.out.println("Welcome to Black Jack"); do { DeckOfCards Deck1 = new DeckOfCards(); Deck1.Shuffle(); BlackJackHand dealerHand = new BlackJackHand (Deck1.deal(), Deck1.deal()); BlackJackHand playerHand = new BlackJackHand (Deck1.deal(), Deck1.deal()); dealerHand.flip(0); System.out.println(dealerHand.ArraytoString()); System.out.println(playerHand.ArraytoString()); if (dealerHand.BlackJackCheck(dealerHand) == 0) { blackJack = true; System.out.println("Dealer Wins"); playGame = false; } if (playerHand.BlackJackCheck(playerHand) == 0) { blackJack = true; System.out.println("Player Wins"); playGame = false; } else { System.out.println ("The value of your hand " + playerHand.Value()); System.out.println ("Hit[H] or Stick[S]"); hOrS = scan.nextLine(); playerHand.addCard(Deck1.deal()); while (hOrS.equalsIgnoreCase("h") == true && hOrS.equalsIgnoreCase("s") == false) { System.out.println(playerHand.ArraytoString()); System.out.println("The value of your hand " + playerHand.Value()); System.out.println("Hit[H] or Stick[S]"); hOrS = scan.nextLine(); playerHand.addCard(Deck1.deal()); } dealerHand.flip(0); System.out.println("Dealers cards " + dealerHand.ArraytoString()); System.out.println("The value of the dealers hand " + dealerHand.Value()); while(dealerHand.Value() <= 16); { dealerHand.addCard(Deck1.deal()); System.out.println("Dealers cards " + dealerHand.ArraytoString()); System.out.println("The value of the dealers hand " + dealerHand.Value()); } } } while(playGame == true); } }
the hand class
+ Show Spoiler +import java.util.*; public class BlackJackHand { private ArrayList<card> Hand = new ArrayList<card>(); private card card1, card2, cardi, valueHolder, aceCard, toStringCard, cardViewed; private int cardsInHand, handValue, handAdd, totalHandValue, handcheck; private String stringHolder; private ArrayList<String> toStringArray = new ArrayList<String>(); private int i = 0; public BlackJackHand(card card1, card card2) { Hand.add(card1); Hand.add(card2); } public ArrayList addCard(card cardi) { Hand.add(cardi); return Hand; } public int Value() { for (int i=0; i < Hand.size(); i++) { System.out.println("i:"+i); System.out.println("size:"+Hand.size()); totalHandValue = 8; } return totalHandValue; }
public int handSize() { cardsInHand = Hand.size(); return cardsInHand; } public int reduceHand(card aceCard) { if (handValue > 21 && aceCard.toInt(aceCard) == 11) { handValue = handValue - 10; return handValue; } else return handValue; } public String ArraytoString() { for (int i=0; i<Hand.size(); i++) { toStringCard = Hand.get(i); System.out.println (toStringCard.toString(toStringCard)); } return " ";
} public String compareTo(BlackJackHand dealerHand, BlackJackHand playerHand) { if (dealerHand.Value() >= playerHand.Value()) { return ("Dealer Wins"); } else return ("Player Wins"); } public int BlackJackCheck(BlackJackHand Hand1) { if (Hand1.Value() == 21) { return 0; } else return 1; } public ArrayList flip(int handPlace) { cardViewed = Hand.remove(handPlace); cardViewed.flip(); Hand.add(cardViewed); return Hand; } }
I didn't include the card or the deck class but if anyone wants me to put them up as well i will. Also i found that it proubably doesn't have anything to do with the for loop in the value class but i can't find where i called the value class three times or
|
No way to help you unless you post the full code with all the classes, also note that the problem for the first loop may be a)In the main class (like running the loop n times) b)the Hand.size() method, maybe the method changes the class variable you use for the class hand. As i dont have access to that info i can help. Also you may want to put a "home made" debugger: put some variable in the loop and watch how it changes throught your code once it runs.
|
OK, the reason for the triple output in the beginning is because the method Value() is called within BlackJackCheck(). And you call BlackJackCheck() in the beginning twice. Problem solved .
BTW, since you use the method Value() frequently, it might be a good idea to keep the value in a member variable and recalculate it each time you change the hand.
|
On January 27 2009 05:49 MER wrote:OK, the reason for the triple output in the beginning is because the method Value() is called within BlackJackCheck(). And you call BlackJackCheck() in the beginning twice. Problem solved .
Thanks but i also just figured this out but commenting everything out until it worked. Also thanks to everyone who helped. I've also decided to attach the rest of the code if any of you guys want to screw around with it. Also I will post the finished product later tonight but don't expect a GUI just yet. deck of cards class + Show Spoiler +import java.util.*; import java.util.Random; public class DeckOfCards { private final int DECK = 52; card[] cards = new card[DECK]; private int suit = 0; String cardchecker1; private int cardnum = 1,cardsLeft; private ArrayList<card> finaldeck = new ArrayList<card>(); private Random gen = new Random(); private card placeholder, cardholder, dealcard; public DeckOfCards() { finaldeck = create(); } public void cardchecker() { for (int i=0; i<finaldeck.size(); i++) { cardholder = finaldeck.get(i); System.out.println (cardholder.toString(cardholder)); } } public ArrayList create() { for (int i=0; i<cards.length; i++) { cards[i] = new card(cardnum, suit); if (i == 12) { suit++; cardnum = 0; } if (i == 25) { suit++; cardnum = 0; } if (i == 38) { suit++; cardnum = 0; } else { suit = suit; } finaldeck.add(cards[i]); cardnum++; } return finaldeck;
}
public ArrayList Shuffle() { for(int i=0; i<9001;i++) { placeholder = finaldeck.get(gen.nextInt(52)); finaldeck.remove(placeholder); finaldeck.add(gen.nextInt(52), placeholder); } return finaldeck; } public card deal() { dealcard = finaldeck.remove(0); return dealcard; } public int cardsleft() { cardsLeft = finaldeck.size(); return cardsLeft; } }
card class + Show Spoiler +public class card { public final int SPADES = 0, HEARTS = 1, DIAMONDS = 2, CLUBS = 3; // Codes for the 4 suits. public final int ACE = 1,JACK = 11, QUEEN = 12, KING = 13; // Codes for the non-numeric cards. // Cards 2 through 10 have their // numerical values for their codes. private boolean isFlipped = false; private String cardstring; private final int cardSuit; // The suit of this card, one of the constants // SPADES, HEARTS, DIAMONDS, CLUBS. private final int cardValue; // The value of this card, from 1 to 11. public card(int theValue, int theSuit) { // Construct a card with the specified value and suit. // Value must be between 1 and 13. Suit must be between // 0 and 3. If the parameters are outside these ranges, // the constructed card object will be invalid. cardValue = theValue; cardSuit = theSuit; } public int integerValue() { if (cardValue == ACE) // returns the ACE card value return 11; if (cardValue == KING) // returns the KING card value return 10; if (cardValue == QUEEN) // returns the QUEEN card value return 10; if (cardValue == JACK) // returns the JACK card value return 10; else return cardValue; } public String value() //A method that returns the value of a card { if (cardValue == ACE) // returns the ACE card value return ("ACE"); if (cardValue == KING) // returns the KING card value return ("KING"); if (cardValue == QUEEN) // returns the QUEEN card value return ("QUEEN"); if (cardValue == JACK) // returns the JACK card value return ("JACK"); else cardstring = Integer.toString(cardValue); // returns the card value of the numerical types return cardstring; } public String suit() // A method that returns the suit of a card { if (cardSuit == 0) //returns Spades return ("Spades"); if (cardSuit == 1) // returns Hearts return ("Hearts"); if (cardSuit == 2) // returns Diamonds return ("Diamonds"); else //returns Clubs return ("Clubs"); } public int compareTo (card firstCard, card secondCard) // A method that compares two cards { if (firstCard.integerValue() >= secondCard.integerValue()) // checks if the first card is greater than second card return 1; // returns 1 if the first card is greater than the second if (firstCard.integerValue() <= secondCard.integerValue()) // checks if the first card is less than the second card return -1; // returns -1 if the first card is less than the second else return 0; // returns 0 if the cards are equal } public void flip() // a method to flip a card over { if (isFlipped == true) // checks if card is already flipped isFlipped = false; // if card is flipped it flips it right side up else isFlipped = true; // flips card over so no value can be shown } public String toString(card playerCard) // A toString method { if (isFlipped == true) return ("XXXXXXX"); //returns XXXXXXX if card is Flipped else return (playerCard.value() + " of " + playerCard.suit()); //returns card value and suit } public int toInt(card playerCard) { if ((playerCard.value()).equalsIgnoreCase("ace") == true ) // returns the ACE card value return 11; if (playerCard.integerValue() == KING) // returns the KING card value return 10; if (playerCard.integerValue() == QUEEN) // returns the QUEEN card value return 10; if (playerCard.integerValue() == JACK) // returns the JACK card value return 10; else return playerCard.integerValue(); } }
|
The first two prints of "0, 2, 1, 2" are from + Show Spoiler +if (dealerHand.BlackJackCheck(dealerHand) == 0) {
blackJack = true; System.out.println("Dealer Wins"); playGame = false; } if (playerHand.BlackJackCheck(playerHand) == 0) { blackJack = true; System.out.println("Player Wins"); playGame = false; } When the if statements are being evaluated BlackJackCheck is called, which calls Value, which has the side effect of printing those numbers.
Regardless, your code makes no sense. Value just prints out 0, x, 1, x ... x-1, x(x being Hand.size()) and then returns 8. It always returns 8, it doesn't do anything that adds up the value of your hand.
Edit: I should really start refreshing threads before posting my replies. ;P
|
You may want to try the "switch case" command instead of those ugly nested "if" but thats just for a better code presentation (check suns java tutorial for use), also remeber to put class names with upper first letter. There are some other nice commands that simplify code and remember to use proper identation when managing loops, that help you to look for errors (like putting a tab space to separate the inside of a loop). Good luck.
|
|
|
|