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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/* 17 und 4 */
#include <iostream> /* cout, cin, endl */
#include <bitset>
#include <ctime> /* time */
using namespace std;
const unsigned decks=2;
unsigned value[]{10, 11, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10};
string suit("dhsc"); // Diamonds, Hearts, Spades, Clubs
string avers("JA23456789TQK"); // you know it
//------------------------------------------------------------------------------
class Shoe
{
bitset<decks * 52 - 1> cf; // Card-Flags, 1 - in shoe, 0 - dealt out
public:
void shuffle()
{
cout << " new deck, shuffle... ";
cf.set();
}
int fortuity;
int deal(bool hidden)
{
do {fortuity = rand() % (decks * 52);} while (!cf[fortuity]);
cf[fortuity] = false;
if (hidden)
cout << "NOSHOW, ";
else
cout << suit[fortuity / 13 % 4] << avers[fortuity % 13] << ", ";
if (cf.count() < 52) shuffle();
return value[fortuity % 13]; // return card's value
}
};
////////////////////////////////////////////////////////////////////////////////
int main()
{
Shoe cards; // see above
unsigned sum_D, sum_P, aoh_D, aoh_P; // value and #aces in current hand
unsigned curav, hc; // current avers, hidden card of D.
char wuw; // What User Wants
bool ail_D, ail_P; // ace is low? (flag for "hard" and "soft")
/* prepare the table */
srand (time(NULL)); // haphazardly seed
cout << "Start\n"; // indicate c'est parti !
cout << "To control enter first letter: Hit or Card to get one more card,\n";
cout << "Stand or Rest for standing, Quit to leave.\n";
cards.shuffle();
once_more:
ail_D = false; // value of aces now 11
ail_P = false;
aoh_D = 0; // aces on hand
aoh_P = 0;
sum_D = 0; // totalized values
sum_P = 0;
/* deviation from _all_ usual dial sequence */
cout << "\nDialer: ";
sum_D = cards.deal(true);
hc = cards.fortuity; // remember hidden card for later
if ((hc % 13) == 1)
aoh_D = 1;
sum_D += cards.deal(0); // dealer upcard
curav = cards.fortuity % 13;
if ((hc % 13 + curav) == 1) // 0=J, 1 = A
{
cout << " BLACKJACK! ";
cout << " Dialer uncovers: " << suit[hc / 13 % 4] << avers[hc % 13];
goto D_wins;
}
if (curav == 1)
aoh_D += 1;
if (sum_D == 22) // two aces trigger soft -> hard
{
ail_D = true;
sum_D = 2;
}
/* one player only */
cout << "\nYou: ";
sum_P = cards.deal(0);
curav = cards.fortuity % 13;
if (curav == 1)
aoh_P = 1;
sum_P += cards.deal(0);
if ((cards.fortuity % 13 + curav) == 1)
{
cout << " BLACKJACK!\n";
goto P_wins;
}
if (cards.fortuity % 13 == 1)
aoh_P += 1;
if (aoh_P == 2) // two aces trigger low
{
ail_P = true;
sum_P = 2;
}
/* loop until player stands or busted */
for (;;)
{
cin >> wuw;
cin.clear();
cin.ignore(999, '\n');
switch (wuw)
{
case 'Q':
case 'q':
return (0); // Quit silently
case 'R':
case 'S':
case 'r':
case 's':
goto rest;
case 'C':
case 'H':
case 'c':
case 'h':
default:
curav = cards.deal(0);
if (cards.fortuity % 13 == 1)
aoh_P += 1;
}
if ((curav == 11) && ail_P)
curav -= 10;
sum_P += curav;
if (sum_P > 21)
{
if (aoh_P > 0 && !ail_P)
{
ail_P = true;
sum_P -= aoh_P * 10;
}
else
{
cout << " a bust! ";
goto D_wins;
}
}
} // end do forever
/* continue with dialers turn */
rest:
/* Would be nice for Dialer hole card to repeate also his upcard */
cout << "\nDialer hole card: " << suit[hc / 13 % 4] << avers[hc % 13] << ", ";
while (sum_D < 17)
{
curav = cards.deal(0);
if (cards.fortuity % 13 == 1)
aoh_D += 1;
if ((curav == 11) && ail_D)
curav -= 10;
sum_D += curav;
if (sum_D > 21)
{
if (aoh_D > 0 && !ail_D)
{
ail_D = true;
sum_D -= aoh_D * 10;
}
else
{
cout << " a bust! ";
goto P_wins;
}
}
}
/* the hand is over, now compare sums */
if (sum_P > sum_D)
goto P_wins;
D_wins:
cout << " Dialer wins " << sum_D << '/' << sum_P << '.';
goto again;
P_wins:
cout << " Player wins " << sum_P << '/' << sum_D << '.';
again:
cout << " Again? Y/n ";
cin >> wuw;
if (wuw != 'N' && wuw != 'n')
goto once_more;
// return 0;
}
| |