Code help, function help

This is a discussion on Code help, function help within the C Programming forums, part of the General Programming Boards category; Im making a blackjack game, there is an array cards: Code: int cards[] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,\ 8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13}; Code: void dealplayer1 (){ ...

1. Code help, function help

Im making a blackjack game, there is an array cards:
Code:
```int cards[] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,\
8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13};```
Code:
```void dealplayer1 (){
double R = (double)rand()/(double)RAND_MAX;
double P;
player1.card1 = cards[rand() % 52];
while (isalpha(player1.card1))
player1.card1 = cards[rand() % 52];
cards[player1.card1] = 'X';
if (player1.card1 == 11 || player1.card1 == 12 || player1.card1 == 13 )
player1.card1new = 10;
else
player1.card1new = player1.card1;
player1.card2 = cards[rand() % 52];
while (isalpha(player1.card2))
player1.card2 = cards[rand() % 52];
cards[player1.card2] = 'X';
if (player1.card2 == 11 || player1.card2 == 12 || player1.card2 == 13 )
player1.card2new = 10;
else
player1.card2new = player1.card2;
player1.total = player1.card1new + player1.card2new;
P = (double)1 - (((double)21 - (double)player1.total)/(double)21);
while (R <= P)
player1.extra = cards[rand() % 52];
while (isalpha(player1.extra))
player1.extra = cards[rand() % 52];
cards[player1.extra] = 'X';
if (player1.extra == 11 || player1.extra == 12 || player1.extra == 13 )
player1.extranew = 10;
player1.total += player1.extranew;

}```

This is the code for player1 of the game the problem im having is increase how many cards he can get Is there a better way to do this? looks a bit messy

2. I don't know if I fully understand your intent with this code. There are some nit-picky specifics of game play I could point out, but I'm guessing that's not what you're looking for at the moment.

I'm also not clear on what "increase implementation number" is supposed to mean.

However, a brief analysis of your code, in addition to your indentation, leads me to conclude that you neglected to add braces to the block of code beneath your last "while" loop. I imagine you want all of those lines to execute during each iteration of the loop.

And even if you meant to include these braces, it would yield an infinite loop if "R <= P" (not sure what values these are supposed to represent, either) because neither of those values are updated in the body of the loop (with or without the braces).

3. Originally Posted by Matticus
I don't know if I fully understand your intent with this code. There are some nit-picky specifics of game play I could point out, but I'm guessing that's not what you're looking for at the moment.

I'm also not clear on what "increase implementation number" is supposed to mean.

However, a brief analysis of your code, in addition to your indentation, leads me to conclude that you neglected to add braces to the block of code beneath your last "while" loop. I imagine you want all of those lines to execute during each iteration of the loop.

And even if you meant to include these braces, it would yield an infinite loop if "R <= P" (not sure what values these are supposed to represent, either) because neither of those values are updated in the body of the loop (with or without the braces).
Sorry I just did very fast, will update when I get home. Basically this is a player playing by it self, p is generated using a formula and r randomly. The while at the end is missing few things will update soon