QUOTE (Self) |

Hey, I haven't coded C++ lately. I wonder if I still remember. |

So I decided to make a blackjack program (figured it uses a lot of stuff I used to know). I started going to town. Here's where I'm at now:

CODE |

#include <iostream> #include <cmath> using namespace std; /* Functions */ void init(); void deal(); void display(); void pformat(int card, int num); void dformat(int card, int num); int dealercard(int x); int playercard(int x); /* Globals */ int deck[51]; int ptotal = 0; int dtotal = 0; int pcard[25]; int dcard[25]; char pdisplay[25]; char ddisplay[25]; int pc[25]; int dc[25]; |

CODE |

int main() { srand((unsigned)time(0)); /* Let's get a REAL deck eh? */ init(); /* K done. Now to deal the cards */ deal(); /* We need to display everything! */ display(); return 0;} |

CODE |

void init() { int z = 0; /* First we have to get all of our 10s */ for (int i = 0;i < 16;i++) deck[i] = 10; /* K sweet. Now we have to get 1 through 9 (we'll get 11 in there later, don't worry) */ for (int x = 1;x < 10;x++) { for (int y = 0;y < 4;y++) { z++; deck[15+z] = x;}}} |

CODE |

void deal() { int y = 0; int z = 0; /* We gotta give the players alternating cards. It's teh r00lz! We gonna do it like this: If the number is even, card goes to dealer. If odd, card goes to player (that way player gets the first card) */ for (int x = 1;x < 5;x++) { if (x%2 != 0) { playercard(y); y++;} else { dealercard(z); z++;}} } |

CODE |

int playercard(int x) { /* We need some sh*t declared here */ int ace = 0; int y = rand()%51+0; /* Now we have to get a card from the deck and check whether it's taken (by seeing if the value is 0). */ while (deck[y] == 0) pcard[x] = deck[y]; pformat(y,x); /* Now we have to set the deck card value to 0 (for other checks) */ deck[y] = 0; /* Now we have to add our card to our total */ ptotal += pcard[x]; /* Alrighty. Let's check if we got an Ace (1). Why is this you ask? Well Ace is either 11 OR 1. We have to see if adding 11 to our total will put us over. If it won't, we add it. If it will, we don't. This will actually get really complicated. If we get an Ace on the first card it will be 11. If we then got, say, a seven we have a total of 18. If we hit and get a four our total will be 22. sh*t! We'll get to that in a second. First lets see if it's an Ace and can be used as 11. If it can, we add 10 to it. We're also going to keep track of how many Aces we have. */ if ((pcard[x] == 1) && ((ptotal + 11) < 22)) { pcard[x] = 11; ptotal += pcard[x]; ace++;} /* Now we have to see whether a previous ace put us over. If it did we will subtract 10 from the player's total. */ if ((ptotal > 21) && ace) ptotal -= 10; /* Now we return ptotal. */ /* Note: We return ptotal every time but it doesn't matter because we only call it when it's the actual total */ return ptotal;} |

CODE |

int dealercard(int x) { /* We need some sh*t declared here */ int ace = 0; int y = rand()%51+0; /* Now we have to get a card from the deck and check whether it's taken (by seeing if the value is 0). */ while (deck[y] == 0) dcard[x] = deck[y]; /* Now we have to add our card to our total */ dtotal += dcard[x]; /* We gotta make the cards look pretty */ dformat(y,x); /* Now we have to set the deck card value to 0 (for other checks) */ deck[y] = 0; /* Alrighty. Let's check if we got an Ace (1). Why is this you ask? Well Ace is either 11 OR 1. We have to see if adding 11 to our total will put us over. If it won't, we add it. If it will, we don't. However, if we get an Ace on the first card it will be 11. If we then got, say, a seven we have a total of 18. If we hit and get a four our total will be 22. sh*t! We'll get to that in a second. First lets see if it's an Ace and can be used as 11. If it can, we add 10 to it. We're also going to keep track of how many Aces we have. */ if ((dcard[x] == 1) && ((dtotal + 11) < 22)) { dcard[x] = 11; dtotal += dcard[x]; ace++;} /* Now we have to see whether a previous ace put us over. If it did we will subtract 10 from the player's total. */ if ((dtotal > 21) && ace) dtotal -= 10; /* Now we return dtotal. */ /* Note: We return dtotal every time but it doesn't matter because we only call it when it's the actual total */ return dtotal;} |

CODE |

void pformat(int card, int num) { for (int i = 0;i < 25;i++) pc[i] = 0; /* Player check */ if (card < 4) { pdisplay[num] = 'K'; pc[num] = 1;} else if ((card < 8) && (card > 3)) { pdisplay[num] = 'Q'; pc[num] = 1;} else if ((card < 12) && (card > 7)) { pdisplay[num] = 'J'; pc[num] = 1;} else pcard[num] = deck[num]; } |

CODE |

void dformat(int card, int num) { for (int i = 0;i < 25;i++) dc[i] = 0; /* Dealer check */ if (card < 4) { ddisplay[num] = 'K'; dc[num] = 1;} else if ((card < 8) && (card > 3)) { ddisplay[num] = 'Q'; dc[num] = 1;} else if ((card < 12) && (card > 7)) { ddisplay[num] = 'J'; dc[num] = 1;} else dcard[num] = deck[num]; } |

CODE |

void display() { cout << "Player has: "; for (int i = 0;i < 2;i++) { if (pc[i] == 1) cout << pdisplay[i] << " "; else cout << pcard[i] << " ";} cout << endl; cout << "Dlayer has: "; for (int i = 0;i < 2;i++){ if (dc[i] == 1) cout << ddisplay[i] << " "; else cout << dcard[i] << " ";} cout << endl; } |

Now, as you may be able to tell. You can't do anything yet. It displays the 2 cards that the player and dealer get on the deal. That's it for now.

Now you may be saying to yourself:

QUOTE (You) |

What the f*ck is the point of that!? I can't even play it and you're releasing it!? |

Well, I'm stuck. Like I said before, I haven't coded C++ in a

**long**time.

I come to you oh coding gurus of GTAF for help. WTF is wrong? When I run it, it doesn't

*seem*to work right. I get a lot of 10's, face cards (which is to be expected I suppose), some 0s, and sometimes it seems to just get stuck in a loop.

So please. If you have any ideas as to why it's all f*cked up. Please do help.

P.S. If you need me to host a compiled .exe I can, though I don't know why anyone would need one if they have a compiler. Just let me know if you need one.

And ty in advance for any help you give. If I get no help: f*ck you all

Btw. Sorry for the long ass code. I like my code in a certain way and didn't feel like re-formatting it to post here.

Here's a text file of the code (copy and save as .cpp) for all you who don't want to copy each function omg blackjack.txt