In this program we are going to simulate a simple card game. The rules are that the player selects a card out of a deck of 52 cards. The computer picks a random card out of the deck. The person with the highest card face number wins.
In this program it would be very helpful to use the examples of card games in the book. This will give you a majority of the code to setup this program. Then you need to add your own classes and code.
You must have 2 classes in your program. One class will be an individual card. The second class will be the game, which must contain an array of cards (your card class). You will need to shuffle the deck of cards each time the player plays the game. Make main() as small as possible and write most of your code in your objects. You must use constructors to setup your cards and your deck of cards. The computer player will select a random card from the deck of cards. You must not allow the player and the computer to choose the same card. Warn the user and get a new selection from them if they select an illegal card from the deck.
You must use classes and objects correctly to get full credit. Using any global variables will result in a 0 for the assignment. If your program does not compile, it will result in a 0 for the assignment.
Sample Output:
Welcome to Highest Card Wins. You will pick a card in the deck
and I will pick a card out of the deck. The player with the
highest card face value will win!
What card out of the deck would you like to choose? (1 to 52) 0
Sorry, that is not a valid card number.
Would you like to play again? (Y/N) y
What card out of the deck would you like to choose? (1 to 52) 5
Your card is: 7 of spades
My card is : 7 of diamonds
It's a tie.
Would you like to play again? (Y/N) y
What card out of the deck would you like to choose? (1 to 52) 50
Your card is: 3 of spades
My card is : 4 of hearts
Sorry you lose.
Would you like to play again? (Y/N) y
What card out of the deck would you like to choose? (1 to 52) 21
Your card is: jack of clubs
My card is : 10 of spades
You win!
Would you like to play again? (Y/N) n
//***********************************************
#include<cstdlib>
#include <process.h>
#include<ctime>
#include <iostream>
using namespace std;
// FACE CARDS ARE DEFINED WITH ENUMERATION.
enum Suit { clubs, diamonds, hearts, spades };
const int jack = 11; //from 2 to 10 are
const int queen = 12; //integers without names
const int king = 13;
const int ace = 14;
// CARDS ARE CREATED AND GIVEN A SUIT,
////////////////////////////////////////////////////////////////
class card
{
private:
int number; //2 to 10, jack, queen, king, ace
Suit suit;
//clubs, diamonds, hearts, spades
public:
card () //constructor (no args)
{ }
//constructor (two args)
void set (int n, Suit s)
{ number = n; suit = s; }
void display(); //display card
// bool isEqual(card); //same as another card?
};
//--------------------------------------------------------------
void card::display() //display the card
{
if( number >= 2 && number <= 10 )
cout << number;
else
switch(number)
{
case jack:
cout << "J";
break;
case queen:
cout << "Q";
break;
case king:
cout << "K";
break;
case ace:
cout << "A";
break;
}
switch(suit)
{
case clubs:
cout << static_cast<char>(5);
break;
case diamonds:
cout << static_cast<char>(4);
break;
case hearts:
cout << static_cast<char>(3);
break;
case spades:
cout << static_cast<char>(6);
break;
}
}
//--------------------------------------------------------------
//ORDERED DECK AND SHUFFLE DECK
/////////////////////////////////////////
class deck
{
private:
int k;
int j;
public:
deck () //constructor (no args)
{ }
void shuffle(); //shuffle and
void game(); //play.
};
//---------------------------------------------------------------------------------------------
void deck::shuffle() // creates an ordered deck and shuffles it.
{
card deck[52];// creates an multi dimensional-array of Objects with the Class "card"
for (j=0; j<52; j++) //ordered deck
{
int num = (j%13)+2;
Suit su = Suit (j /13);
deck[j].set(num, su);
}
srand(time(NULL)+_getpid()); // initialize seed "randomly"
for(j=0; j<52; j++)
{
int k = rand()%52;
card temp = deck[j];
deck[j] = deck[k];
deck[k] = temp;
}
/* for(j=0; j<52; j++)//garbage code..sets the card's faces and repeats until all cards are displayed
{//garbage code..
deck[j].display();
cout<< ", ";//garbage code
if (!((j+1) % 13))//newline every thirteen...... garbage code
cout << endl; //garbage code..
}
*/
}
//---------------------------------------------------------------------------------------------
void deck:game()
{
for(j=0; j<52; j++)//garbage code..sets the card's faces and repeats until all cards are displayed
{//garbage code..
deck[j].display();
cout<< ", ";//garbage code
if (!((j+1) % 13))//newline every thirteen...... garbage code
cout << endl; //garbage code..
}
}
//---------------------------------------------------------------------------------------------
/*class game
{
private:
public:
game () //constructor (no args)
{ }
}; */
//---------------------------------------------------------------------------------------------
int main()
{
deck.shuffle(); //creates and shuffles deck which calls class "card"
system("pause");
return 0;
}
what I am confused about is....
what do I need to put in main(), is it just class::function calls?
the compare card and the player /computers card is going to be assigned by class "card" according to the assignment, and I could only get it to work in class "deck".
Then there is the issue about passing the player's/npc's card object values from the card class to the deck class... and if we could move the deck::shuffle() to card, it would be better... but i couldn't figure out how to get the array values of "card deck[j]" build within the card class, only main and in my new deck class.
no *pointers yet..... we haven't got that far
and never any global variables( instructor says "Auto-F" on assignment)