Game of BlackJack


This is for reference for anyone to make a game of BlackJack..
I just want to know if i could improve the codes e.g. make it shorter.
Please feel free to tell me what you think of my codes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <windows.h>

using namespace std;

class BuildHand {
		int addCard();
	public:
		int addCardToHand(bool&);
		void announceWinner(int, int);
} draw; 

void showOpenCard(int);
int PlayerHand();
int drawPlayerCard(bool& hasAce);
bool keepHand();
bool isOver();

int main() {
	srand(time(0));
	do {
		cout << "Welcome to the game of Blackjack"<< endl;
		cout << "--------------------------------"<< endl << endl;
		Sleep(300);
		bool hasAce = false;
		int House_hand = draw.addCardToHand(hasAce);
		showOpenCard(House_hand);
		Sleep(1000);
		cout << "Your turn" << endl << endl;
		Sleep(2000);
		int Player_hand = PlayerHand();
		while(House_hand < 17)
		{
			House_hand += draw.addCardToHand(hasAce);
			if(House_hand > 21 && hasAce)
				House_hand -= 10;
			showOpenCard(House_hand);
		}
		draw.announceWinner(Player_hand, House_hand);
	} while(isOver());
	system("pause>0");
	return 0;
}
int BuildHand::addCard() {
	return(rand()%13+1);
}
int BuildHand::addCardToHand(bool& ace) {
	int hand  = 0;
	hand = addCard();
	if(hand == 1 && !ace)
	{
		hand = 11;
		ace = !ace;
	}
	else if(hand >= 10)
		hand = 10;
	return hand;
}
void showOpenCard(int p) {
	cout << "The house draws... " << p << endl << endl;
}
int PlayerHand() {
	bool hasAce = false;
	int hand = drawPlayerCard(hasAce);
	cout << "Total card in hand is " << hand;
	if(hasAce)
		cout << " and you have an Ace" << endl;
	cout << endl << endl;
	while(keepHand()) {
		hand += draw.addCardToHand(hasAce);
			cout << "Total card in hand is " << hand << endl;;
		if(hasAce)
			cout << " and you have an Ace" << endl;
		if(hand > 21 && hasAce)
		{
            hasAce = !hasAce;    
			hand-=10;
        }
		else if (hand > 21)
			return hand;
	}
	return hand;
}
int drawPlayerCard(bool& hasAce) {
	cout << "drawing 2 cards" << endl;
	int x = draw.addCardToHand(hasAce);
	int y = draw.addCardToHand(hasAce);
	cout << "Your two cards is "<< x << " and " << y << endl;
	return (x+y);
}
bool keepHand() {
	char input;
	while(true) {
	cout << "Do you want to keep the hand? [Y] or [N] ";
	cin >> input;
	if(toupper(input) == 'Y')
		return false;
	else if(toupper(input) == 'N')
		return true;
	else
		cout << "Invalid input" << endl;
	}
}
bool isOver() {
	char input;
	while(true) {
	cout << "Do you want to play again? [Y] or [N] ";
	cin >> input;
	if(toupper(input) == 'Y') {
		system("cls");
    	return true;
    }
	else if(toupper(input) == 'N')
		return false;
	else
		cout << "Invalid input" << endl;
	}	
}
void BuildHand::announceWinner(int x, int y) {
	if(x > 21 && y <= 21)
		cout << "The house wins" << endl;
	else if(y > 21 && x <= 21)
		cout << "The house lose. You win" << endl;
	else if(y > 21 && x > 21)
		cout << "The game is draw" << endl;
	else if(x == y)
		cout << "The game is draw" << endl;
	else if(x > y)
		cout << "The house lose. You win" << endl;
	else
		cout << "The house wins" << endl;
}

..
Last edited on
Topic archived. No new replies allowed.