This code is part of my game to check for aces in the player's hand as part of a blackjack game. It's supposed to check hand, pval_hand, for any values that are '1' or '11' (since they would be aces) and keep track of them. ace1, ace2, ace3, & ace4 are there to keep track of which slot the 1st, 2nd, 3rd, & 4th ace(s) are being held. pindex is the index count of how many cards are in pval_hand and is 2 at this point in the game. pval_hand is a vector with 10 elements {11 11 0 0 0 0 0 0 0 0} at the beginning.
My problem is that it cycles once and finds the 1st & 2nd aces at the correct index, but after it cycles again it assigns those values to ace3 & ace4. What I want to happen is that ace1 keeps track of the 1st ace to show up in the player's hand, ace2 the 2nd ace, etc. i.e. if a player had the hand {11 3 2 11 0 0 0 0 0 0} ace1 = 1, ace2 = 4, ace3 = 0, ace4 = 0, and after one pass through the hand should be updated to {1 3 2 11 0 0 0 0 0 0} ace1 = 1, ace2 = 4, ace3 = 0, ace4 = 0. What actually happens is that after the 1st pass it updates to {1 3 2 11 0 0 0 0 0 0} ace1 = 1, ace2 = 4, ace3 = 1, ace4 = 4
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
do
{
phand = 0; //sets base value of player's hand to 0
for (i = 0; i < pindex; i++)
{
if (pval_hand[i] == 11)
{
aceCheck = true;
}
cout << "loop " << i << " phand = " << phand << " + " << pval_hand[i] << " is ";
phand = pval_hand[i] + phand;
cout << phand << endl;
}
cout << "phand is " << phand << endl;
if (aceCheck == true)
{
for (i = 0; i < pindex; i++)
{
if (pval_hand[i] == 11 || pval_hand[i] == 1)
{
aceCount++;
if (ace1 == 0)
{
ace1 = i + 1;
cout << "aceCount loop " << i << " ace1 = " << ace1 << endl;
}
else if (ace2 == 0)
{
ace2 = i + 1;
cout << "aceCount loop " << i << " ace2 = " << ace2 << endl;
}
else if (ace3 == 0 && aceCount < pindex)
{
ace3 = i + 1;
cout << "aceCount loop " << i << " ace3 = " << ace3 << endl;
}
else if (ace4 == 0 && aceCount < pindex)
{
ace4 = i + 1;
cout << "aceCount loop " << i << " ace4 = " << ace4 << endl;
}
else if (aceCount >= pindex)
{
cout << "ace overcount" << endl;
}
else
cout << "ERROR with acecount" << endl;
}
}
cout << "There are " << aceCount << " Ace(s) in the player's hand." << endl;
}
if (phand < 21)
{
//phand <21 code
}
else if (phand == 21)
{
//phand == 21 code
}
else if (phand > 21 && aceCheck == true)
{
phand = 0;
if (ace1 > 0)
{
cout << "ace1" << endl;
pval_hand[ace1 - 1] = 1;
for (i = 0; i < pval_hand.size(); i++)
phand = phand + pval_hand[i];
}
else if (ace2 > 0)
{
cout << "ace2 man" << endl;
pval_hand[ace2 - 1] = 1;
for (i = 0; i < pval_hand.size(); i++)
phand = phand + pval_hand[i];
}
else if (ace3 > 0)
{
cout << "ace3 man" << endl;
pval_hand[ace3 - 1] = 1;
for (i = 0; i < pval_hand.size(); i++)
phand = phand + pval_hand[i];
}
else if (ace4 > 0)
{
cout << "ace4 man" << endl;
pval_hand[ace4 - 1] = 1;
for (i = 0; i < pval_hand.size(); i++)
phand = phand + pval_hand[i];
}
else
{
cout << "acechange error" << endl;
//false positive code
}
}
else
{
cout << "Player busts" << endl;
pturn = 0;
cout << "End player_turn()" << endl;
return;
}
} while (pturn = 1);
|
I will happily provide more code upon request, but I was trying to keep it reduced to only sections that are related to this individual problem. Again, I'm quite new to C++ (and programming in general) so any help is greatly appreciated.