Code:
#include <afxwin.h>
#include <strstrea.h>
#include "resource.h"
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
using std::string;
using std::vector;
#define DEALER_WINS 1
#define PLAYER_WINS 2
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
class Card {
public:
string suit, face;
string ToString();
int value;
protected:
private:
};
Card c;
// -----------------------------------------------------------------------------
static const string SUIT[4] = {"Hearts", "Diamonds", "Clubs", "Spades" };
static const string FACE[13] = {"Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack",
"Queen", "King"};
// -----------------------------------------------------------------------------
string Card::ToString()
{
return face+" of "+suit;
}
// -----------------------------------------------------------------------------
//------------------------------------------------------------------------------
class Deck {
public:
Deck();
~Deck();
void Shuffle(void);
Card DealCard(void);
protected:
private:
vector<Card> cards;
void CreateCards(void);
void SwapCards(int, int);
};
Deck d;
// -----------------------------------------------------------------------------
Deck::Deck()
{
Shuffle();
}
// -----------------------------------------------------------------------------
Deck::~Deck()
{
cards.clear();
}
// -----------------------------------------------------------------------------
void Deck::CreateCards(void)
{
cards.clear();
Card c;
int s, f;
for (s=0; s < 4; s++) { // loop for all suits
for (f=0; f < 13; f++) { // loop for all faces
c.face = FACE[f];
c.suit = SUIT[s];
c.value = f + 1;
if (c.value > 10) // make sure face cards' values = 10
c.value = 10;
cards.push_back(c);
}
}
}
// -----------------------------------------------------------------------------
void Deck::Shuffle(void)
{
CreateCards();
srand((unsigned)time(NULL));
int i, random_pos;
for (i = 0; i < 52; i++) {
random_pos = rand() % 52;
SwapCards(i, random_pos);
}
}
// -----------------------------------------------------------------------------
void Deck::SwapCards(int pos1, int pos2)
{
Card temp;
temp = cards[pos1];
cards[pos1] = cards[pos2];
cards[pos2] = temp;
}
// -----------------------------------------------------------------------------
Card Deck::DealCard(void)
{
Card rt;
if (!cards.empty()) {
rt = cards[0];
cards.erase(cards.begin());
}
return rt;
}
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
class Hand {
public:
void AddCard(Card);
Card FirstCard(void);
int Value(void);
int value;
protected:
private:
vector<Card> cards;
};
// -----------------------------------------------------------------------------
void Hand::AddCard(Card c)
{
cards.push_back(c);
value += c.value;
}
// -----------------------------------------------------------------------------
int Hand::Value(void)
{
int sum = 0;
for (int i=0; i < cards.size(); i++)
sum += cards.at(i).value;
return sum;
}
// -----------------------------------------------------------------------------
Card Hand::FirstCard(void)
{
// precondition is that the cards vector is not empty - which sould always be true here
return cards.at(0);
}
//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
class CMainWindow : public CDialog
{
public:
CMainWindow() : CDialog(MAIN) {};
afx_msg void OnAdd();
afx_msg void OnStay();
afx_msg void OnQuit();
BOOL CMainWindow::OnInitDialog();
private:
DECLARE_MESSAGE_MAP()
};
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
class CStatusWindow : public CDialog
{
public:
CStatusWindow() : CDialog(STATUS) {};
BOOL CStatusWindow::OnInitDialog();
afx_msg void OnAgain();
afx_msg void OnQuit();
private:
DECLARE_MESSAGE_MAP()
};
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
class InternalInfo{
public:
InternalInfo();
~InternalInfo();
Hand player_hand;
Hand dealer_hand;
int winner;
int dealer_wins;
int player_wins;
CListBox* pData; //data pointer for player
CListBox* pData2; //data pointer for dealer
CListBox* pAdd; //data pointer for player added cards
CEdit* pTotal1; //data pointer for player score
CEdit* pTotal2; //data pointer for dealer score
int sw;
CStatusWindow second;
CListBox* pAddDealer;
private:
};
InternalInfo data;
//----------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
BOOL CMainWindow::OnInitDialog()
{
// deal the intial two cards to player and dealer
CDialog::OnInitDialog();
data.pData= (CListBox*) GetDlgItem(IDC_LIST1);
//card 1 for player
c = d.DealCard();
c.ToString();
data.player_hand.AddCard(c);
data.pData->InsertString(-1, c.ToString().c_str());
data.pTotal1=(CEdit*) GetDlgItem(IDC_TOTAL1);
char buffer6[30];
ostrstream str6(buffer6, 30);
str6.seekp(0);
str6<<data.player_hand.Value()<<ends;
data.pTotal1->SetWindowText(buffer6);
//end card 1 for player
//card 2 for player
c = d.DealCard();
c.ToString();
data.player_hand.AddCard(c);
data.pData->InsertString(-1, c.ToString().c_str());
char buffer7[30];
ostrstream str7(buffer7, 30);
str7.seekp(0);
str7<<data.player_hand.Value()<<ends;
data.pTotal1->SetWindowText(buffer7);
//end of card 2 for player
data.pData2= (CListBox*) GetDlgItem(IDC_LIST2);
//card 1 for dealer
c = d.DealCard();
c.ToString();
data.dealer_hand.AddCard(c);
data.pData2->InsertString(-1, c.ToString().c_str());
data.pTotal2=(CEdit*) GetDlgItem(IDC_TOTAL2);
char buffer8[30];
ostrstream str8(buffer8, 30);
str8.seekp(0);
str8<<data.dealer_hand.Value()<<ends;
data.pTotal2->SetWindowText(buffer8);
//end card 1 for dealer
//card 2 for dealer
c = d.DealCard();
c.ToString();
data.dealer_hand.AddCard(c);
//end card 2 for dealer
return TRUE;
}
//--------------------------------------------------------------------------------------
afx_msg void CMainWindow::OnAdd()
{
c = d.DealCard();
c.ToString();
data.player_hand.AddCard(c);
data.pAdd=(CListBox*) GetDlgItem(IDC_LIST1);
data.pAdd->InsertString(-1, c.ToString().c_str());
data.pTotal1=(CEdit*) GetDlgItem(IDC_TOTAL1);
char buffer3[30];
ostrstream str3(buffer3, 30);
str3.seekp(0);
str3<<data.player_hand.Value()<<ends;
data.pTotal1->SetWindowText(buffer3);
if (data.player_hand.Value() > 21)
{ data.winner = DEALER_WINS;
MessageBox("D'OH!", "Sorry", MB_OK);}
}
//------------------------------------------------------------------------------------
afx_msg void CMainWindow::OnStay()
{
data.sw=data.second.DoModal();
if(data.sw==IDC_QUIT){
MessageBox("thanks", "thanks", MB_OK);
SendMessage(WM_CLOSE);}
data.pAddDealer=(CListBox*) GetDlgItem(IDC_LIST2);
data.winner=PLAYER_WINS;
if (data.player_hand.Value() > 21)
data.winner = DEALER_WINS;
else {
// make the dealer try to win until he "busts" or matches/exceeds player's hand
while (data.dealer_hand.Value() < data.player_hand.Value()) {
c = d.DealCard();
data.dealer_hand.AddCard(c);
data.pAddDealer->InsertString(-1, c.ToString().c_str());
CEdit* pTotal2;
pTotal2=(CEdit*) GetDlgItem(IDC_TOTAL2);
char buffer2[30];
ostrstream str2(buffer2, 30);
str2.seekp(0);
str2<<data.dealer_hand.Value()<<ends;
pTotal2->SetWindowText(buffer2);
}
// if the dealer didn't "bust", then he must have won (b/c loop only ends on win or bust)
if (data.dealer_hand.Value() <= 21)
data.winner = DEALER_WINS;
}
}
//------------------------------------------------------------------------------------
afx_msg void CMainWindow::OnQuit()
{
MessageBox("Thanks for playing blackjack", "GAME OVER", MB_OK);
SendMessage(WM_CLOSE);
}
BEGIN_MESSAGE_MAP(CMainWindow, CDialog)
ON_COMMAND(IDC_HIT, OnAdd)
ON_COMMAND(IDC_STAY, OnStay)
ON_COMMAND(IDC_QUIT, OnQuit)
END_MESSAGE_MAP()
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
BOOL CStatusWindow::OnInitDialog()
{
CDialog::OnInitDialog();
return TRUE;
}
//------------------------------------------------------------------------------------
afx_msg void CStatusWindow::OnAgain()
{
MessageBox("Lets play another", "New Game", MB_OK);
data.pData->ResetContent();
data.pData2->ResetContent();
data.pTotal1->Clear();
data.pTotal2->Clear();
SendMessage(WM_CLOSE);
}
//------------------------------------------------------------------------------------
afx_msg void CStatusWindow::OnQuit()
{
MessageBox("Thanks for playing blackjack", "GAME OVER", MB_OK);
SendMessage(WM_CLOSE);
}
//------------------------------------------------------------------------------------
BEGIN_MESSAGE_MAP(CStatusWindow, CDialog)
ON_COMMAND(IDOK, OnAgain)
ON_COMMAND(IDC_QUIT, OnQuit)
END_MESSAGE_MAP()
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
class CMainApp : public CWinApp
{
BOOL InitInstance(){
int sw=IDOK;
CMainWindow main;
while(sw==IDOK){sw=main.DoModal();}
return FALSE;
}
};
CMainApp app;
//------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
InternalInfo::InternalInfo()
{
dealer_wins=0;
player_wins=0;
}
//-----------------------------------------------------------------------------------
InternalInfo::~InternalInfo()
{
dealer_wins=0;
player_wins=0;
}
//------------------------------------------------------------------------------------
//----------------------------------------------------------------------------------
--