How would you represent a deck of cards?

Started by
21 comments, last by vinb 19 years, 6 months ago
Quote:Original post by Guimo
<snip>

Pick a card... any card... and make sure it can't be picked again.

Sure, it can be done with your solution with some other ugly hack to make sure it doesn't behave odd in any given situation.

Oh btw, in your solution, a straight flush of clubs is better than a straight flush of hearts; should you re-use the class for a poker game later on. That is ofcourse, unless you use a switch section to detect which is worth more, or god forbid - tons of if statements. myCard.suit > hisCard.suit is in my opinion much cleaner. [oh]

Ugly hacks make code less (re)usable and maintainable, it's not an opinion; it's a fact.
Advertisement
Quote:Original post by graveyard filla
why would you want to store pointers to Card's??? is there some sort of inheritense tree going on with the cards that you'd like to take advantage of? otherwise, i dont see the point [smile]


One reason would be that, if you have identical cards in the deck (see post about using multiple decks) you don't really need to differenciate between them. So you can instead store the card properties separately (e.g. in a static array holding the values, the images...), and just store pointers into that array to materialize the cards.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Well, as promised, here's the code. I had a question regarding using pointers to objects. I am using a pointer to the game ojbects (the house, player1, the deck) for no other reason than because it's how I saw somebody else do it. Now that I look at the code, it seems that I'm not really gaining anything because I could still pass the objects around to the functions by reference if I created the objects on the stack. Is there anything to be gained by creating the objects on the heap?

main.cpp
#include <iostream>#include <iomanip>#include <windows.h> #include "blackjack.h"#include <ctime>#include <time.h>using namespace std ;//FUNCTIONSvoid updateScreen(const Player &player1, const Player &player2) ;void dealCards(Deck *theDeck, Player *player1, Player *player2) ;void hit(Deck *theDeck, Player *thePlayer) ;void writeGame(const Player *theHouse, const Player *player1, int totalscore) ;void writeHeadings() ;void gameloop(Deck *theDeck, Player *theHouse, Player *player1, int &totalscore) ;void resetDeck(Deck *theDeck) ;COORD setPoint(const int x, const int y) ;void SetConsolePosition(int x, int y) ;const WIN = 1 ;const LOSE = 0 ;const LEFT_MARGIN = 20 ;/////////////////////////////////////////////// MAIN/////////////////////////////////////////////void main(){	Deck *theDeck = new Deck() ;	int totalscore = 0 ;	char response ;	do {		Player *theHouse = new Player();		Player *player1 = new Player() ;		system("cls") ;		theHouse->setScore('!') ;		player1->setScore('!') ;		gameloop(theDeck, theHouse, player1, totalscore) ;		cout << "Play again? Y/N" ;		cin >> response ;		if (response == 'N' || response == 'n' ) break ;				resetDeck(theDeck);		delete theHouse ;		delete player1 ;		}while (true) ;	delete theDeck ;		system("pause") ;	}/////////////////////////////////////////////void gameloop(Deck *theDeck, Player *theHouse, Player *player1,int &totalscore) {	int cardCount = 0 ;	dealCards(theDeck, theHouse, player1) ;	cardCount = 2 ;	// write to screen	for (int i=0;i<2;i++){		writeGame(theHouse, player1, totalscore) ;		}	char response ;	bool hitTheHouse = true;	do{		if ( theHouse->getScore() == 21) {			cout << "YOU LOOSE!!!" << endl ;			totalscore -= 10 ;			hitTheHouse = false ;			break ;			}		if (player1->getScore() == 21){			cout << "YOU WIN!!!" << endl ;			totalscore += 10 ;			hitTheHouse = false ;			break ;			}		SetConsolePosition(0,3) ;		cout << "Hit? (Y or N)" ;		cin >> response;		if (response == 'N' || response == 'n') break ;				hit(theDeck, player1) ;				writeGame(theHouse, player1, totalscore) ;		cardCount++ ;				if (player1->getScore() > 21){			cout << "BUSTED!!!" << endl;			totalscore -= 10 ;			hitTheHouse = false ;			break ;			}		if (player1->getScore() == 21){			cout << "YOU WIN!!!" << endl ;			totalscore += 10 ;			hitTheHouse = false ;			break ;			}	} while (true) ;	// hit the house until player1 loses or the house busts.	while (hitTheHouse){		if ( theHouse->getScore() <= player1->getScore() ) {			hit(theDeck, theHouse ) ;			}		writeGame(theHouse, player1, totalscore) ;		cardCount++ ;		if ( theHouse->getScore() > 21) {			cout << "YOU WIN!!!" << endl ;			totalscore += 10 ;			break ;			}		if ( theHouse->getScore() == 21) {			cout << "YOU LOOSE!!!" << endl ;			totalscore -= 10 ;			break ;			}		if ( theHouse->getScore() > player1->getScore() && theHouse->getScore() < 21 ){			cout << "YOU LOOSE!!!" << endl ;			totalscore -= 10 ;			break ;			}		} ;	}/////////////////////////////////////////////void SetConsolePosition(int x, int y){	COORD point = setPoint(x, y) ; 	HANDLE stdCon = GetStdHandle(STD_OUTPUT_HANDLE);	SetConsoleCursorPosition(stdCon, point);}/////////////////////////////////////////////void writeHeadings(){	SetConsolePosition(LEFT_MARGIN,3) ;	cout << setw(11) << "House:    "  << setw(11) << "You:      " << setw(11) << "Bank" << endl ;		SetConsolePosition(LEFT_MARGIN,4) ;	cout << "----------------------------------------" << endl ;	}/////////////////////////////////////////////COORD setPoint(const int x, const int y){	COORD point = {x,y} ;	return point ;}/////////////////////////////////////////////void writeGame(const Player *theHouse, const Player *player1, int totalscore){ ;	system("cls") ;	writeHeadings() ;	cout << endl ;	int houseTotal = 0, player1Total = 0 ;	int colspace = 5;	for (int i=0;i<10;i++){				if(player1->hand.getName() == '!' && theHouse->hand.getName() == '!' ) break ;		SetConsolePosition(LEFT_MARGIN,i+6) ;				houseTotal += theHouse->hand.getVal() ;						cout << setiosflags (ios_base::left) ;		cout << theHouse->hand.getSuit() ;		cout << setw(colspace) ;		cout << setiosflags (ios_base::left);		cout << theHouse->hand.getName() ;		cout << setw(colspace) << setiosflags (ios_base::left);		cout << houseTotal  ;							player1Total += player1->hand.getVal() ;				cout << setiosflags (ios_base::left);		cout << player1->hand.getSuit()   ;		cout << setw(colspace) ;		cout << setiosflags (ios_base::left);		cout << player1->hand.getName() ;					cout << setw(colspace) << setiosflags (ios_base::left) ;		cout << player1Total;		cout << setiosflags (ios_base::left);		cout << setw(colspace) << setiosflags (ios_base::left) ;		cout << "$" << totalscore  ;				cout << endl ;			}}/////////////////////////////////////////////void dealCards(Deck *theDeck, Player *theHouse, Player *player1){		// start with 2 cards	srand(time(NULL)) ;	int index, i ;	// deal the house	for (i=0; i<2; i++){		index = rand() % DECK_SIZE ;		theHouse->hand = theDeck->getCard(index);		theHouse->setScore( theDeck->getCard(index).getVal() ) ;		theDeck->getCard(index).Use() ;	}	// deal player 1	for (i=0; i<2; i++){		index = rand() % DECK_SIZE ;		player1->hand = theDeck->getCard(index);		player1->setScore( theDeck->getCard(index).getVal() ) ;		theDeck->getCard(index).Use() ;	}}/////////////////////////////////////////////void hit(Deck *theDeck, Player *thePlayer){		int i=0, index ;	do{		if (thePlayer->hand.getName() == '!') break;		i++;	}while (true);    do{		index = rand() % DECK_SIZE ;		if (!theDeck->getCard(index).Used() ){			thePlayer->hand = theDeck->getCard(index);			thePlayer->setScore( theDeck->getCard(index).getVal() ) ;			theDeck->getCard(index).Use() ;			break ;		}	} while (theDeck->getCard(index).Used()) ;}/////////////////////////////////////////////void resetDeck(Deck *theDeck){	for (int i=0;i<DECK_SIZE;i++){		theDeck->getCard(i).PutBack() ;		}	}/////////////////////////////////////////////int WinOrLose(Player *thePlayer){		return 0 ;}


blackjack.cpp
#ifndef BLACKJACK	#define BLACKJACK#include "blackjack.h"#include <iostream>#include <stdlib.h>#include <stdio.h> using namespace std; ////////////////////////////////////////////// PLAYER ////////////////////////////////////////////int Player::getScore() const {	return score ;}/////////////////////////////////////////////void Player::setScore(char cardValue){	if (cardValue == '!')		score = 0 ;	}/////////////////////////////////////////////void Player::setScore(int cardValue){	score+=cardValue ;	}/////////////////////////////////////////////int Player::getStatus(){	return status ;}/////////////////////////////////////////////void Player::stand(){}/////////////////////////////////////////////Player::Player(){	this->score = 0 ;	this->status = 0 ;	for(int i=0;i<10;i++)		hand.setName('!') ;}/////////////////////////////////////////////Player::~Player(){}/////////////////////////////////////////////void Player::setHand(Card card){	}////////////////////////////////////////////// CARD////////////////////////////////////////////Card::Card(){	value = 0 ;	name = ' ' ;	suit = ' ';	used = false ;}/////////////////////////////////////////////Card::~Card(){}/////////////////////////////////////////////char Card::getName() const {	return name ;}/////////////////////////////////////////////void Card::setName(int i){		if (i==0 || i==13 || i==26 || i==39) name = '2'  ; 	if (i==1 || i==14 || i==27 || i==40) name = '3'  ; 	if (i==2 || i==15 || i==28 || i==41) name = '4'  ; 	if (i==3 || i==16 || i==29 || i==42) name = '5'  ; 	if (i==4 || i==17 || i==30 || i==43) name = '6'  ; 	if (i==5 || i==18 || i==31 || i==44) name = '7'  ; 	if (i==6 || i==19 || i==32 || i==45) name = '8'  ; 	if (i==7 || i==20 || i==33 || i==46) name = '9'  ; 	if (i==8 || i==21 || i==34 || i==47) name = 'T'  ; 	if (i==9 || i==22 || i==35 || i==48) name = 'J' ;	if (i==10 || i==23 || i==36 || i==49) name = 'Q' ;	if (i==11 || i==24 || i==37 || i==50) name = 'K' ;	if (i==12 || i==25 || i==38 || i==51) name = 'A' ;}void Card::setName(char c) {	name = c ;}/////////////////////////////////////////////char Card::getSuit() const {	return suit ;}/////////////////////////////////////////////void Card::setSuit(int i){		if (i >=0 && i < 15) this->suit = 'H' ; 	if (i >=15 && i < 28) this->suit = 'D' ; 	if (i >=28 && i < 41) this->suit = 'S' ; 	if (i >=41) this->suit = 'C' ; }/////////////////////////////////////////////int Card::getVal() const{	return value ;}/////////////////////////////////////////////void Card::setVal(int i){	if (i==0  || i==13 || i==26 || i==39) value = 2 ; 	if (i==1  || i==14 || i==27 || i==40) value = 3 ; 	if (i==2  || i==15 || i==28 || i==41) value = 4 ; 	if (i==3  || i==16 || i==29 || i==42) value = 5 ; 	if (i==4  || i==17 || i==30 || i==43) value = 6 ; 	if (i==5  || i==18 || i==31 || i==44) value = 7 ; 	if (i==6  || i==19 || i==32 || i==45) value = 8 ; 	if (i==7  || i==20 || i==33 || i==46) value = 9 ; 	if (i==8  || i==21 || i==34 || i==47) value = 10 ; 	if (i==9  || i==22 || i==35 || i==48) value = 10 ;	if (i==10 || i==23 || i==36 || i==49) value = 10 ;	if (i==11 || i==24 || i==37 || i==50) value = 10 ;	if (i==12 || i==25 || i==38 || i==51) value = 11 ;}//////////////////////////////////////////////void Card::Use(){	used = true ;}void Card::PutBack(){	used = false ;}//////////////////////////////////////////////bool Card::Used(){	return (used)? true : false ;}////////////////////////////////////////////// Deck////////////////////////////////////////////Deck::Deck(){	for (int i=0;i<DECK_SIZE;i++){		        card.setVal(i) ;         card.setSuit(i) ;        card.setName(i) ;			//	cout << card.getName() << "  "  ;	//	cout << card.getSuit() << "  "  ;	//	cout << card.getVal() << "  " << endl ;			} }/////////////////////////////////////////////Deck::~Deck(){	}/////////////////////////////////////////////void Deck::deal(){}/////////////////////////////////////////////int Deck::getIndex(){	return index ;}/////////////////////////////////////////////Card Deck::getCard(int i){	return this->card ;}#endif


blackjack.h
////////////////////////////////////////////// CARD INTERFACE////////////////////////////////////////////const int DECK_SIZE = 52;class Card{private:	char suit ;	char name ;	int value ;	bool used ;public:		Card();	~Card();	char getSuit() const ;	void setSuit(int i) ;	char getName() const;	void setName(int i) ;	void setName(char c) ;	int getVal() const;	void setVal(int i);	void Use() ;	void PutBack() ;	bool Used() ;};////////////////////////////////////////////// DECK INTERFACE////////////////////////////////////////////class Deck{private:	int index ;	Card card[DECK_SIZE] ;public:	Deck();	~Deck();	Card getCard(int card) ;	void deal() ;	int getIndex() ;	} ;////////////////////////////////////////////// PLAYER INTERFACE////////////////////////////////////////////class Player{private:		int score ;	int status ;public:	Player();	~Player();		Card hand[10] ;	int getStatus() ;     void stand() ;	void setScore(int cardValue) ;	void setScore(char cardValue) ;	int getScore() const ;	void setHand(Card card) ;	//void doubleDown();} ;

This topic is closed to new replies.

Advertisement