I am having an issue with my code that calculates an integer for the hand total in a game of Black Jack. For some reason the for loop is iterating the first time through twice, meaning that it uses i = 0 twice through the loop before incrementing i. Is there a reason why it is doing this? I don't know what to change.
private void setTotal(){
boolean ace = false;
for (int i = 0; i<hand.size(); i++){
if((hand.get(i).getValue()) == 1){
handTotal += 1;
ace = true;
}
else if((hand.get(i).getValue()) > 10){
handTotal += 10;
}
else {
handTotal += (hand.get(i).getValue());
}
}
if(ace == true && handTotal<12){
handTotal += 10;
}
}
Below is the code with a bunch of print statements I've been using to check it.
private void setTotal(){
boolean ace = false;
for (int i = 0; i<hand.size(); i++){
System.out.println("start " + i);
if((hand.get(i).getValue()) == 1){
handTotal += 1;
ace = true;
System.out.println("ace one");
}
else if((hand.get(i).getValue()) > 10){
handTotal += 10;
System.out.println("face card");
}
else {
handTotal += (hand.get(i).getValue());
System.out.println("num");
}
System.out.println("end " + i);
}
if(ace == true && handTotal<12){
handTotal += 10;
System.out.println("ace plus");
}
}
In the main, I run this
public class BlackJack {
public static void main(String[] args){
Deck deck = new Deck();
Dealer dealer = new Dealer();
deck.shuffle();
for(int i =0; i<2; i++){
dealer.dealCard(deck.draw());
}
System.out.println(dealer.getHand());
System.out.println(dealer.getHand().size());
System.out.println(dealer.getTotal());
}
}
And for example it printed start 0 num end 0 start 0 num end 0 start 1 num end 1 [7 of Clubs, 4 of Clubs] 2 18
handTotalto 0 before entering the loop.