Jump to content
  • Advertisement
Sign in to follow this  
  • entries
    72
  • comments
    38
  • views
    22334

More on twenty-one

Sign in to follow this  
mozie

285 views

Building this deck of cards is pretty fun. I have what I consider to be a pretty good foundation thus far. I spent some time today playing with what I wanted to do to my code. I do not have a compiler with me at work, so I always have errors when I get home and try to build. I always rely on the compiler to remember the syntax of things I am new to. What that means is I spent most of the evening fixing my code to work, and now it works. The thing I like the most, is the Deck::operator/ that I added. It takes the current deck and returns a vector(n). I had to fix alot of the crap I had in my other operators, I wasn't using them so they didn't get tested. Until tonight, and they didn't work correctly. I know you dont like it when I talk alot, so here are the goodies.

deck.h

#ifndef _DECK_H_
#define _DECK_H_

#include "card.h"

using namespace std;

class Deck
{
public:
unsigned int numCards;
vector Cards;

Deck() { numCards = 0; }
Deck(int ds)
{
numCards = ds;
Cards.resize(numCards);
}
Deck(const Deck & d)
{
Cards = d.Cards;
numCards = d.numCards;
}

~Deck() {};

bool Generate(unsigned int n);
void Display();
void Shuffle();
Card TakeBottom();
Card TakeTop();

void AddTop(const Card & c)
{
++numCards;
vector::iterator e = Cards.end();
Cards.insert(e,c);
}
void AddBottom(const Card & c)
{
++numCards;
vector::iterator b = Cards.begin();
Cards.insert(b,c);
}
void ShiftUp()
{
Card C = TakeBottom();
AddTop(C);
}
void ShiftDown()
{
Card C = TakeTop();
AddBottom(C);
}
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
==============================*/

const Deck operator= ( const Deck & a)
{
Cards = a.Cards;
numCards = a.numCards;
return *this;
}
/*==============================
adds deck 'a' to the end of 'this' deck
==============================*/

const Deck operator+ ( const Deck & a)
{
vector::const_iterator b = a.Cards.begin();
vector::const_iterator e = a.Cards.end();
for(;b != e; ++b)
{
AddTop(*b);
}
return *this;
}
const Deck operator+= ( const Deck & a)
{
vector::const_iterator b = a.Cards.begin();
vector::const_iterator e = a.Cards.end();
for(;b != e; ++b)
{
AddTop(*b);
}
return *this;
}
const Deck operator+ ( const Card & a )
{
AddTop(a);
return *this;
}
vector operator/ ( unsigned int n )
{
vector d (n);

if( n % 2 )
return d;
if( n > Cards.size() )
return d;

unsigned int i=0,j=0,s=(unsigned int)(Cards.size() / n);

for(; i < n; ++i)
{
for(j=0; j < s; ++j)
d.AddBottom(TakeTop());
}

return d;
}
};

// Display Suit and Value
void DisplaySV(unsigned char sample);

#endif



deck.cpp

#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 S_SPADE:
cout << "S";
break;
case S_DIMON:
cout << "D";
break;
case S_CLUBS:
cout << "C";
break;
case S_HEART:
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 << " ";
}

bool Deck::Generate(unsigned int n)
{
if( (n > 1) && (n % 2) )
return false;

Cards.resize( n * 52 );

vector::iterator b = Cards.begin();
unsigned char suitn = 0x10;

for(unsigned int a=0;a < (n * 4);++a)
{
for(unsigned char i=1;i<0x0E;++i)
{
b->value = (unsigned char)(suitn + i);
++b;
}
suitn = suitn << 1;
if(!(suitn & 0xF0))
suitn = 0x10;
}

numCards = 52 * n;
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;
vector D;

srand((unsigned)time(NULL));
unsigned int ra = 5 + (rand() % 3);
unsigned int rn;

Display(); cout << endl;
for(unsigned int h = 0;h < ra; ++h)
{
*this = *this << (1 + (rand() % (sz/4)));
D = *this / 2;
D[0].Display(); cout << endl;
D[1].Display(); cout << endl;
for(unsigned int i = 0; i < sz ;)
{
rn = 1 + (rand() % 3);
for(unsigned int j=0;(j < rn && i < sz);++j)
{
unsigned int tmpD = D[0].numCards;
if(tmpD > 0)
{
Card C = D[0].TakeBottom();
AddTop(C);
++i;
}
}
rn = 1 + (rand() % 3);
for(unsigned int k=0;(k < rn && i < sz);++k)
{
unsigned int tmpD = D[1].numCards;
if(tmpD > 0)
{
Card C = D[1].TakeBottom();
AddTop(C);
++i;
}
}
}
}
Display(); cout << endl;
}

Card Deck::TakeBottom()
{
Card c;
vector::iterator i = Cards.begin();
c = *i;
Cards.erase(i,i + 1);
--numCards;
return c;
}

Card Deck::TakeTop()
{
Card c;
vector::iterator i = Cards.end() - 1;
c = *i;
Cards.erase(i,i + 1);
--numCards;
return c;
}



Even poof I can use it!

Quote:

Dealer: S6 S7 Points: 13
layer: S4 D3 Points: 7
Dealer takes a card..
S6 S7 H2 Points: 15
Dealer takes a card..
S6 S7 H2 SJ Points: 25
layer wins
layer Funds: 290
Press any key to continue . . .

I broke the player name somehow, and I still have not looked into it.
Sign in to follow this  


1 Comment


Recommended Comments

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!