-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackjack.cpp
116 lines (92 loc) · 2.65 KB
/
blackjack.cpp
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
// BlackJack Game from Dealer Simulation
//including the libraries
#include <iostream>
#include <random>
using namespace std;
//Global
random_device rd;
//Declaration
int NextCard();
bool DealerStatus(int currentscore);
int simulateGame();
void keepTrack(int input);
int main()
{
//input declaration
int input;
//Printing out intro messages
cout << "Welcome To BlackJack, this program just simulates the probabilities of the dealer bustimg " <<'\n' <<endl;
cout << "How many games do you want to simulate" << endl;
//Taking input from user of how times the want to run the simulation
cin >> input;
//Calling the fuction of the multiple games
keepTrack(input);
return 0;
}
//Generates next card at ramdom
int NextCard()
{
//random funtion
int numGenerated;
mt19937 mt(rd());
uniform_int_distribution<>dis(1, 13);
//assigning the random number to this
numGenerated = dis(mt);
return numGenerated;
}
//function to check dealer is busted or not
bool DealerStatus(int currentscore)
{
//Checking if Dealer busted or not
return currentscore >= 22 || currentscore >= 17;
}
//function that perform one pass of the game
int simulateGame()
{
int currentscore = 0;
int card[13] = {2,3,4,5,6,7,8,9,10,10,10,10,11};
while (DealerStatus(currentscore)==false)
{
//picking random card;
int temp = card[NextCard()];
//If the score is more than just stop it
if (currentscore >= 17)
break;
// If the temp is not 11, just add normally
if(temp!=11)
{
currentscore+=temp;
}
// If the score is 11 and between 6 and 10, it can add 11 because it wont bust
else if (temp == 11 && (currentscore >= 6 && currentscore <= 10))
{
currentscore += 11;
}
//But if not, it will add 1
else
{
currentscore += 1;
}
}
return currentscore;
}
//function to calculate the busts
void keepTrack(int input)
{
int hold = 0;
int bust = 0;
//for loop to run multiple
for (int i=0; i<input; i++)
{
//Setting the score to the simulateGame function
int score = simulateGame();
//if score is more then 22 then busts then hold.
if (score >= 22)
bust += 1;
else
hold += 1;
}
//Printing the Probability and amount of games it busts
cout<< bust <<" games out of the total "<< input << " games were busted"<<endl;
cout<<"Probability of busts for the given input = " << ((float)bust/input) * 100 << "%"<<endl;
}