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
|
#include <cstdlib>
#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;
string suits[4] = {"Hearts", "Diamonds", "Spades", "Clubs"};
string ranks[13] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
int card_drawn[52];
int cards_remaining = 52;
//This function finds the Nth element of card_drawn and skipping over all those those elements already set to true.
int select_next_available(int n) {
int i = 0;
//at the beginning of deck, skip past cards already drawn.
while (card_drawn[i])
i++;
while (n-- > 0) { //do the following n times:
i++; //advanced next card
while (card_drawn[i]) //skip past cards
i++;// already drawn.
}//end while
card_drawn[i] = true; // note card to be drawn
return i; //return this number
} //end select_next_available function
int rand_0toN1(int n) {
return rand() % n;
} //end function
void draw_a_card(int &r, int &s, int &a){
int n, card;
n = rand_0toN1(cards_remaining--); //get random number from the remaining card availble
card = select_next_available(n);
r = card % 13; //random index (0 to 12) into rank array
s = card % 4; //random index (0 to 3) into suits array
cout << ranks[r] << " of " << suits[s] << endl;
a = a + r + 1;
cout << "Total: " << a << endl;
} //end draw a card
int main ()
{
int n, c, i;
int card, type, value;
string a;
// n == 2; This is a comparison operator ==
n = 2;
c = 1;
srand(time(NULL));
cout << "Blackjack" << endl;
cout << "Play (Y/N)" << endl;
cin >> a;
if (a == "y" || a == "Y")
{
for(i = 0; i < n; i++) // 0, 1, break gives you two iterations
draw_a_card(card,type);
}
while (1)
{
cout << "Hit?" << endl;
cin >> a;
if (a == "y" || a == "Y")
{
for (i = 0; i < c; i++)
draw_a_card(card, type, value);
}
else
cout << value << endl;
}
system("PAUSE");
return 0;
}//end main
|