Well, as promised, here's the code for my black jack game. 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 (especially in the context of what I'm trying to do here)?
If you're as new to C++ as I am I hope you can get some benefit from the code ( even as an example of what NOT to do. :)
( Sorry for the double post, I mistakenly included this topi in response to another thread I had opened )
main.cpp
#include <iostream>
#include <iomanip>
#include <windows.h>
#include "blackjack.h"
#include <ctime>
#include <time.h>
using namespace std ;
//FUNCTIONS
void 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.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();
} ;
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) ;
}
}
/////////////////////////////////////////////
Deck::~Deck(){
}
/////////////////////////////////////////////
void Deck::deal(){
}
/////////////////////////////////////////////
int Deck::getIndex(){
return index ;
}
/////////////////////////////////////////////
Card Deck::getCard(int i){
return this->card ;
}
#endif