EverCrest Message Forums
You are not logged in. Login or Register.
Author
Topic: Blackjack C++ help
Lyinar Ka`Bael
Are you looking at my pine tree again?
posted 02-11-2007 12:52:58 AM
I will never ever play this game ever again. Having some issues with my client file for my blackjack program. Here's the gist


#include <iostream>
using namespace::std;


#include "deck1.h"
#include "hand1.h"

int main()
{
Deck d; // instantiate object d of class Deck
Hand player, dealer; // instantiate player and dealer objects of class Hand

int choice; // player inputs a selection
int card = d.deal(); // gives card value of dealt card

cout << "(S)tand, (H)it, (Q)uit: ";
cin >> choice;

while (choice != 'Q' && choice != 'q'){
switch (choice){

case '?':
d.shuffle();
d.print();
cout << endl;
break;

case 'H':
case 'h':
cout << "You draw a " << card << endl;
player.addCard(card);
cout << "Dealer shows a " << card << endl;
dealer.addCard(card);
player.print();
break;

case 'S':
case 's':
cout << "Dealer draws a " << card << endl;
dealer.addCard(card);
dealer.print();
break;

default:
break;
} // end switch
} // end while
return 0;
}


The while is causing an infinite loop. And the switch statement isn't working right. It just prints out "(S)tand, (H)it: H and then goes to the next line without going into the switch statement. Without the while and switch statements, all those commands print out as they're supposed to, but I need the ability to keep filling the two hands. Anyone see what I'm doing wrong?


Lyinar Ka`Bael, Piney Fresh Druidess - Luclin

Aaron (the good one)
posted 02-11-2007 01:11:25 AM
You aren't getting your choice again while in the loop. Since you only got it once and let's say the choice was 'h'. it would use 'h' over and over and over again for an infinite loop.

nm I remembered what the switch statement is!

Delidgamond fucked around with this message on 02-11-2007 at 01:13 AM.

Galbadia Hotel - Video Game Music
I am Canadian and I hate The Tragically Hip
Random Insanity Generator
Condom Ninja El Supremo
posted 02-11-2007 01:31:09 AM
Choice never gets changed and therefor remains effectively a constant.

I only see one spot where you're reading a new value for 'choice' and it's outside the loop.

* NullDevice kicks the server. "Floggings will continue until processing power improves!"
-----------------------------------
"That was black magic, and it was easy to use. Easy and fun. Like Legos." -- Harry Dresden
-----------------------------------
That's what playing Ragnarok Online taught me: There's no problem in the universe that can't be resolved by the proper application of daggers to faces.
Lyinar Ka`Bael
Are you looking at my pine tree again?
posted 02-11-2007 02:50:18 AM
Yeah...11 hours of working on something will make you miss something obvious like that. Thanks, RIG. Going to test that in a bit. I got into EQ2 for a while to blow off steam.


Lyinar Ka`Bael, Piney Fresh Druidess - Luclin

Alidane
Urinary Tract Infection
posted 02-11-2007 04:25:52 AM
I thought UBB had a code tag?

code:

def foo():
print "C++? lol"

Yes, yes it does. Use it when posting code.

Cherveny
Papaya
posted 02-11-2007 08:37:13 AM
Also, why do you declare choice to be an int, when you are using it for character values? Shouldn't you delcare it to be a char?
Blindy.
Suicide (Also: Gay.)
posted 02-11-2007 11:43:06 AM
Depending on the compiler it may know how to convert the char into the asc ii code. Personally I'd use a char myself.
Alidane
Urinary Tract Infection
posted 02-11-2007 06:55:27 PM
quote:
Cherveny posted

Also, why do you declare choice to be an int, when you are using it for character values? Shouldn't you delcare it to be a char?


I sincerely doubt that your compiler is dumb enough to leave trash in the top three bytes of an int.

Lyinar Ka`Bael
Are you looking at my pine tree again?
posted 02-11-2007 09:09:08 PM
What's the C++ version of C's fpurge? Think that might help the infinite loop I'm still getting. Although my program's changed quite a bit from that. That was just trying to get things working and test the class calls.


Lyinar Ka`Bael, Piney Fresh Druidess - Luclin

Alidane
Urinary Tract Infection
posted 02-11-2007 11:22:01 PM
quote:
Lyinar Ka`Bael posted

What's the C++ version of C's fpurge? Think that might help the infinite loop I'm still getting. Although my program's changed quite a bit from that. That was just trying to get things working and test the class calls.


Uh, i doubt you're getting an infinite loop because of stream buffering.

fpurge is pretty sloppy anyway, in my opinion.

Naimah
In a Fire
posted 02-11-2007 11:47:29 PM
You're code is riddled with logic errors so I wouldn't worry about the esoteric problems at the moment.
Random Insanity Generator
Condom Ninja El Supremo
posted 02-12-2007 02:57:44 AM
2 basic logic setups that I can see for this... which one would be classified as "right"?

<initialize>
<Initial Question>
<Loop Begin>
<Process input>
<Read new input>
<Loop End>

or

<initialize>
<Loop Begin>
<Read new input>
<Process input>
<Loop End>

* NullDevice kicks the server. "Floggings will continue until processing power improves!"
-----------------------------------
"That was black magic, and it was easy to use. Easy and fun. Like Legos." -- Harry Dresden
-----------------------------------
That's what playing Ragnarok Online taught me: There's no problem in the universe that can't be resolved by the proper application of daggers to faces.
Lyinar Ka`Bael
Are you looking at my pine tree again?
posted 02-12-2007 10:31:31 PM
Neeeeeeeeeeevermind.

We figured it out.

I could use some advice, though. I need to figure out winnings for each hand and keep track of those for an ever-changing bankroll. What's the best way to set that up?

Lyinar Ka`Bael fucked around with this message on 02-12-2007 at 11:32 PM.


Lyinar Ka`Bael, Piney Fresh Druidess - Luclin

Doomjudge
Pancake
posted 02-14-2007 11:41:19 PM
I'd say the best way to set that up is just have a function that manages all of it and you toss a pointer to the bank roll into that, or make a separate class dealing soley with the bankroll that you pass your cash variable to that has a series of functions inside it.

[edit]looking at it, you'd probably want it to be another class, and you just call all the functions for handling the bankroll, make a function that's called everytime you start a hand that asks you about wagering, after the player handles his wagering though, it's gonna be a little messy. You'll have to determine how the computer will figure out it's wager. I'm completely unseasoned on gambling so I don't know if in BJ you bet after the deal or before the deal. Either or you'll have you figure that out.[/edit]

Doomjudge fucked around with this message on 02-14-2007 at 11:46 PM.

Naimah
In a Fire
posted 02-15-2007 12:03:35 AM
I'd personally make a player class that contains both their hand and their bank/wager as child classes. This way the program can more easily support any number of players.
Doomjudge
Pancake
posted 02-15-2007 12:53:40 AM
quote:
Naimah had this to say about Optimus Prime:
I'd personally make a player class that contains both their hand and their bank/wager as child classes. This way the program can more easily support any number of players.

Yeah, that'd be the best bet actually.

Lyinar Ka`Bael
Are you looking at my pine tree again?
posted 02-15-2007 09:51:11 PM
We've not covered Inheritance yet. Our rules say the wager is taken out before the deal, so I could do that as a function if necessary. I just had it set up as variables in the game client file itself, it just wasn't wanting to print the original $1000 value correctly.


Lyinar Ka`Bael, Piney Fresh Druidess - Luclin

7404
Pancake
posted 02-15-2007 10:11:47 PM
Lyinar, you are without a doubt the worst programmer ever. Please quit now before you cause something expensive to be lost.

You're not even a man ffs.

All times are US/Eastern
Hop To: