-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCroupier.cpp
More file actions
168 lines (148 loc) · 4.51 KB
/
Croupier.cpp
File metadata and controls
168 lines (148 loc) · 4.51 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* Copyright 2013 James O'Hea
*
* This file is part of Blackjack.
*
* Blackjack is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Blackjack is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Blackjack. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Croupier.hpp"
/* Declare the names and values of all cards in a deck */
const char* Suits[] = {"Spades", "Hearts", "Diamonds", "Clubs"};
const int Values[] = {11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10};
const char* Names[] = {"Ace",
"Two",
"Three",
"Four",
"Five",
"Six",
"Seven",
"Eight",
"Nine",
"Ten",
"Jack",
"Queen",
"King"};
/* Constructor for the Croupier object */
Croupier::Croupier()
{
int DeckNumber;
int SuitNumber;
int CardNumber;
int ShoePosition;
NextCard = -1;
HandValue = 0;
AcesHeld = 0;
/* Populate the shoe with cards for all decks */
for(DeckNumber = 0; DeckNumber < NumberOfDecks; DeckNumber++)
{
for(SuitNumber = 0; SuitNumber < SuitsPerDeck; SuitNumber++)
{
for(CardNumber = 0; CardNumber < CardsPerSuit; CardNumber++)
{
ShoePosition = (DeckNumber * (CardsPerDeck)) + (CardNumber + (SuitNumber * CardsPerSuit));
Shoe[ShoePosition].SetSuit(Suits[SuitNumber]);
Shoe[ShoePosition].SetName(Names[CardNumber]);
Shoe[ShoePosition].SetValue(Values[CardNumber]);
}
}
}
/* Place the cut card at the back of the shoe ready to be placed during the shuffle */
Shoe[ShoeSize].SetSuit("Cut");
Shoe[ShoeSize].SetName("Cut");
Shoe[ShoeSize].SetValue(0);
/* Shuffle the complete shoe */
Shuffle();
}
/* Deal out the next card of the shoe */
const Card Croupier::Deal()
{
Card TempStore;
NextCard++;
/* When the cut card is reached, reshuffle the shoe */
string CompareString = Shoe[NextCard].GetName();
if (CompareString.compare("Cut") == 0)
{
cout << "Cut card reached. Reshuffling cards...." << endl << endl;
/* Move the cut card to the end of the pack so that it isn't shuffled */
TempStore = Shoe[ShoeSize];
Shoe[ShoeSize] = Shoe[NextCard];
Shoe[NextCard] = TempStore;
Shuffle();
NextCard = 0;
}
return Shoe[NextCard];
}
/* Take the ordered shoe and shuffle it */
void Croupier::Shuffle()
{
int i, k;
int RandomNumber;
Card TempStore;
/* Initialise the random number generator using time as a seed */
srand (time(NULL));
/* Use the Fisher-Yates / Knuth algorithm to shuffle the shoe */
for(i = ShoeSize; i > 0; i--)
{
/* Generate a random between 1 and the available shoe size */
RandomNumber = rand() % i;
/* Pick out a random card */
TempStore = Shoe[RandomNumber];
/* Shift all sequential cards down by one position to fill the gap */
for(k = RandomNumber; k < (ShoeSize - 1); k++)
{
Shoe[k] = Shoe[k + 1];
}
/* Place the chosen card at the end of the pack */
Shoe[ShoeSize - 1] = TempStore;
}
/* Place the cut card randomly in the last quater of the shoe */
/* Find the first card of the last quarter of the shoe */
int Minimum = (ShoeSize - (ShoeSize / 4));
/* Pick a random card from the last quarter of the pack /*
/* Swap it with the cut card (which always starts as the/*
/* the last card in the shoe) */
RandomNumber = rand() % (ShoeSize - Minimum) + Minimum;
TempStore = Shoe[RandomNumber];
Shoe[RandomNumber] = Shoe[ShoeSize];
Shoe[ShoeSize] = TempStore;
}
/* List all the cards in the shoe */
void Croupier::List() const
{
int i;
/* Run through all the cards in the shoe (including the cut card, hence the +1) */
for(i = 0; i < ShoeSize + 1; i++)
{
cout << i << " " << Shoe[i].GetName() << " of " << Shoe[i].GetSuit() << " has the value " << Shoe[i].GetValue() << endl;
}
}
/* Declare the dealers upcard */
void Croupier::InitialStatus() const
{
cout << "Dealer's upcard is the " << Hand.front().GetName() << " of " << Hand.front().GetSuit() << endl << endl;
}
/* State the dealer's hand */
void Croupier::Status() const
{
cout << "Dealer is holding: " << endl << endl;
ListHand();
StatusResult();
}
/* Reset all flags of the dealer */
void Croupier::ReturnCards()
{
Hand.clear();
HandValue = 0;
AcesHeld = 0;
}