Issue With Methods Not Being Called
Author |
Message |
Varsteil
|
Posted: Thu Jan 10, 2013 10:22 am Post subject: Issue With Methods Not Being Called |
|
|
Occasionally, when I run my Blackjack program, when it comes to the option to call a different method, when I choose to go to my method to "hit", my program just keeps going back to the options, but when I choose to "fold" or "stay", everything is fine. I noticed that seems to occur with specific cards, such as Twos or Threes.
Here's an example of my console output:
code: | The card you were dealt is Two of Spades
Your total hand is 13
Press 1 to hit, press 2 to fold, press 3 to stay
1
Your total hand is 13
Press 1 to hit, press 2 to fold, press 3 to stay
1
Your total hand is 13
Press 1 to hit, press 2 to fold, press 3 to stay
3
You Lose! The computer had a total of 18 while you had a total of 13 |
Here's my block of code for changing methods:
code: | System.out.println("Press 1 to hit, press 2 to fold, press 3 to stay"); //Options for player
action = input.nextInt();
switch (action) {
case 1: hitMethod(); break; //Add another card
case 2: lossMethod(); break; //Give up
case 3: comparison();break; //Stay the same |
Here's my block of code for selecting my cards:
code: | i = (cards.nextInt(51)); //Generating card
deck[i] = 1; //Setting card as used
if (deck[i] == 1) { //If used card is chosen
i = (cards.nextInt(51));//Pick a new card
}
deck[i] = 1;
value[0] = i + 1; //Add 1 to card chosen so the card value is never 0
if (value[0] > 13 && value[0] < 27) { //Sets the value of cards over 12 (Not Spades) to equal their card amount
value[0] = value[0] - 13;
} else if (value[0] > 26 && value[0] < 40) {
value[0] = value[0] - 26;
} else if (value[0] > 39 && value[0] < 53) {
value[0] = value[0] - 39;
}
if (value[0] > 10) { //Sets all Jacks, Queens, Kings to equal 10
value[0] = 10;
}
switch (i) { //Assigns the random card a name
case 0: cardNames[0] = "Ace of Spades";break;
case 1: cardNames[0] = "Two of Spades";break;
case 2: cardNames[0] = "Three of Spades";break;
case 3: cardNames[0] = "Four of Spades";break;
case 4: cardNames[0] = "Five of Spades";break;
case 5: cardNames[0] = "Six of Spades";break;
case 6: cardNames[0] = "Seven of Spades";break;
case 7: cardNames[0] = "Eight of Spades";break;
case 8: cardNames[0] = "Nine of Spades";break;
case 9: cardNames[0] = "Ten of Spades";break;
case 10: cardNames[0] = "Jack of Spades";break;
case 11: cardNames[0] = "Queen of Spades";break;
case 12: cardNames[0] = "King of Spades";break;
case 13: cardNames[0] = "Ace of Hearts";break;
case 14: cardNames[0] = "Two of Hearts";break;
case 15: cardNames[0] = "Three of Hearts";break;
case 16: cardNames[0] = "Four of Hearts";break;
case 17: cardNames[0] = "Five of Hearts";break;
case 18: cardNames[0] = "Six of Hearts";break;
case 19: cardNames[0] = "Seven of Hearts";break;
case 20: cardNames[0] = "Eight of Hearts";break;
case 21: cardNames[0] = "Nine of Hearts";break;
case 22: cardNames[0] = "Ten of Hearts";break;
case 23: cardNames[0] = "Jack of Hearts";break;
case 24: cardNames[0] = "Queen of Hearts";break;
case 25: cardNames[0] = "King of Hearts";break;
case 26: cardNames[0] = "Ace of Clubs";break;
case 27: cardNames[0] = "Two of Clubs";break;
case 28: cardNames[0] = "Three of Clubs";break;
case 29: cardNames[0] = "Four of Clubs";break;
case 30: cardNames[0] = "Five of Clubs";break;
case 31: cardNames[0] = "Six of Clubs";break;
case 32: cardNames[0] = "Seven of Clubs";break;
case 33: cardNames[0] = "Eight of Clubs";break;
case 34: cardNames[0] = "Nine of Clubs";break;
case 35: cardNames[0] = "Ten of Clubs";break;
case 36: cardNames[0] = "Jack of Clubs";break;
case 37: cardNames[0] = "Queen of Clubs";break;
case 38: cardNames[0] = "King of Clubs";break;
case 39: cardNames[0] = "Ace of Diamonds";break;
case 40: cardNames[0] = "Two of Diamonds";break;
case 41: cardNames[0] = "Three of Diamonds";break;
case 42: cardNames[0] = "Four of Diamonds";break;
case 43: cardNames[0] = "Five of Diamonds";break;
case 44: cardNames[0] = "Six of Diamonds";break;
case 45: cardNames[0] = "Seven of Diamonds";break;
case 46: cardNames[0] = "Eight of Diamonds";break;
case 47: cardNames[0] = "Nine of Diamonds";break;
case 48: cardNames[0] = "Ten of Diamonds";break;
case 49: cardNames[0] = "Jack of Diamonds";break;
case 50: cardNames[0] = "Queen of Diamonds";break;
case 51: cardNames[0] = "King of Diamonds";break;
} |
Thanks for reading, does anyone know what might be causing this? The cards I noticed this happening with are mostly the Two and Three of Spades and the Three of Clubs. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DemonWasp
|
Posted: Thu Jan 10, 2013 11:53 am Post subject: RE:Issue With Methods Not Being Called |
|
|
This code does not do what you want it to do:
code: |
deck[i] = 1; //Setting card as used
if (deck[i] == 1) { //If used card is chosen
i = (cards.nextInt(51));//Pick a new card
}
deck[i] = 1;
|
|
|
|
|
|
|
Varsteil
|
Posted: Fri Jan 11, 2013 10:19 am Post subject: Re: Issue With Methods Not Being Called |
|
|
Thanks for pointing that out to me, I made a revision to it
code: | if (deck[i] == 1) { //If used card is chosen
i = (cards.nextInt(51));//Pick a new card
}
deck[i] = 1; //Setting card as used |
Do you have any idea how I could fix the bugs I mentioned in the OP? |
|
|
|
|
|
DemonWasp
|
Posted: Fri Jan 11, 2013 11:14 pm Post subject: RE:Issue With Methods Not Being Called |
|
|
First, that block of revised code still doesn't do what you want it to do. What if the "Pick a new card" bit also picks a card that's already used?
Second, that switch statement looks very suspicious to me, since it's always assigning to cardNames[0] rather than cardNames[variable], as I would generally expect.
Other than that, though, you haven't actually given us the full code of hitMethod() and an explanation of what your variables are used for. Without more details, I can't help you. |
|
|
|
|
|
Varsteil
|
Posted: Mon Jan 14, 2013 9:54 am Post subject: Re: Issue With Methods Not Being Called |
|
|
code: | public static void hitMethod() {
Scanner input = new Scanner(System.in);
if (playerCard[2] == 0) { //Checks if the third card was selected
i = (cards.nextInt(51)); //Geneartes random card
if (deck[i] == 1) { //If the card was already chosen, generate a new card
i = (cards.nextInt(51));
}
deck[i] = 1; //Setting the card as used
value[2] = i + 1; //Converting cards to their values
if (value[2] > 13 && value[2] < 27) {
value[2] = value[2] - 13;
} else if (value[2] > 26 && value[2] < 40) {
value[2] = value[2] - 26;
} else if (value[2] > 39 && value[2] < 53) {
value[2] = value[2] - 39;
}
if (value[2] > 10) {
value[2] = 10;
}
playerHand = playerHand + value[2]; //Calculating the value of the hand
if (deck[i] == 1) {
playerCard[2] = deck[i];
}
switch (i) { //Assigning card names
case 0: cardNames[2] = "Ace of Spades";break;
case 1: cardNames[2] = "Two of Spades";break;
case 2: cardNames[2] = "Three of Spades";break;
case 3: cardNames[2] = "Four of Spades";break;
case 4: cardNames[2] = "Five of Spades";break;
case 5: cardNames[2] = "Six of Spades";break;
case 6: cardNames[2] = "Seven of Spades";break;
case 7: cardNames[2] = "Eight of Spades";break;
case 8: cardNames[2] = "Nine of Spades";break;
case 9: cardNames[2] = "Ten of Spades";break;
case 10: cardNames[2] = "Jack of Spades";break;
case 11: cardNames[2] = "Queen of Spades";break;
case 12: cardNames[2] = "King of Spades";break;
case 13: cardNames[2] = "Ace of Hearts";break;
case 14: cardNames[2] = "Two of Hearts";break;
case 15: cardNames[2] = "Three of Hearts";break;
case 16: cardNames[2] = "Four of Hearts";break;
case 17: cardNames[2] = "Five of Hearts";break;
case 18: cardNames[2] = "Six of Hearts";break;
case 19: cardNames[2] = "Seven of Hearts";break;
case 20: cardNames[2] = "Eight of Hearts";break;
case 21: cardNames[2] = "Nine of Hearts";break;
case 22: cardNames[2] = "Ten of Hearts";break;
case 23: cardNames[2] = "Jack of Hearts";break;
case 24: cardNames[2] = "Queen of Hearts";break;
case 25: cardNames[2] = "King of Hearts";break;
case 26: cardNames[2] = "Ace of Clubs";break;
case 27: cardNames[2] = "Two of Clubs";break;
case 28: cardNames[2] = "Three of Clubs";break;
case 29: cardNames[2] = "Four of Clubs";break;
case 30: cardNames[2] = "Five of Clubs";break;
case 31: cardNames[2] = "Six of Clubs";break;
case 32: cardNames[2] = "Seven of Clubs";break;
case 33: cardNames[2] = "Eight of Clubs";break;
case 34: cardNames[2] = "Nine of Clubs";break;
case 35: cardNames[2] = "Ten of Clubs";break;
case 36: cardNames[2] = "Jack of Clubs";break;
case 37: cardNames[2] = "Queen of Clubs";break;
case 38: cardNames[2] = "King of Clubs";break;
case 39: cardNames[2] = "Ace of Diamonds";break;
case 40: cardNames[2] = "Two of Diamonds";break;
case 41: cardNames[2] = "Three of Diamonds";break;
case 42: cardNames[2] = "Four of Diamonds";break;
case 43: cardNames[2] = "Five of Diamonds";break;
case 44: cardNames[2] = "Six of Diamonds";break;
case 45: cardNames[2] = "Seven of Diamonds";break;
case 46: cardNames[2] = "Eight of Diamonds";break;
case 47: cardNames[2] = "Nine of Diamonds";break;
case 48: cardNames[2] = "Ten of Diamonds";break;
case 49: cardNames[2] = "Jack of Diamonds";break;
case 50: cardNames[2] = "Queen of Diamonds";break;
case 51: cardNames[2] = "King of Diamonds";break;
}
System.out.println("The card you were dealt is " + cardNames[2]);
if (value[2] == 1) { //Choosing 1 or 11 value for Ace
System.out.println("You have gotten an Ace! Would you like to give the card a value of 1 or 11?");
value[2] = input.nextInt();
}
} else if (playerCard[2] != 0 && playerCard[3] == 0) { //Checking if the third card was chosen
i = (cards.nextInt(51)); //Generating new card
if (deck[i] == 1) { //Checking if the card was chosen
i = (cards.nextInt(51));
}
switch (i) { //Giving card its' name
*****}
System.out.println("The card you were dealt is " + cardNames[3]);
value[3] = i + 1; //Converting card to its' value
if (value[3] > 13 && value[3] < 27) {
value[3] = value[3] - 13;
} else if (value[3] > 26 && value[3] < 40) {
value[3] = value[3] - 26;
} else if (value[3] > 39 && value[3] < 53) {
value[3] = value[3] - 39;
}
if (value[3] > 10) {
value[3] = 10;
}
if (value[3] == 1) { //Value of 1 or 11 for Ace
System.out.println("You have gotten an Ace! Would you like to give the card a value of 1 or 11?");
value[3] = input.nextInt();
}
playerHand = playerHand + value[3]; //Update the value for the player's hand
deck[i] = 1;
if (deck[i] == 1) {
playerCard[3] = deck[i];
}
}else if (playerCard[2] != 0 && playerCard[3] != 0 && playerCard[4] == 0) {
i = (cards.nextInt(51));
switch (i) {
*****
}
System.out.println("The card you were dealt is " + cardNames[4]);
value[4] = i + 1;
if (value[4] > 13 && value[4] < 27) {
value[4] = value[4] - 13;
} else if (value[4] > 26 && value[4] < 40) {
value[4] = value[4] - 26;
} else if (value[4] > 39 && value[4] < 53) {
value[4] = value[4] - 39;
}
if (value[4] > 10) {
value[4] = 10;
}
playerHand = playerHand + value[4];
if (value[4] == 1) { //Option to select 1 or 11 for Ace
System.out.println("You have gotten an Ace! Would you like to give the card a value of 1 or 11?");
value[4] = input.nextInt();
}
if (deck[i] == 1) { //Checks if the card was chosen
i = (cards.nextInt(51));
}
deck[i] = 1;
if (deck[i] == 1) {
playerCard[4] = deck[i];
}
}
System.out.println("Your total hand is " + playerHand);
if (playerHand > 21) { //Player goes bust
System.out.println("You went over 21, you lose!");
playerMoney = playerMoney - bet;
botMoney = botMoney + bet;
System.out.println("Play again? 1 for Yes, 2 for No");
action = input.nextInt();
if (action == 1) {
startMenu();
} else if (action == 2) {
loseCondition();
}
} else {
options();
}
} |
The ***** is the long list of card names
Here's my code for the hit method, from what I can see, only the hit method causes bugs. What I'm trying to do is; it checks for whether or not the card in the hand already has a value, and if it does, it moves onto the next card, to a maximum of 5 cards in the hand since, from what I googled, the maximum hand size for blackjack is 5 cards. |
|
|
|
|
|
|
|