Below is a Black Jack text based game I developed in C++.

blackjack.cpp file
//============================================================================
// Name : blackjack.cpp
// Author : Rianne Trujillo
// Version :
// Copyright :
// Description : Black Jack in C++
//============================================================================
#include
#include
#include "dealer.h"
#include "player.h"
using namespace std;
int main() {
//display game title
cout << "BLACKJACK\n" << endl;
//player class + human instance
Player human;
//if playerName is set to default, ask for name
if (human.name=="default") {
std::string playerName;
cout << "Enter Your Name: ";
cin >> playerName;
human.name = playerName;
cout<<endl;
}
//Print Greeting
cout << "Welcome " << human.name << ". Let's Play!\n" << endl;
human.newGame();
return 0;
}
player.cpp file
/*
* player.cpp
*/
#include "player.h"
#include "dealer.h"
Player::Player () {
name="default";
}
void Player::play21(void) {
//randomize the cards
srand((int) time(0));
//classes+instances
Dealer dealer;
Player player;
// deal the cards
int person = dealer.dealCards(2, "Your Cards:");
std::cout << " = " << person;
std::cout << "\n";
int computer = dealer.dealCards(2, "Computer's Cards:");
std::cout <<" = " << computer;
std::cout << "\n";
// Ask if human wants a hit
//player.hit(person);
char takeHit = 'y';
while (takeHit != 'n') {
if (person < 21) {
//does the player want a hit?
std::cout << "\nDo you want a hit (y/n)? ";
std::cin >> takeHit;
//If yes
if (takeHit == 'y'){
//Deal a card
person += dealer.dealCards(1,"\nHit:");
std::cout << "\n";
//total
std::cout << "Total: " << person << "\n";
} else //the player does not want another card
takeHit ='n';
} else {
//the player has busted
if (person > 21)
std::cout <<"\nYOU BUSTED!\n";
takeHit ='n';
}
}
//Determine if computer takes a hit
while ((computer < person) && (computer <= 21) && (person <= 21)) {
std::cout << "\n";
computer += dealer.dealCards(1,"The Computer took a card: ");
}
//show who won.
dealer.determineWinner(person, computer);
}
void Player::newGame(void){
char keepPlaying;
//set variable for keepPlaying char to n
keepPlaying = 'n';
do {//do play21 function while keepPlaying = y
play21();
//ask for keepPlaying input
std::cout << "\nDo you want to play another hand (y/n)?";
std::cin >> keepPlaying;
std::cout << "\n";
} while (keepPlaying=='y');
if (keepPlaying=='n') {//if no, print game over
std::cout << "\nGAME OVER.\nThanks For playing!";
}
}
player.h file
/*
* player.h
*/
#ifndef PLAYER_H_
#define PLAYER_H_
#include
#include
class Player {
public:
// properties:
std::string name;
void play21(void);
void newGame(void);
Player();
private:
std::string playerName;
};
#endif /* PLAYER_H_ */
dealer.cpp file
/*
* dealer.cpp
*/
//dealer shuffles cards and
//gives card to players when they ask for hit
#include "dealer.h"
#include "player.h"
Dealer::Dealer() {
personScore = 0;
computerScore = 0;
}
int Dealer::dealCards(int numCards, std::string message){
//deal cards
//set cardDealt and totalValue to 0
int cardDealt = 0;
int totalValue = 0;
//print players cards to the screen
std::cout << message << " ";
//deal the number of required cards
for (int i = 1 ; i <= numCards ; i++){
//deal a card between 1 and 10
cardDealt = Shuffle(1, 10);
//if card dealt is equal to 1
if (cardDealt == 1){
//and if total value of card dealt is less than 10
if (totalValue+=cardDealt > 10)
//card is 11
cardDealt =11;
//card is 1
else {cardDealt =1;}
}
//accumulate the card values
//totalValue is equal to the number of cards dealt
totalValue += cardDealt;
std::cout << cardDealt << " ";
}
//return total value
return totalValue;
}
void Dealer::determineWinner(int person, int computer) {
this->computerScore=computer;
this->personScore=person;
//Display total scores
std::cout <<"\nYour Score: " << person;
std::cout <<"\nComputer Score: " << computer;
std::cout << "\n";
//Display winner
//if person is equal to computer, its a tie
//if person = 21, or => computer, or computer is > than 21, person wins
//else computer wins
if (person == computer)
std::cout << "\nTie";
else if ((person == 21 || person >= computer|| computer > 21) && (person <= 21))
std::cout <<"\nYou Won!\n";
else
std::cout <<"\nThe Computer Won!\n";
}
int Dealer::Shuffle(int lowerLimit, int upperLimit) {
//returns a random number within the given boundary
return 1 + rand() % (upperLimit - lowerLimit + 1);
}
dealer.h file
/*
* dealer.h
*/
#ifndef DEALER_H_
#define DEALER_H_
#include
#include
class Dealer {
public:
int dealCards(int numCards, std:: string message);
void determineWinner(int person, int computer);
int Shuffle(int lowerLimit, int upperLimit);
Dealer();
private:
int personScore;
int computerScore;
};
#endif /* DEALER_H_ */



You must be logged in to post a comment.