card.h
enum suit {clubs, diamonds, hearts, spades};
class Card {
public:
Card(suit = clubs, int = 1);
suit su() {return f; }
int value() {return v; }
void write_out();
private:
suit f;
int v;
int s;
};
card.cpp
#include "card.h"
#include <cassert>
#include <iostream>
using namespace std;
Card::Card(suit ss, int vv)
{
assert(vv >=1 && vv<=13);
s=ss;
v=vv;
}
void Card::write_out()
{
const char *tab1[] = {"Clubs", "Diamonds", "Hearts", "Spades"};
const char *tab2[] = {"Jack", "Queen", "King"};
cout << tab1[ s ] << ' ';
if (v == 1)
cout << "Ace";
else if (v <= 10)
cout << v;
else
cout << tab2[v-11];
}
cardstack.h
#include "card.h"
#include <vector>
using namespace std;
class Cardstack {
public:
Cardstack() {stack.reserve(52);}
void throw_cards() {stack.clear();}
int number_cards() { return stack.size();}
Card look_at(int no);
Card deal_top();
void lay_top(Card k);
void new_pack();
void shuffle();
private:
vector<Card> stack;
};
cardstack.cpp
#include "cardstack.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
Card Cardstack::look_at(int no)
{
return stack.at(stack.size() -no);
}
Card Cardstack::deal_top()
{
Card top = stack.back();
stack.pop_back();
return top;
}
void Cardstack::lay_top(Card k)
{
stack.push_back(k);
}
void Cardstack::new_pack()
{
stack.clear();
for (suit s=clubs; s<spades; s=suit(s+1))
for (int v=1; v<=13; v++)
stack.push_back(Card(s,v));
}
void Cardstack::shuffle()
{
srand(time(0));
for (int i=1; i<1000; i++)
{
int n1 = rand() % stack.size();
int n2 = rand() % stack.size();
Card temp = stack[n1];
stack[n1] = stack[n2];
stack[n2] = temp;
}
}
player.h
#include "cardstack.h"
class Player {
public:
Player(Cardstack& cardpack, bool is_computer)
: pack(cardpack), computer(is_computer) {};
int play();
private:
Cardstack hand;
Cardstack& pack;
const bool computer;
int points();
};
player.cpp
#include "player.h"
#include <iostream>
using namespace std;
main()
{
Cardstack pack;
Player you (pack, false);
Player I (pack, true);
char answer[10];
cout << "Welcome to Black Jack, the game of twenty-one!" << endl;
while (true)
{
cout << "New game? "; cin >> answer;
if (answer[0] != 'y') break;
pack.new_pack();
pack.shuffle();
int p1 = you.play();
if (p1 > 21)
cout << "Sorry, you busted. You lose." << endl;
else if (p1 == 21)
cout << "You Win!" << endl;
else
{
//the computer must play
int p2 = I.play();
if (p2 <=21 && p2 >=p1)
cout << "You lost!" << endl;
else
cout << "You won!" << endl;
}
}
}
points.cpp
#include "player.h"
#include "card.h"
#include <iostream>
using namespace std;
int Player::points()
{
int p = 0, number_aces = 0;
for (int i = 1; i <= hand.number_cards(); i++)
{
int v = hand.look_at(i).value();
if (v == 1) {
p+= 14;
number_aces++;
}
else
p += v;
}
for (int j=1; j <= number_aces && p > 21; j++)
p -= 13; // counts an ace as 1
return p;
}
play.cpp
#include "points.cpp"
#include <iostream>
using namespace std;
int Player::play()
{
bool contine = true;
int p;
while (contine)
{
Card k = pack.deal_top();
hand.lay_top(k);
p = points();
if (computer) {
cout << "The computer got "; k.write_out();
cout << endl;
if (p >= 16)
{
cout << "The computer has " << p << " points" << endl;
contine = false;
}
}
else
{ //Person
cout << "You got "; k.write_out();
cout << " and have " << p << " points" << endl;
if (p < 21)
{
char answer[10];
cout << "One more card? Indicate 'y' for yes or 'n' for no "; cin >> answer; // this needs to be fixed
contine = answer[0] == 'y';
}
else
contine = false;
}
}
hand.throw_cards();
return p;
}
Any help will be appriciated.











