I'm trying to make blackjack where if the ace other card > 21, the ace's value is 1 instead of 11. Everything worked until I added that part so I believe it's looping itself since it's using the method in itself. What do I do differently?
@SuppressWarnings({"ConstantConditions", "unused", "SimplifiableIfStatement"})
//HandValue handValue = new HandValue();
public int getCardValue(final Player player, final int cardPos, final Plugin plugin) {
int cardValue = 0;
List<String> types = Arrays.asList("Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King");
for(String type : types) {
if(plugin.getConfig().getList(player.getName() "'s game deck").get(cardPos).toString().contains(type)) {
switch (type) {
case "Ace":
cardValue = 11;
if(getCardValue(player,1,plugin) getCardValue(player,3,plugin) > 21) {
cardValue = 1;
}
break;
case "Two":
cardValue = 2;
break;
case "Three":
cardValue = 3;
break;
case "Four":
cardValue = 4;
break;
case "Five":
cardValue = 5;
break;
case "Six":
cardValue = 6;
break;
case "Seven":
cardValue = 7;
break;
case "Eight":
cardValue = 8;
break;
case "Nine":
cardValue = 9;
break;
case "Ten": case "Jack": case "Queen": case "King":
cardValue = 10;
break;
}
}
}
return cardValue;
}
}
CodePudding user response:
I reputation is too low so that I cannot post comments.
In my opinions. You can make a flag (or flags since you may get two or more ace?), don't give 1 or 11 inside "case "Ace". Do scoring after all done.
CodePudding user response:
may be this might not be a legit way to solve it
but it works fine and you can give it a try
initialize a variable outside the getCardValue() method
int b=0;
then change your getCardValue() method to
case "Ace":
cardValue = 11;
if(b==0)
{
b=1;
int a=getCardValue(player,1,plugin) getCardValue(player,3,plugin) ;
b=0;
if(a> 21) {
cardValue = 1;
}
}
break;
so basically here b=0 as default and when the card is ace it satisfies the condition b==0
and then calls the method again but before doing that it sets b=1 ...
so when it comes back here again ( the ace with the condition and method call )..the condition if(b==0)
is false as 'b' was set to 1 ... so it does not execute the code inside if(b==0)
then after you have the returned value in variable 'a' the next line gets execute that is b=0;
and then if(a> 21) { cardValue = 1; }
hope you solved it and understood the solution