Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 02-26-2003, 02:18 PM   #1 (permalink)
ShadowSoft
Registered User
 
ShadowSoft's Avatar
 
Join Date: Feb 2003
Location: NJ
Posts: 7
ShadowSoft is on a distinguished road
Send a message via AIM to ShadowSoft
Unhappy Black Jack woes

In my programing class we are required to write the game blackjack.

This isn't too much trouble but my problem is that when i "hit" if your starting value is less then eighteen, not only does the program deal a new card it also sends the user two new random cards and adds them to the new card so most likely you always bust.

here's my code maybe someone will understand: it's a lot a code brace your self....
/*the two includes in quotes are my own classes, so they've been foolproofed they're not the problem*/
#include "carddeck.h"
#include "card.h"
#include <iostream.h>
int cardValue(card);
int main()
{
int u=0, notu=0;
char hitStay;
cout<<"Hello, and welcome to the game of blackJack."<<endl;
while(u<=21 && notu<=21 && hitStay!='s')
{
CardDeck BlackJack;
BlackJack.Shuffle();

Card temp,comp1;
//the following code shows the user what he's been dealt
temp=(BlackJack.Deal());
//this next code runs the card through the function CardValue
u+=cardValue(temp);
temp.printCard();
temp=(BlackJack.Deal());
u+=cardValue(temp);
temp.printCard();
//next code shows how many points the user has.
cout<<"You have "<<u<<" points"<<endl;
cout<<" "<<endl;

/*this code deals to the computer, but only shows the user the top card.*/
cout<<"Computer has: "<<endl;
cout<<" "<<endl;
comp1=(BlackJack.Deal());
notu+=cardValue(comp1);
comp1.printCard();
comp1=(BlackJack.Deal());
notu+=cardValue(comp1);

/*this is the part of the program that allows the user to "hit" or "stay"
cout<<"H:Hit or S:Stay"<<endl;
cin<<hitStay;
if (hitStay=='h')
{
temp=(BlackJack.Deal());
u+=cardvalue(temp);
temp.printCard();
cout<<"You now have "<<u<<" points"<<endl;
if(u>21)
cout<<"Bust. You lose."<<endl;
else if (u==21)
cout<<"Way to go, perfect score.<<endl;
}
else if (hitStay=='s')
{
cout<<"You now have "<<u<<" points"<<endl;
comp1.printCard();
cout<<"Computer has "<<notu<<" points"<<endl;
if (u>notu)
cout<<"You win, congratulations"<<endl;
else if (notu>=u)
cout<<"Unlucky for you. The computer has won this time."<<endl;
}
}
return 0;
}
//this function turns all high value caards to the value of 10
int cardValue(Card c)
{
if (c.getValue()>10)
return 10;
else
return c.getValue();
}

see, i wasn't lying.
ShadowSoft is offline   Reply With Quote
Old 02-26-2003, 02:54 PM   #2 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
clarification

So when you hit the computer deals two cards and adds both their totals to the users total?
saline is offline   Reply With Quote
Old 02-26-2003, 04:02 PM   #3 (permalink)
ShadowSoft
Registered User
 
ShadowSoft's Avatar
 
Join Date: Feb 2003
Location: NJ
Posts: 7
ShadowSoft is on a distinguished road
Send a message via AIM to ShadowSoft
Talking

yes.
and thank you for helping. Who would have known that my problem was so stupid.

Around line 50ish in my if h statement i realized that I forgot to tell the program to repeat the hit function if the value was less than 21. Instead the program saw that u was less than 21 and it reenacted the entire while loop.

:o I feel so embarased. Such a stupid mistake but thanks anyway....this code needs lots of work i was thinking about putting all the dealing and that into one or two functions to clear up main more?
ShadowSoft is offline   Reply With Quote
Old 02-26-2003, 04:42 PM   #4 (permalink)
saline
I am red.
 
saline's Avatar
 
Join Date: Feb 2003
Location: Cleveland, OH
Posts: 139
saline is on a distinguished road
saline

oh, um, glad you found the problem. I'm very surprised if I actually helped at all. cool.
saline is offline   Reply With Quote
Old 02-27-2003, 12:22 PM   #5 (permalink)
ShadowSoft
Registered User
 
ShadowSoft's Avatar
 
Join Date: Feb 2003
Location: NJ
Posts: 7
ShadowSoft is on a distinguished road
Send a message via AIM to ShadowSoft
just in case you're interested...

Well here's the finished solution.

i finally got the program to work..if you're interested just send me a message or e-mail me...i'd be more than happy to send the code and all the .cpp and .h files.
ShadowSoft is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 09:07 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting