Hello, my name is Dallas. I'm (obviously) new to the site. I've spent the past couple of days doing absolutely nothing but trying to work on this program. I usually don't like trying to get help, but I've spent so much time on it by myself that I have no other choice but to ask for help. So here we go!
The assignment is to create a Poker game using 3 classes - a Card class, a Deck Class, and a Player Class. So far, all I've been able to manage is to create a card, create a deck of 52 cards, shuffle that deck, and print the deck out. Obviously I have a LONG ways to go, but what I'm working on now is dealing the 5 cards from the deck to the player's hands (in this program, there are 2 hands/players - one is the user and the other is the "computer", although there is no AI implemented). What seems to be happening is that even though the deck has been shuffled, the player's hand consists of the 1st 5 cards from the deck when it was unshuffled.
Card.h
#ifndef CARD_H
#define CARD_H
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Card
{
public:
Card();
int getCard();
void setCard(int);
void displayCard(Card);
protected:
int value;
int suit;
int cardID;
};
#endif
Card.cpp
#include "card.h"
Card::Card() : value(0), suit(0), cardID(0)
{}
int Card::getCard()
{
return cardID;
}
void Card::setCard(int newID)
{
if (newID >= 0 && newID < 52)
{
cardID = newID;
value = cardID%13;
suit = cardID/13;
}
else
{
cardID = 0;
value = 0;
suit = 0;
}
}
void Card::displayCard(Card ID)
{
string Suits[] = {"Diamonds", "Hearts", "Clubs", "Spades"};
string Values[] = { "Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"};
cout << Values[ID.value] << " of " << Suits[ID.suit] << endl;
}
Deck.h
#ifndef DECK_H
#define DECK_H
#include "card.h"
using namespace std;
class Deck : public Card
{
friend ostream& operator <<(ostream&, const Card&);
public:
Deck();
void shuffleDeck();
void displayDeck();
int dealCard();
void swapCards(int,int);
protected:
Card aDeckOfCards[52]; // create a deck 52 cards
int deckTop;
};
#endif
Deck.cpp
#include "deck.h"
Deck::Deck()
{
deckTop=0;
for(int i=deckTop; i < 52; i++)
aDeckOfCards[i].setCard(i); // pass a unique value to setCard
}
void Deck::shuffleDeck()
{
cout << "\nDeck after shuffling: " << endl << endl;
for (int i = 0; i < 100; i++)
{
int index1 = rand()%52;
int index2 = rand()%52;
swapCards(index1, index2);
}
}
void Deck::displayDeck()
{
for (int i = deckTop; i < 52; i++)
{
Card j = aDeckOfCards[i];
displayCard(j);
}
}
int Deck::dealCard()
{
if (deckTop < 51)
{
deckTop++;
}
else
{
shuffleDeck();
deckTop = 1;
dealCard();
}
return aDeckOfCards[deckTop-1].getCard();
}
void Deck::swapCards(int first, int second)
{
Card temp = aDeckOfCards[first];
aDeckOfCards[first] = aDeckOfCards[second];
aDeckOfCards[second] = temp;
}
ostream& operator <<(ostream& out, const Card& c)
{
out << c;
return out;
}
Player.h
#ifndef PLAYER_H
#define PLAYER_H
#include "card.h"
#include "deck.h"
class Player: public Deck
{
public:
Player();
void showHand();
void addCard(int);
void showCard(int);
void deleteCard();
private:
Card aHand[5];
int currentCard;
};
#endif
Player.cpp
#include "player.h"
const int HAND_SIZE = 5;
Player::Player()
{
for (int i = 0; i < HAND_SIZE; i++)
{
addCard(i);
deckTop++;
}
}
void Player::addCard(int cardNum)
{
aHand[cardNum] = aDeckOfCards[cardNum];
}
void Player::deleteCard()
{
}
void Player::showCard(int id)
{
Card j = aHand[id];
displayCard(j);
}
void Player::showHand()
{
for (int i = 0; i < HAND_SIZE; i++)
showCard(i);
}
And main.cpp
#include "card.h"
#include "deck.h"
#include "player.h"
int main()
{
Deck d;
d.shuffleDeck();
d.displayDeck();
cout << "\n\nPlayer1's hand is: " << endl;
Player player1;
player1.showHand();
system("PAUSE");
return 0;
}
Again, this is obviously not even closed to being finished, but I'm just trying to do this one step at a time and have each player draw 5 cards from the deck. The data members and functions shown are required, so I have to have them. And I've seen quite a few codes use vectors or something, but we haven't learned about those, so I don't think I can use them for this assignment.