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
|
///////////////////////////////////////////////////////////////////////
// "cardgame.cpp"
// Author: Matthew Franz
// Date: 11/14/2013
// PURPOSE: Use of 2-D array to create a card game in which two players
// draw 5 cards each from a standard deck of cards (52 cards; 4 suits,
// 13 faces).
// Compile instructions:
///////////////////////////////////////////////////////////////////////
#include <iostream>
#include <cstdlib>
#include <ctime> // library used to re-seed the rand() function
#include <iomanip>
using namespace std;
// FUNCTION PROTOTYPES:
// function protoype that prints the contents of the array
void Print2DArray(int a[][13], int rows, int cols);
// function prototype that initializes all the elements of
// the array to zero.
void Init2DArray(int a[][13], int rows, int cols);
///////////////////////////////////////////////////////////////////////
int main()
{
int suitvalue; // integer suit value from rand() call (0-3: 4 suits)
int facevalue; // integer face value from rand() call (0-12: 13 cards)
int numcards = 0; // keep track of how many cards dealt
// array for the suits in the deck of cards:
char suits[4][10] = {"Spades", "Diamonds", "Clubs",
"Hearts"};
// array for the values in the deck of cards:
char faces[13][10] = {"ACE","K","Q","J","10","9","8","7","6","5","4","3","2"};
int card_deck[4][13]; // This will be the master deck
// NOTE: Could store user's cards in an array of 5x2 elements: 5 cards
// and [i][0] would store face value of i'th card, and [i][1] suitvalue
// BUT, by storing in same way that are keeping track of the cards in the
// deck that have been dealt, might be easier to 'rank' the cards?
// initialize all elements to zero:
Init2DArray(card_deck, 4, 13);
// print out the deck array:
cout << "\nHere is the array: \n";
Print2DArray(card_deck, 4, 13);
// randomly "deal" a hand of cards:
srand(time(0));
while (numcards < 5 ) {
suitvalue = rand() % 4;
facevalue = rand() % 13;
cout << "Your card is a " << faces[facevalue];
cout << " of " << suits[suitvalue] << endl;
if(card_deck[suitvalue][facevalue]==1){
continue;
}
card_deck[suitvalue][facevalue]=1;
// increment number of cards
numcards++;
}
// print out the deck array:
cout << "\nHere is the array: \n";
Print2DArray(card_deck, 4, 13);
// Now the computer will be dealt a hand:
numcards = 0;
while (numcards < 5 ) {
suitvalue = rand() % 4;
facevalue = rand() % 13;
cout << "Computer's card is a " << faces[facevalue];
cout << " of " << suits[suitvalue] << endl;
if(card_deck[suitvalue][facevalue]==1){
continue;
}
card_deck[suitvalue][facevalue]=1;
// increment number of cards
// increment number of cards
numcards++;
}
// print out the array:
cout << "\nHere is the array that shows the cards used in the deck: \n";
Print2DArray(card_deck, 4, 13);
system("PAUSE");
return EXIT_SUCCESS;
} // end main
///////////////////////////////////////////////////////////////////////
// FUNCTION: Print2DArray
// PURPOSE: Print out a 2D array with 13 elements per row
// INPUT PARAMETERS (include: 1. Type, 2.Ref or Val, 3.Description):
// RETURNS:
// MODIFIES(for globals or parameters passed by reference):
///////////////////////////////////////////////////////////////////////
void Print2DArray(int a[][13], int rows, int cols)
{
for(int i=0; i < rows; i++){
for(int k=0; k < cols; k++){
cout << setw(4) << a[i][k] << " ";
}
cout << endl;
}
return;
} // end Print2DArray
///////////////////////////////////////////////////////////////////////
// FUNCTION: Init2DArray
// PURPOSE: Fill a 2D array with 13 elements per row with zeros
// INPUT PARAMETERS (include: 1. Type, 2.Ref or Val, 3.Description):
// RETURNS:
// MODIFIES(for globals or parameters passed by reference):
///////////////////////////////////////////////////////////////////////
void Init2DArray(int a[][13], int rows, int cols)
{
for(int i=0; i < rows; i++){
for(int k=0; k < cols; k++){
a[i][k]=0;
}
}
return;
} // end Init2DArray
|