Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Untitled

Untitled

mozie
Not your journal. · · 5 min read
1,113 0
My ISP has been flakey, the name server will just start timing out, but it is still online because it can be pinged. I want to find a nameserver I can set to avoid having to use my ISPs. Oh well, every time I get pissed and decide to call, it starts working again. That means I have had time to play around with some code. I have been really far from anything game related, until I read the story of Mel in some jargon file site. And Mel wrote a blackjack program for some old computer. I worked at a casino doing security for a while, and that really made me want to write slot machine type code to reproduce the game. I never did make anything at the time, but now I decided to make a Deck class. The operator+ and operator= dont really work yet. But the rest of it is working.

deck.h
using namespace std;class	Card{public:	unsigned char value;	Card () {}	~Card () {}	unsigned char	Suit()	{		return (value & 0xF0);	}	unsigned char	Point()	{		switch( (value & 0x0F) )		{		case 0x0A:		case 0x0B:		case 0x0C:		case 0x0D:			return 0x0A;			break;		case 0x01:			return 0x01;			break;		default:			return (value & 0x0F);			break;		}	}};class	Hand{public:	unsigned int numCards;	vector cards;	Hand() 	{		numCards = 0;	}	~Hand() {}	void	Hit(Card &c)	{		++numCards;		cards.push_back(c);	};	vector	GiveBack()	{		vector c (cards);		cards.clear();		numCards = 0;		return c;	};};class	Player{public:	Hand	hand;	Player() {}	~Player() {}	unsigned int Points()	{		unsigned int p = 0;		for(unsigned int i = 0;i < hand.numCards; ++i)		{			p += hand.cards.Point();		}		return p;	}};class	Deck{public:	int numCards;	vector<unsigned char>	cards;	vector Cards;	Deck() { numCards = 0; }	Deck(int ds) 	{		numCards = ds;		Cards.resize(numCards);	}	Deck(Deck & d)	{		Cards = d.Cards;		numCards = d.numCards;	}	~Deck() {};	/*==============================		Generates the deck of cards, if the deck 		is greater or less than 52 cards		it will return false;	==============================*/	bool	Generate();	void	Display();	void	Shuffle();	Card	TakeBottom();	Card	TakeTop();	void	AddTop(const Card & c)	{		++numCards;		Cards.push_back(c);	// *	}	void	AddBottom(const Card & c)	{		++numCards;		vector::iterator bb = Cards.begin();	// *		Cards.insert(bb,c);	// *	}	void	ShiftUp()	{		AddTop(TakeBottom());	}	void	ShiftDown()	{		AddBottom(TakeTop());	}	const Deck	operator<<  ( unsigned int n )	{		for(unsigned int i = 0;i < n; ++i)			ShiftUp();		return *this;	}	const Deck	operator>>  ( unsigned int n )	{		for(unsigned int i = 0;i < n; ++i)			ShiftDown();		return *this;	}	/*==============================		equality operator, copy	==============================*/	Deck operator= ( Deck & a)	{		Deck D;		vector<unsigned char>::iterator b = a.cards.begin();		vector<unsigned char>::iterator e = a.cards.end();		for(;b != e; ++b)		{			D.cards.push_back(*b);		}		return D;	}	/*==============================		adds deck 'a' to the end of 'this' deck	==============================*/	Deck operator+ ( Deck & a)	{		Deck D(*this);		vector<unsigned char>::iterator b = a.cards.begin();		vector<unsigned char>::iterator e = a.cards.end();		for(;b != e; ++b)		{			D.cards.push_back(*b);		}		D.numCards = (D.numCards + a.numCards);		return D;	}	Deck operator+ ( unsigned char a )	{		Deck D(*this);		vector<unsigned char>::iterator p = D.cards.begin();		D.cards.insert(p, a);		++numCards;		return D;	}};// Display Suit and Valuevoid	DisplaySV(unsigned char sample);

deck.cpp
#include "stdafx.h"#include "stdafx.h"#include "deck.h"using namespace std;void	DisplaySV(unsigned char sample){	unsigned char sh = sample & 0xF0;	unsigned char sl = sample & 0x0F;	switch(sh)	{	case 0x80:		cout << "S";		break;	case 0x40:		cout << "D";		break;	case 0x20:		cout << "C";		break;	case 0x10:		cout << "H";		break;	default:		break;	}	switch(sl)	{	case 0x01:		cout << "A";		break;	case 0x0B:		cout << "J";		break;	case 0x0C:		cout << "Q";		break;	case 0x0D:		cout << "K";		break;	default:		cout << (int)sl;		break;	}	cout << endl;}bool	Deck::Generate(){	vector::iterator b = Cards.begin();	vector::iterator e = Cards.end();// + 1;	unsigned char suitn = 0x10;	if( e - b != 52 )		return false;	while(b != e)	{		for(unsigned char i=1;i<0x0E;++i)		{			b->value = (unsigned char)(suitn + i);			++b;		}		suitn = suitn << 1;		if(!suitn)			break;	}	numCards = 52;	return true;}void	Deck::Display(){	vector::iterator B = Cards.begin();	vector::iterator E = Cards.end();	for(;B != E; ++B)		DisplaySV(B->value);}void	Deck::Shuffle(){	unsigned int sz = numCards;	Deck A;	Deck B;	srand((unsigned)time(NULL));	int ra = 11 + (rand() % 5);	for(int i = 0;i < ra; ++i)	{		*this << ra;		for(unsigned int i=0;i		{			if(i < (sz/2))				A.AddTop(TakeBottom());			else				B.AddTop(TakeBottom());		}		for(unsigned int i = 0; i < ( sz / 2 ); ++i)		{				AddTop(A.TakeBottom());			AddTop(B.TakeBottom());		}	}}Card	Deck::TakeBottom(){	Card c;	vector::iterator i = Cards.begin();	c = *i;	Cards.erase(i);	--numCards;	return c;}Card	Deck::TakeTop(){	Card c;	vector::iterator i = Cards.end() - 1;	c = *i;	Cards.erase(i);	--numCards;	return c;}

main.cpp
#include "stdafx.h"#include "deck.h"using namespace std;int _tmain(int argc, _TCHAR* argv[]){		Deck TheCards(52);	Deck TheBoot;	TheCards.Generate();	cout << "the cards:" << endl;	TheCards.Display();	cout << "the boot:" << endl;	TheBoot.Display();	cout << "Shuffle..." << endl;	TheCards.Shuffle();	cout << "the cards:" << endl;	TheCards.Display();	cout << "Shuffle..." << endl;	TheCards.Shuffle();	cout << "the cards:" << endl;	TheCards.Display();	Player * player = new Player();	player->hand.Hit(TheCards.TakeTop());	player->hand.Hit(TheCards.TakeTop());	cout << player->Points() << endl;	system("pause");	return 0;}

There you have it. I think I might use it to make a simple black-jack game, but I'm sure it will be a little while unitl I get it in a graphic interface.

edit: Added some more support classes, Player, Hand, and Card. Almost all vector's have been removed, with exception to the operator+/= becuase they still dont work. Because I still dont use/need them.

Discussion

Loading comments...