C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 10th, 2005, 06:13 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
Multi Player BlackJack

This is multiplayer black jack heres the code have fun with playing it -(more than 1 person need to play srry for those that have no friends)
Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>

using namespace std;

class Card
{
public:
enum rank {ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
enum suit {CLUBS, DIAMONDS, HEARTS, SPADES};
//overloading << operator so can send Card object to standard outpt
friend ostream& operator<<(ostream& os, const Card& aCard);

Card(rank r = ACE, suit s = SPADES, bool ifu = true);

//returns the value of a card, 1- 11
int GetValue() const;

//flips a card; if face up, becomes face down and vice versa
void Flip();

private:
rank m_Rank;
suit m_Suit;
bool m_IsFaceUp;
};

Card::Card(rank r, suit s, bool ifu): m_Rank(r), m_Suit(s), m_IsFaceUp(ifu)
{}

int Card::GetValue() const
{
//if a cards is face down, its value is 0
int value = 0;
if (m_IsFaceUp)
{
//value is number showing on card
value = m_Rank;
//value is 10 for face cards
if (value > 10)
value = 10;
}
return value;
}
void Card::Flip()
{
m_IsFaceUp = !(m_IsFaceUp);
}
class Hand
{
public:
Hand();

virtual ~Hand();

//adds a card to the hand
void Add(Card* pCard);

//clears hand of all cards
void Clear();
//gets hand total value, intelligently treats aces as 1 or 11
int GetTotal() const;

protected:
vector<Card*> m_Cards;
};

Hand::Hand()
{
m_Cards.reserve(7);
}

Hand::~Hand() //don't use the keyboard virtual outside of class definition
{
Clear();
}
void Hand::Add(Card* pCard)
{
m_Cards.push_back(pCard);
}
void Hand::Clear()
{
//iterate through vector, freeing all memory on the heap
vector<Card*>::iterator iter = m_Cards.begin();
for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
{
delete *iter;
*iter = 0;
}
//clear vector of pointers
m_Cards.clear();
}

int Hand::GetTotal() const
{
//if no cards in hand, return 0
if(m_Cards.empty())
return 0;

//if a first card has value of 0, then card is face down; return 0
if (m_Cards[0]->GetValue() == 0)
return 0;

//adds up card values, treat each ace as 1
int total = 0;
vector<Card*>::const_iterator iter;
for (iter = m_Cards.begin(); iter !=m_Cards.end(); ++iter)
total += (*iter)->GetValue();

//Determine if hand contains ace
bool containsAce = false;
for (iter = m_Cards.begin(); iter != m_Cards.end(); ++iter)
if ((*iter)->GetValue() == Card::ACE)
containsAce = true;

//if hand contains ace and total is low enough, treat ace as 11
if (containsAce && total <= 11)
//add only 10 since we've already added 1 for the ace
total += 10;

return total;
}

class GenericPlayer : public Hand
{
friend ostream& operator<<(ostream& os,const GenericPlayer& aGenericPlayer);

public:
GenericPlayer(const string& name = "");

virtual ~GenericPlayer();

//indicates whether or not a generic player wants to keep hitting
virtual bool I****ting() const = 0;

//indicates whether generic player has busted - has a total greater than 21
bool IsBusted() const;

//announces that the generic player is busted
void Bust() const;

protected:
string m_Name;
};

GenericPlayer::GenericPlayer(const string& name): m_Name(name)
{}

GenericPlayer::~GenericPlayer()
{}

bool GenericPlayer::IsBusted() const
{
return (GetTotal() > 21);
}

void GenericPlayer::Bust() const
{
cout<<m_Name<<" busts.\n";
}

class Player : public GenericPlayer
{
public:
Player(const string& name = "");

virtual ~Player();

//returns whether or not the player wants another hit
virtual bool I****ting() const;

//announces that the player wins
void Win() const;

//announces that the player loses
void Lose() const;

//announces that the player pushes
void Push() const;
};

Player::Player(const string& name): GenericPlayer(name)
{}

Player::~Player()
{}

bool Player::I****ting() const
{
cout<<m_Name<<", do you want a hit? (Y/N): ";
char response;
cin>> response;
return (response == 'y' || response == 'Y');
}

void Player::Win() const
{
cout<<m_Name<<" wins.\n";
}

void Player::Lose() const
{
cout<<m_Name<<" loses.\n";
}

void Player::Push() const
{
cout<<m_Name<<" pushes.\n";
}

class House : public GenericPlayer
{
public:
House(const string& name = "House");

virtual ~House();

//indicates whether house is hitting - will always hit on 16 or less
virtual bool I****ting() const;

//flips over first card
void FlipFirstCard();
};

House::House(const string& name): GenericPlayer(name)
{}

House::~House()
{}

bool House::I****ting() const
{
return (GetTotal() <= 16);
}

void House::FlipFirstCard()
{
if (!(m_Cards.empty()))
m_Cards[0]->Flip();
else cout << "No card to flip!\n";
}

class Deck : public Hand
{
public:
Deck();
virtual ~Deck();

//create a standard deck of 52 cards
void Populate();

//shuffle cards
void Shuffle();

//deal one card to a hand
void Deal (Hand& aHand);

//give additional cards to a generic player
void AdditionalCards(GenericPlayer& aGenericPlayer);
};

Deck::Deck()
{
m_Cards.reserve(52);
Populate();
}

Deck::~Deck()
{}

void Deck::Populate()
{
Clear();
//create standards deck
for (int s = Card::CLUBS; s <= Card::SPADES; ++s)
for (int r = Card::ACE; r <= Card::KING; ++r)
Add(new Card(static_cast<Card::rank>(r),
static_cast<Card::suit>(s)));
}

void Deck::Shuffle()
{
random_shuffle(m_Cards.begin(), m_Cards.end());
}

void Deck::Deal(Hand& aHand)
{
if (!m_Cards.empty())
{
aHand.Add(m_Cards.back());
m_Cards.pop_back();
}
else
{
cout<<"Out of cards. Unable to deal.";
}
}

void Deck::AdditionalCards(GenericPlayer& aGenericPlayer)
{
cout<< endl;
//continue to deal a card as long as generic player isn't busted and
//wants another hit
while ( !(aGenericPlayer.IsBusted()) && aGenericPlayer.I****ting() )
{
Deal(aGenericPlayer);
cout<< aGenericPlayer<< endl;

if (aGenericPlayer.IsBusted())
aGenericPlayer.Bust();
}
}

class Game
{
public:
Game(const vector<string>& names);

~Game();

//plays the game of blackjack
void Play();

private:
Deck m_Deck;
House m_House;
vector<Player> m_Players;
};

Game::Game(const vector<string>& names)
{
//create a vector of players from a vector of names
vector<string>::const_iterator pName;
for (pName = names.begin(); pName != names.end(); ++pName)
m_Players.push_back(Player(*pName));

srand(time(0));   //seed the random generator
m_Deck.Populate();
m_Deck.Shuffle();
}

Game::~Game()
{}

void Game::Play()
{
//deal initial 2 cards to everyone
vector<Player>::iterator pPlayer;
for (int i = 0; i < 2; ++i)
{
for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
m_Deck.Deal(*pPlayer);
m_Deck.Deal(m_House);
}

//hide house's first card
m_House.FlipFirstCard();

//display everyone's hand
for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
cout<< *pPlayer<<endl;

//deal additional cards to players
for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
m_Deck.AdditionalCards(*pPlayer);

//reveal house's first card
m_House.FlipFirstCard();
cout<<endl<<m_House;

//deals additional cards to house
m_Deck.AdditionalCards(m_House);

if (m_House.IsBusted())
{
//everyone still playing wins
for (pPlayer = m_Players.begin(); pPlayer!= m_Players.end(); ++pPlayer)
if ( !(pPlayer->IsBusted()) )
pPlayer->Win();
}
else
{
//compare each player still playing to house
for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
if ( !(pPlayer->IsBusted()) )
{
if (pPlayer->GetTotal() > m_House.GetTotal())
pPlayer->Win();
else if (pPlayer->GetTotal() < m_House.GetTotal())
pPlayer->Lose();
else
pPlayer->Push();
}
}
//remove everyone's cards
for (pPlayer = m_Players.begin(); pPlayer != m_Players.end(); ++pPlayer)
pPlayer->Clear();
m_House.Clear();
}

//function prototypes
ostream& operator<<(ostream& os, const Card& aCard);
ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer);

int main()
{
cout<<"\t\tWelcome to Blackjack!\n\n";

int numPlayers = 0;
while (numPlayers < 1 || numPlayers > 7)
{
cout<<"How many players? (1-7): ";
cin>> numPlayers;
}
vector<string> names;
string name;
for (int i = 0; i < numPlayers; ++i)
{
cout<<"Enter player name: ";
cin>> name;
names.push_back(name);
}
cout<< endl;

//the game loop
Game aGame(names);
char again = 'y';
while (again != 'n' && again != 'N')
{
aGame.Play();
cout<<"\nDo you want to play again? (Y/N): ";
cin>> again;
}
return 0;
}

//overloads << operator so Card object can be sent to cout
ostream& operator<<(ostream& os, const Card& aCard)
{
const string RANKS[] = {"0", "A","2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
const string SUITS[] = {"c", "d", "h", "s"};

if (aCard.m_IsFaceUp)
os<< RANKS[aCard.m_Rank] << SUITS[aCard.m_Suit];
else
os<< "XX";
return os;
}

//overloads <<operator so a GenericPlayer object can be sent to cout
ostream& operator<<(ostream& os, const GenericPlayer& aGenericPlayer)
{
os <<aGenericPlayer.m_Name<< ":\t";

vector<Card*>::const_iterator pCard;
if (!aGenericPlayer.m_Cards.empty())
{
for (pCard = aGenericPlayer.m_Cards.begin();
pCard != aGenericPlayer.m_Cards.end(); ++pCard)
os<< *(*pCard) << "\t";


if (aGenericPlayer.GetTotal() != 0)
cout<< "(" << aGenericPlayer.GetTotal() << ")";
}
else
{
os << "<empty>";
}
return os;
}



there r no error at all and i use the worst compiler out there Borland C++ Builder 6

Reply With Quote
  #2  
Old August 10th, 2005, 07:27 PM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
Where did you get the code, from a programming book or something?
Comments on this post
Geo.Garnett agrees: Hmmm Looks Like It
__________________
Officially a member of the Itsacon fan club. Beer blasts are every friday at Viper_SB's house. I bring the chips.



Reply With Quote
  #3  
Old August 10th, 2005, 09:55 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
y u want to know? b-con? thinkin of closing this thread i just post for other people to play with the code and play blackjack i wasnt askin for help

Reply With Quote
  #4  
Old August 11th, 2005, 04:52 AM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
This forum is for programming C/++ questions -- not for posting fun C/++ code. (Fun code is what http://koders.com is for.) Please limit your threads here to being C/C++ learning related. If you want a forum to just post code in, maybe you should check out this site.

Might I remind you that the name of the forum is, after all, "C/C++ Help".

Reply With Quote
  #5  
Old August 11th, 2005, 05:13 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
k w/e mr. keep it to the freaking rules
ill stop posting my codes then

Reply With Quote
  #6  
Old August 11th, 2005, 06:41 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
I agree with bCoN

=/ Most of the people here are all growd up, bloodlust and they either do this for a living or are trying to start doing this for a living or are just plain serious about it, even a person that does this as a hobby is still very serious about it. Now if you had a problem with the source code from your book that you copied that code from, that's another story, 'and you did copy it'. so what b-con is trying to say(I think) is quit wasting every ones time with trivial questions that you don't even attempt to answer yourself, and stop wasting forum space by blogging it down with your copied source code, and quit trying to show off and pretend you wrote something, because you got embarrassed in a earlier post that was closed. Now if you were getting help with something and it was neccessary to post the source code then thats not outlawed, and then you finished your project and wanted to show it off thats not really a big deal, Just not posting every example in your book as your own, that gets annoying. You better read the license agreement on the CD you found on the back of the book you bought, before you start claiming something as your own, as well. I'm not a moderator or anything but I think most people would agree. Once you get more educated about programming you will see what were talking about, not to say that I am but I can see what he means.

Examples:
[1]post bloodlust
hey guys how to I declare a variable?

[2]post bloodlust
hey guys how much can I assign for that variable?

[3]post bloodlust
hey guys I did it yea
Code:
int main(void)
{
blah blah blah
}

[4]post bloodlust
hey guys what is a gl source?

[5]post bloodlust
hey guys its been two weeks since I started programming and I wrote a complicated game. YEA Right...

This is what I mean, no one wants to lead to step by step through the process of learning this language so have some incentive to do it on your own, that's all. No hard feelings just get serious about it.

Reply With Quote
  #7  
Old August 11th, 2005, 08:48 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
u know what i have no idea what u mean but i just posted this source code for people that have time to see it and enjoy they will do it or if they want to play with the code they will do so and b-con has been changin some of the furoms like gl soucre all i am askin is what books they recommend and anyway i have no idea what u guys r trippin about cause if u guys dont want to see the code that see!! ignore it like i said to b-con if u dont like it u can ignore and also i was looking at sites u recomend me for posting game code and i thank him for that i also did helped a fool out by posting game code so that is a freakin good reason to post this one

Reply With Quote
  #8  
Old August 11th, 2005, 09:36 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
I don't think you get it, people on this site are on this site cause they need help with something, not because they were looking for a chat room. You wouldn't go to a video game forum and try to talk about writing a C++ program would you. We're not tripping about nothing just simply saying people read these forums for help not to see the code you copy and pasted to the forum. I wasn't trying to be mean but I also did some of the things you did(not copy code but other things) and I got told whats up with this forum just like bcon is doing to you, matter of fact bcon is the one who told me to stick to the point. Bottom line no one cares if you post a game that you have been working on and getting help with, but obviously,(not to be mean) you didn't write that program and wasn't getting help with it either, so that's why they say if your just posting for fun just post it in the right place,"not in C++ HELP" because its for help questions that's all.

Reply With Quote
  #9  
Old August 12th, 2005, 04:32 AM
B-Con's Avatar
B-Con B-Con is offline
:bcon: moderator
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Apr 2005
Location: int main()
Posts: 351 B-Con User rank is Private First Class (20 - 50 Reputation Level)B-Con User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 23 h 8 m 6 sec
Reputation Power: 4
This forum is for posting threads with questions related to C/++. The posts within each thread should be devoted to answering the origonal question. I don't see what's so hard to understand about that concept.

You may have perfectly good intentions, Bloodman, in posting code for a specific program, but this is not the place for it. This is a forum for posting (legit, not lazy) questions and getting answers.

Quote:
Originally Posted by BloodlustMan
k w/e mr. keep it to the freaking rules

That earned you a warning. This is the second or third time I've warned you not to insult people -- something that shouldn't even need a single reminder at all.

Not that "Mr. Keep it to the Rules" (paraphrased) is really all that insulting of a name when you concider that it's my job to do so....


I'll leave this thread open for the time being. Hopefully I've made everything as clear as possible....

Reply With Quote
  #10  
Old August 12th, 2005, 01:15 PM
BloodlustShaman BloodlustShaman is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: in earth
Posts: 176 BloodlustShaman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 Days 12 h 9 m 3 sec
Reputation Power: 4
Send a message via Yahoo to BloodlustShaman
Quote:
Originally Posted by Geo.Garnett
I don't think you get it, people on this site are on this site cause they need help with something, not because they were looking for a chat room. You wouldn't go to a video game forum and try to talk about writing a C++ program would you. We're not tripping about nothing just simply saying people read these forums for help not to see the code you copy and pasted to the forum. I wasn't trying to be mean but I also did some of the things you did(not copy code but other things) and I got told whats up with this forum just like bcon is doing to you, matter of fact bcon is the one who told me to stick to the point. Bottom line no one cares if you post a game that you have been working on and getting help with, but obviously,(not to be mean) you didn't write that program and wasn't getting help with it either, so that's why they say if your just posting for fun just post it in the right place,"not in C++ HELP" because its for help questions that's all.
k i got it and i as i said dont wrry i wont post game code no more cause i found a kool site for doin that thanks for b-con and dont wrry geo.garnett no insults were taken but i will be sayin this i am still goin to be in this furoms cuase u guys r experience programmers(or i think so) so when i do have a problem ill ask help here
p.s. what r warnings for come on i have no idea what they can do to me

Reply With Quote
  #11  
Old August 12th, 2005, 02:53 PM
Geo.Garnett's Avatar
Geo.Garnett Geo.Garnett is offline
Registered Loser
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2005
Location: Retardation Nation...
Posts: 347 Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level)Geo.Garnett User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 Days 3 h 13 m 45 sec
Reputation Power: 4
Send a message via AIM to Geo.Garnett
--BloodLust--

Quote:
geo.garnett no insults were taken but i will be sayin this i am still goin to be in this furoms cuase u guys r experience programmers(or i think so) so when i do have a problem ill ask help here


Well BloodLust, I don't think anyone minds you sticking around to get help and stuff =), and I'm not that experienced at programming but we can still learn stuff from one another. We both could read the same thing but I might be able to understand it and you cant, and vise versa. That's what these people are here for and vise versa. Now that, that's all settled, lets just squash it and go about our business.
--No harm--No FowL-- =Þ..

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Multi Player BlackJack


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |