These are from 2 December. The first one builds a deck of cards using the “parallel arrays” technique. The second one (and the basis for assignment 11) converts the parallel arrays into on array of structures.
// cards.cpp -- start of a Poker game!
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// A playing card is one of 4 suits
const int HEARTS = 0;
const int CLUBS = 1;
const int SPADES = 2;
const int DIAMONDS = 3;
const int NUM_SUITS = 4;
// and 13 ranks (A,2,3,4,5,6,7,8,9,10,J,Q,K)
const int ACE = 1;
const int JACK = 11;
const int QUEEN = 12;
const int KING = 13;
const int NUM_RANKS = 13;
const int NUM_CARDS = NUM_SUITS * NUM_RANKS; // 52
void print_card(int rank, int suit)
{
switch(rank) {
case ACE: cout << "Ace"; break;
case JACK: cout << "Jack"; break;
case QUEEN: cout << "Queen"; break;
case KING: cout << "King"; break;
default: cout << rank; break;
}
cout << " of ";
switch(suit) {
case HEARTS: cout << "hearts"; break;
case CLUBS: cout << "clubs"; break;
case SPADES: cout << "spades"; break;
case DIAMONDS: cout << "diamonds"; break;
}
}
int main()
{
// Initialize the random numbers
srand(time(NULL));
// Pick random rank and suit -- problem
// with this is we can pick the same card
// twice, so it doesn't simulate dealing
// from a deck very well.
//int rank = rand() % 13 + 1; // 1-13
//int suit = rand() % 4;
cout << "Hello, welcome to the game.\n";
int rank[NUM_CARDS]; // parallel arrays technique
int suit[NUM_CARDS];
int i = 0; // which card in the deck
// Create the deck
for(int s = 0; s < NUM_SUITS; s++)
{
for(int r = 1; r <= NUM_RANKS; r++, i++)
{
rank[i] = r;
suit[i] = s;
}
}
// Shuffle cards:
for(i = 0; i < 1000; i++) { // # of iterations
// Pick two cards in the deck
int j = rand() % NUM_CARDS;
int k = rand() % NUM_CARDS;
// Swap them
int r = rank[j];
int s = suit[j];
rank[j] = rank[k];
suit[j] = suit[k];
rank[k] = r;
suit[k] = s;
}
// Print entire deck
for(i = 0; i < NUM_CARDS; i++)
{
print_card(rank[i], suit[i]);
cout << "\n";
}
return 0;
}
// cards.cpp -- start of a Poker game!
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// A playing card is one of 4 suits
const int HEARTS = 0;
const int CLUBS = 1;
const int SPADES = 2;
const int DIAMONDS = 3;
const int NUM_SUITS = 4;
// and 13 ranks (A,2,3,4,5,6,7,8,9,10,J,Q,K)
const int ACE = 1;
const int JACK = 11;
const int QUEEN = 12;
const int KING = 13;
const int NUM_RANKS = 13;
const int NUM_CARDS = NUM_SUITS * NUM_RANKS; // 52
// Define my own type for a playing card
struct Card {
int rank; // these are called FIELDS
int suit;
}; // note semi-colon
void print_card(Card c)
{
switch(c.rank) {
case ACE: cout << "Ace"; break;
case JACK: cout << "Jack"; break;
case QUEEN: cout << "Queen"; break;
case KING: cout << "King"; break;
default: cout << c.rank; break;
}
cout << " of ";
switch(c.suit) {
case HEARTS: cout << "hearts"; break;
case CLUBS: cout << "clubs"; break;
case SPADES: cout << "spades"; break;
case DIAMONDS: cout << "diamonds"; break;
}
}
void create_deck(Card cards[NUM_CARDS])
{
int i = 0;
for(int s = 0; s < NUM_SUITS; s++)
{
for(int r = 1; r <= NUM_RANKS; r++, i++)
{
cards[i].rank = r;
cards[i].suit = s;
}
}
}
void print_cards(Card deck[NUM_CARDS],
int start, int how_many)
{
for(int i = 0; i < how_many; i++)
{
print_card(deck[start+i]);
cout << "\n";
}
}
void shuffle_deck(Card deck[NUM_CARDS])
{
int i;
for(i = 0; i < 1000; i++) { // # of iterations
// Pick two cards in the deck
int j = rand() % NUM_CARDS;
int k = rand() % NUM_CARDS;
// Swap them
Card c = deck[j];
deck[j] = deck[k];
deck[k] = c;
}
}
void tally_ranks(Card hand[], int count,
int tally[NUM_RANKS+1])
{
// This is for you to do, but here's a
// "stub" that hard-codes a pair of 6s.
tally[6] = 2;
// What you should really do is count
// the ranks in the hand array.
}
void tally_suits(Card hand[], int count,
int tally[NUM_SUITS]);
/* Returns 0 if no pair found, otherwise
returns rank of pair. */
int detect_pair(Card hand[], int count)
{
int tally[NUM_RANKS+1] = { 0 };
tally_ranks(hand, count, tally);
// Look through tally for a pair.
for(int r = 1; r <= NUM_RANKS; r++)
{
if(tally[r] == 2) return r; // found a pair
}
return 0; // no pair
}
int detect_four_of_kind(Card hand[], int count)
{
// This is just a stub.
return 0; // didn't get four of a kind
}
int main()
{
// Initialize the random numbers
srand(time(NULL));
// Declare a variable of type Card:
Card c; // Declares both integers within Card type
c.rank = 3;
c.suit = CLUBS;
//print_card(c); cout << "\n";
Card d = { 4, DIAMONDS };
//print_card(d); cout << "\n";
// Declare my deck
Card deck[NUM_CARDS];
create_deck(deck);
shuffle_deck(deck);
print_cards(deck, 0, 5);
int p = detect_pair(deck, 5);
if(p > 0) {
cout << "Pair of " << p << "s\n";
}
// Hard-code a particular hand for testing
Card hand[5] = {{3, DIAMONDS},
{3, HEARTS},
{3, SPADES},
{3, CLUBS},
{ACE, SPADES}};
// See if we detect four of a kind.
p = detect_four_of_kind(hand, 5);
if(p == 3) {
cout << "SUCCESS: detected four threes.\n";
} else {
cout << "FAIL: four threes produces " << p << "\n";
}
return 0;
}