This is for an assignment before you tell me not to use an array. Unfortunately I have to.
I've troubleshot this for a good amount of time and I've finally given up and come to ask the smart people here. I know this is something to do with how I'm allocating the array, but I can't figure what's wrong. It goes through the inner loop once and then freeze on the second run. Sorry if I left anything important out. I will add any needed info.
Variables from Card
private:
string *cardRank;
string *suit;
int rankNum;
int value;
Problem Function
void initArray(Card **cPtr)
{
int i;
int j;
int index=0;
cPtr = new Card*[DECK]; //deck is const int 52
for(i=0; i < 4; ++i)
{
for(j=1; j < 14; ++j)
{
cPtr[index] = new Card(j, j, i); //freezes here. does not make it to the first
//function in the constructor
cout << cPtr[index] << endl;
++index;
}
}
}
Constructor
Card::Card(int cRank, int cValue, int suitNum)
{
setRankNum(cRank);
cout << "rank num set\n";
setValue(cValue);
cout << "val set\n";
setSuit(suitNum);
cout << "suit set\n";
setRank(cRank);
cout << "rank set\n";
}
Overloaded <<
ostream &operator << (ostream &strm, Card &aCard)
{
strm << aCard.getRank() << " of " << aCard.getSuit();
return strm;
}
whole program
#include <string>
#include "Card.h"
using namespace std;
const int DECK = 52;
void initArray(Card **&cPtr);
void shufflePArray(Card **pArray);
void determineHand(Card **pArray);
bool isFlush(Card *hand);
bool isStraight(Card *hand);
bool isFour(Card *hand);
bool isThree(Card *hand);
bool isTwo(Card *hand);
void drawHand(Card *hand, Card **pArray);
void displayHand(Card **hand);
int main()
{
Card **cArray;
initArray(cArray);
cout << "done.";
shufflePArray(cArray);
determineHand(cArray);
delete [] cArray;
return 0;
}
void initArray(Card **&cPtr)
{
int i;
int j;
int index=0;
cPtr = new Card*[DECK];
for(i=0; i < 4; ++i)
{
for(j=1; j < 13; ++j)
{ cout << "inner loop " << index << endl;
cPtr[index] = new Card(j, j, i);
cout << cPtr[index] << endl;
++index;
}
}
}
void shufflePArray(Card **pArray)
{
//code here
}
void determineHand(Card ***pArray)
{
Card hand[5];
drawHand(hand, pArray);
displayHand(pArray);
if (isFlush(hand) == true)
{
if(isStraight(hand) == true)
cout << "Straight flush!!!" << endl;
else
cout << "You got a flush!" << endl;
}
else if(isStraight(hand) == true)
{
cout << "You got a straight!" << endl;
}
else if(isFour(hand) == true)
{
cout << "Four of a kind!!!" << endl;
}
else if(isThree(hand)==true)
{
cout << "Three of a kind!" << endl;
}
else if(isTwo(hand) == true)
{
cout << "That's a pair";
}
}
bool isFlush(Card *hand)
{
int i;
int match;
for (i=0; i<5;++i)
{
if (hand[0].getSuit() == hand[i].getSuit())
match++;
}
if (match == 5)
return true;
else
return false;
}
bool isStraight(Card *hand)
{
int match;
for (int i=0; i<5;++i)
{
if (hand[0].getValue() == hand[i].getValue() - 1)
match++;
}
if (match == 5)
return true;
else
return false;
}
bool isFour(Card *hand)
{
int match = 0;
for(int i=0; i<5; ++i)
{
match=0;
for(int j=0;j<5;++j)
{
if (hand[i] == hand[j])
match++;
if (match == 4)
return true;
else
return false;
}
}
}
bool isThree(Card *hand)
{
int match = 0;
for(int i=0; i<5; ++i)
{
match=0;
for(int j=0;j<5;++j)
{
if (hand[i] == hand[j])
match++;
if (match == 3)
return true;
else
return false;
}
}
}
bool isTwo(Card *hand)
{
int match = 0;
for(int i=0; i<5; ++i)
{
match=0;
for(int j=0;j<5;++j)
{
if (hand[i] == hand[j])
match++;
if (match == 2)
return true;
else
return false;
}
}
}
void drawHand(Card *hand, Card **pArray)
{
for(int i=0; i<5;++i)
cout << hand[i];
}
}
{
hand[i] = *pArray[i];
}
}
void displayHand(Card **hand)
{
Card temp;
for (int i = 0; i < 5; ++i)
{
contents of card.h
#ifndef CARD_H
#define CARD_H
#include <string>
#include <iostream>
using namespace std;
class Card
{
private:
string *cardRank;
string *suit;
int rankNum;
int value;
public:
Card();
Card(int cRank, int cValue, int suitNum);
friend ostream &operator << (ostream &strm, Card &aCard);
bool operator > (const Card &aCard);
bool operator < (const Card &aCard);
bool operator == (const Card &aCard);
void setRank(int r);
void setSuit(int s);
void setValue(int v);
void setRankNum(int n);
string getRank();
string getSuit();
int getRankNum();
int getValue();
};
#endif // CARD_H
contents of Card.cpp
#include "Card.h"
#include <string>
#include <iostream>
Card::Card()
{
cardRank = NULL;
suit = NULL;
rankNum = 0;
value = 0;
}
Card::Card(int cRank, int cValue, int suitNum)
{
setRankNum(cRank);
cout << "rank num set\n";
setValue(cValue);
cout << "val set\n";
setSuit(suitNum);
cout << "suit set\n";
setRank(cRank);
cout << "rank set\n";
}
ostream &operator << (ostream &strm, Card &aCard)
{
strm << aCard.getRank() << " of " << aCard.getSuit();
return strm;
}
bool Card::operator > (const Card &aCard)
{
if (aCard.value > value)
return true;
else
return false;
}
bool Card::operator < (const Card &aCard)
{
if (aCard.value < value)
return true;
else
return false;
}
bool Card::operator == (const Card &aCard)
{
if (value == aCard.value)
return true;
else
return false;
}
void Card::setRank(int r)
{
switch(r)
{
case 13:
*cardRank = "Ace";
case 1:
*cardRank = "Two";
case 2:
*cardRank = "Three";
case 3:
*cardRank = "Four";
case 4:
*cardRank = "Five";
case 5:
*cardRank = "Six";
case 6:
*cardRank = "Seven";
case 7:
*cardRank = "Eight";
case 8:
*cardRank = "Nine";
case 9:
*cardRank = "Ten";
case 10:
*cardRank = "Jack";
case 11:
*cardRank = "Queen";
case 12:
*cardRank = "King";
}
}
void Card::setSuit(int s)
{
if(s==0){
*suit = "Hearts";
cout << "suit set";}
else if(s==1)
*suit = "Diamonds";
else if (s==2)
*suit = "Clubs";
else if (s==3)
*suit = "Spades";
else
cout << "Invalid suit num.";
}
void Card::setValue(int v)
{
if (v > 0)
value = v;
}
void Card::setRankNum(int n)
{
rankNum = n;
}
string Card::getRank()
{
return *cardRank;
}
string Card::getSuit()
{
return *suit;
}
int Card::getRankNum()
{
return rankNum;
}
int Card::getValue()
{
return value;
}
One possible reason for your troubles is that you are dereferencing a NULL pointer:
void Card::setRank(int r)
{
switch(r)
{
case 13:
*cardRank = "Ace";
case 1:
*cardRank = "Two";
//...
The cardRank is NULL. You now try to dereference a NULL pointer. Unless I missed something, where is the call to "cardRank = new std::string;"?
But that brings up a bigger point -- in your comment you stated that your professor wants you to use pointers. But honestly, there is no reason whatsoever to use pointers for those string members -- absolutely none.
You do need pointers to implement your dynamic array, but that's it. Either you are reading the professor's intentions incorrectly, or the professor needs to get another profession.
You're passing a pointer to a pointer as value in : initArray(Card **cPtr), apparently your intention is to change it (i.e return the allocated Card array.
I cannot really say what is happening exactly but some memory gets overwritten, the return address in the stack probably
Anyway either change it to reference i.e. initArray(Card **&cPtr) or use initArray(Card ***cPtr) and change the code accordingly.
The second problem in the use of strings, change string *cardRank to string cardRank and suit as well, you don't need pointers to strings (it's not like char*, those are objects any way, the assignments should be changed from *cardRank = "Ace" to cardRank = "Ace"
Related
I'm not too sure if questions of this sort is allowed on stackoverflow but here it goes. I finished up this assignment to calculate reverse polish expressions with a small set of operators. All the tests passed and I have already submitted the assignment. I was wondering if you guys had any suggestions where I could improve on the code that could be applied to my future assignments/projects.
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
class TheStack
{
public:
void push(double d1);
double pop();
int getNodeTracker();
private:
struct Node
{
double data;
Node* next;
};
Node* bottom = NULL;
Node* top = NULL;
Node* newP = NULL;
int nodeTracker = 0;
};
void TheStack::push(double d1)
{
newP = new Node;
newP->data = d1;
newP->next = NULL;
if (bottom == NULL)
{
bottom = top = newP;
top->next = NULL;
}
else
{
newP->next = top;
top = newP;
}
nodeTracker += 1;
}
double TheStack::pop()
{
Node* tempP;
double tempD;
if (top == NULL)
{
cout << "The stack is empty." << endl;
}
else
{
tempP = top;
tempD = tempP->data;
top = top->next;
delete(tempP);
nodeTracker -= 1;
return(tempD);
}
}
int TheStack::getNodeTracker()
{
return nodeTracker;
}
bool isOperand(string s1);
bool isOperator(string s1);
double operate(string s1, double d1, double d2);
int processor(string str);
bool validString(string str);
int main()
{
// getting user input
string str;
getline(cin, str);
// input loop
while (str != "0")
{
processor(str);
cout << endl;
getline(cin, str);
}
// end program
return 0;
}
bool isOperand(string s1)
{
try
{
stod(s1);
}
catch (...)
{
return false;
}
return true;
}
bool isOperator(string s1)
{
string validOps[4] = { "+", "-", "*", "/" };
for (int i = 0; i < 4; i++)
{
if (s1 == validOps[i])
{
return true;
}
}
return false;
}
double operate(string s1, double d1, double d2)
{
char op = s1[0];
switch (op)
{
case '+':
return d1 + d2;
case '-':
return d1 - d2;
case '*':
return d1 * d2;
case '/':
return d1 / d2;
}
}
bool validString(string str)
{
for (int i = str.size()-1; i >= 0; i--)
{
if (str[i] == '=')
{
return true;
}
}
return false;
}
int processor(string str)
{
TheStack stacklist;
istringstream iss(str);
string streamVar;
iss >> streamVar;
// checks if expression has = sign
if (!validString(str))
{
cout << "Error: Please input valid expression." << endl;
return -1;
}
// builds and operates upon stack
while (streamVar != "=")
{
if (isOperand(streamVar))
{
stacklist.push(stod(streamVar));
}
else if (isOperator(streamVar))
{
if (stacklist.getNodeTracker() > 1)
{
double d2 = stacklist.pop();
double d1 = stacklist.pop();
if (streamVar == "/" && d2 == 0)
{
cout << "Error: Division by 0." << endl;
return -1;
}
double result = operate(streamVar, d1, d2);
stacklist.push(result);
}
else
{
cout << "Error: Too many operators." << endl;
return -1;
}
}
iss >> streamVar;
}
// operand error case
if (stacklist.getNodeTracker() > 1)
{
cout << "Error: Too many operands." << endl;
return -1;
}
// output
cout << stacklist.pop() << endl;
return 0;
}
I am writing a priority queue with a max heap structure as an assignment for school. I can either write it as an array or I can use a vector. I chose a vector. So the assignment is this, the user chooses options from a menu where he either wants to add,print, or view the elements. When the user chooses to add he gets ask who wants to be added, the instructor, student, or TA. He can enter i,I,t,T,S,s. The instructor having the highest priority where if the user chooses the option to print and there is an instructor in the queue he gets to go first. The TA having the second highest priority where if there is a TA and a student in the queue, the TA goes first. If there is is more than one instructor than the queue acts as a normal queue. I have written most of it, or tried. I got my max heap implementation from my textbook since they provide one. Now the problem is this, when I have more than one item in the priority queue and I choose to print, it crashes and gives me a vector subscript out of range exception. I been trying to fix it and no luck. Also, when I try to print the elements in the queue or print them, it needs to say the job# with the name of the person. Can someone help me find a way to implement that.
#pragma once
#include <vector>
struct Heap
{
std::vector<int> m_elements;
void ReHeapDown(int, int);
void ReHeapUp(int, int);
void Swap(int& a, int& b);
};
#include "heap.h"
void Heap::ReHeapDown(int index, int bottom)
{
int maxChild, rightChild, leftChild;
leftChild = index * 2 + 1;
rightChild = index * 2 + 2;
if (leftChild <= bottom)
{
if (leftChild == bottom)
maxChild = leftChild;
else
{
if (m_elements[leftChild] <= m_elements[rightChild])
maxChild = rightChild;
else
maxChild = leftChild;
}
if (m_elements[index] < m_elements[maxChild])
{
Swap(m_elements[index], m_elements[maxChild]);
ReHeapDown(maxChild, bottom);
}
}
}
void Heap::ReHeapUp(int index, int bottom)
{
int parent;
if (bottom > index)
{
parent = (bottom - 1) / 2;
if (m_elements[parent] < m_elements[bottom])
{
Swap(m_elements[parent], m_elements[bottom]);
ReHeapUp(index, parent);
}
}
}
void Heap::Swap(int& a, int& b)
{
int temp;
temp = a;
a = b;
b = temp;
}
#include <iostream>
#include "heap.h"
#pragma once
class PQTYPE
{
private:
Heap m_Items;
public:
bool isEmpty() const;
void Enqueue(int, std::string);
void Dequeue(int, std::string);
void printElements();
};
#include "pqtype.h"
bool PQTYPE::isEmpty() const
{
return m_Items.m_elements.empty();
}
void PQTYPE::Enqueue(int newItem, std::string lName)
{
if (lName == "Student")
{
m_Items.m_elements.push_back(newItem);
m_Items.ReHeapUp(0, m_Items.m_elements.size() - 1);
}
else if (lName == "TA")
{
m_Items.m_elements.push_back(newItem);
m_Items.ReHeapUp(0, m_Items.m_elements.size() - 1);
}
else if (lName == "Instructor")
{
m_Items.m_elements.push_back(newItem);
m_Items.ReHeapUp(0, m_Items.m_elements.size() - 1);
}
}
void PQTYPE::Dequeue(int item, std::string lName)
{
if (isEmpty())
std::cout << "No jobs to print\n";
else
{
m_Items.m_elements[0] = m_Items.m_elements.back();
std::cout << "Now printing Job#" << m_Items.m_elements[item - 1] << " " << lName.c_str() << std::endl;
m_Items.m_elements.pop_back();
m_Items.ReHeapDown(0, item - 1);
}
}
void PQTYPE::printElements()
{
if (isEmpty())
std::cout << "No jobs to print\n";
else
{
for (int i = 0; i < m_Items.m_elements.size(); i++)
{
std::cout << "Job#" << m_Items.m_elements[i] << std::endl;
}
}
}
#include"pqtype.h"
struct Person
{
int m_priority;
std::string m_name;
Person()
{
m_priority = 0;
m_name = " ";
}
};
int showMenu();
void addJobs(PQTYPE&, Person&);
void printJobs(PQTYPE&, Person&);
void viewJobs(PQTYPE&);
int main()
{
int option;
Person p;
PQTYPE pq;
do
{
option = showMenu();
switch (option)
{
case 1: addJobs(pq, p);
break;
case 2: printJobs(pq, p);
break;
case 3: viewJobs(pq);
break;
case 4:
break;
default: std::cout << "Wrong input\n";
break;
}
} while (option != 4);
return 0;
}
int showMenu()
{
int choice;
std::cout << " 1.)Add Job\n";
std::cout << " 2.)Print Job\n";
std::cout << " 3.)View Jobs\n";
std::cout << " 4.)Exit\n";
std::cout << " Enter Choice: ";
std::cin >> choice;
return choice;
}
void addJobs(PQTYPE& pq, Person& per)
{
char jobChoice;
std::cout << "Who is the job for ( Instructor(i or I), TA(t or T), Student(s or S) :";
std::cin >> jobChoice;
if (jobChoice == 'S' || jobChoice == 's')
{
per.m_priority++;
per.m_name = "Student";
pq.Enqueue(per.m_priority, per.m_name);
}
else if (jobChoice == 'T' || jobChoice == 't')
{
per.m_priority++;
per.m_name = "TA";
pq.Enqueue(per.m_priority, per.m_name);
}
if (jobChoice == 'I' || jobChoice == 'i')
{
per.m_priority++;
per.m_name = "Instructor";
pq.Enqueue(per.m_priority, per.m_name);
}
}
void printJobs(PQTYPE& pq, Person& p)
{
pq.Dequeue(p.m_priority, p.m_name);
}
void viewJobs(PQTYPE& pq)
{
pq.printElements();
}
In your original code the index used inside Dequeue() for accessing the vector doesn't seem to be initialised in the right way. Let's assume that you have added two entries to your list. In this case the value of P.m_priority inside your main() is 2. Now you're calling printJobs() for the first time. printJobs() calls pq.Dequeue(p.m_priority, p.m_name), so Dequeue() gets p.m_priority as its parameter item. Keep in mind that item has the value 2.
m_Items.m_elements[0] = m_Items.m_elements.back();
std::cout << "Now printing Job#" << m_Items.m_elements[item - 1] << " " << lName.c_str() << std::endl;
m_Items.m_elements.pop_back();
You're accessing your std::vector using an index of item - 1. This works for the first time, as there are two elements in your list. In this call, there is also a pop_back() done on your list, which decreases its size by one. The next time you call printJobs(), the given parameter item won't have changed, it still has the value 2. When you access your Itemlist, there is no longer an index of 1, and an subscript out of range exception will be thrown.
There were no fixed priorities assigned to the three entry types in your original version, so I added these (see addJobs() ).
So a possible solution to store the person's name could look like this:
struct Person
{
int m_priority;
std::string m_name;
Person()
{
m_priority = 0;
m_name = " ";
}
};
struct Heap
{
std::vector<Person> m_elements;
void ReHeapDown(int, int);
void ReHeapUp(int, int);
void Swap(Person& a, Person& b);
};
void Heap::ReHeapDown(int index, int bottom)
{
int maxChild, rightChild, leftChild;
leftChild = index * 2 + 1;
rightChild = index * 2 + 2;
if (leftChild <= bottom)
{
if (leftChild == bottom)
maxChild = leftChild;
else
{
if (m_elements[leftChild].m_priority <= m_elements[rightChild].m_priority)
maxChild = rightChild;
else
maxChild = leftChild;
}
if (m_elements[index].m_priority < m_elements[maxChild].m_priority)
{
Swap(m_elements[index], m_elements[maxChild]);
ReHeapDown(maxChild, bottom);
}
}
}
void Heap::ReHeapUp(int index, int bottom)
{
int parent;
if (bottom > index)
{
parent = (bottom - 1) / 2;
if (m_elements[parent].m_priority < m_elements[bottom].m_priority)
{
Swap(m_elements[parent], m_elements[bottom]);
ReHeapUp(index, parent);
}
}
}
void Heap::Swap(Person& a, Person& b)
{
Person temp;
temp = a;
a = b;
b = temp;
}
#include <iostream>
class PQTYPE
{
private:
Heap m_Items;
public:
bool isEmpty() const;
void Enqueue(Person);
void Dequeue();
void printElements();
};
bool PQTYPE::isEmpty() const
{
return m_Items.m_elements.empty();
}
void PQTYPE::Enqueue(Person newItem)
{
if (!newItem.m_name.compare("Student"))
{
m_Items.m_elements.push_back(newItem);
m_Items.ReHeapUp(0, m_Items.m_elements.size() - 1);
}
else if (!newItem.m_name.compare("TA"))
{
m_Items.m_elements.push_back(newItem);
m_Items.ReHeapUp(0, m_Items.m_elements.size() - 1);
}
else if (!newItem.m_name.compare("Instructor"))
{
m_Items.m_elements.push_back(newItem);
m_Items.ReHeapUp(0, m_Items.m_elements.size() - 1);
}
}
void PQTYPE::Dequeue()
{
if (isEmpty())
std::cout << "No jobs to print\n";
else
{
Person front = m_Items.m_elements.front();
std::cout << "Now printing Job#" << front.m_priority << " " << front.m_name.c_str() << std::endl;
m_Items.m_elements.erase(m_Items.m_elements.begin());
m_Items.ReHeapDown(0, m_Items.m_elements.size() - 1);
}
}
void PQTYPE::printElements()
{
if (isEmpty())
std::cout << "No jobs to print\n";
else
{
for (int i = 0; i < m_Items.m_elements.size(); i++)
{
std::cout << "Job#" << m_Items.m_elements[i].m_priority << " " << m_Items.m_elements[i].m_name.c_str() << std::endl;
}
}
}
int showMenu();
void addJobs(PQTYPE&, Person&);
void printJobs(PQTYPE&, Person&);
void viewJobs(PQTYPE&);
int showMenu()
{
int choice;
std::cout << " 1.)Add Job\n";
std::cout << " 2.)Print Job\n";
std::cout << " 3.)View Jobs\n";
std::cout << " 4.)Exit\n";
std::cout << " Enter Choice: ";
std::cin >> choice;
return choice;
}
void addJobs(PQTYPE& pq, Person& per)
{
char jobChoice;
std::cout << "Who is the job for ( Instructor(i or I), TA(t or T), Student(s or S) :";
std::cin >> jobChoice;
if (jobChoice == 'S' || jobChoice == 's')
{
per.m_priority = 0;
per.m_name = "Student";
pq.Enqueue(per);
}
else if (jobChoice == 'T' || jobChoice == 't')
{
per.m_priority = 1;
per.m_name = "TA";
pq.Enqueue(per);
}
if (jobChoice == 'I' || jobChoice == 'i')
{
per.m_priority = 2;
per.m_name = "Instructor";
pq.Enqueue(per);
}
}
void printJobs(PQTYPE& pq)
{
pq.Dequeue();
}
void viewJobs(PQTYPE& pq)
{
pq.printElements();
}
int main()
int option;
Person p;
PQTYPE pq;
do
{
option = showMenu();
switch (option)
{
case 1: addJobs(pq, p);
break;
case 2: printJobs(pq);
break;
case 3: viewJobs(pq);
break;
case 4:
break;
default: std::cout << "Wrong input\n";
break;
}
} while (option != 4);
return 0
}
Are you sure that the methods ReHeapUp and ReHeapDown meet your requirements? And shouldn't there be a distinction between job number and priority?
I am a beginner with C++ and have tried a few different things, but no matter what I try it doesn’t seem to recognize when a card has already been drawn...
I have tried to utilize to bool isDrawn, but after a few attempts of that with no success I am not 100% sure where to go from here
class Card {
public:
string suitName;
int cardNumber;
void printCard() {
if (cardNumber == 1) {
cout << "Ace";
} else if (cardNumber == 11) {
cout << "Jack";
} else if (cardNumber == 12) {
cout << "Queen";
} else if (cardNumber == 13) {
cout << "King";
} else
cout << cardNumber;
cout << " of " << suitName << endl;
}
bool isDrawn = false;
};
class Deck {
public:
Card deck[52];
void makeDeck(){
int counter = 0;
string suits[] = { "Spades", "Hearts", "Clubs", "Diamonds" };
string face[] = { "Ace", "Jack", "Queen", "King" };
for (int i = 0; i <= 3; i++) {
for (int j = 0; j < 13; j++) {
deck[counter].isDrawn = false;
deck[counter].cardNumber = (j + 1);
deck[counter].suitName = suits[i];
counter++;
}
}
}
Card drawCard() {
int randcard;
do {
randcard = rand() % 52;
} while (deck[randcard].isDrawn == true);
return deck[randcard];
}
};
class Player {
public:
vector<Card> hand;
void setName(string s){
name = s;
}
string printName(){
return name;
}
void printHand() {
for (int i = 0; i < hand.size(); i++){
hand.at(i).printCard();}
}
private:
string name;
};
int main() {
Deck my_deck;
Player p1;
p1.setName("HAL 9000");
cout << p1.printName() << endl;
my_deck.makeDeck();
p1.hand.push_back(my_deck.drawCard());
p1.hand.push_back(my_deck.drawCard());
p1.hand.push_back(my_deck.drawCard());
p1.printHand();
cout << endl;
}
I am making a card game, Crazy Eights, I have everything done except I am having a problem with this AI. The AI needs to delete the card it just played from its card options. (Each player is dealt 5 cards, and if the card matches the suit, value, or an 8, it can play). SO far, the computer does play the correct card, it just doesn't remove the card so it can just keep playing and playing.
Main Function
#include <iostream>
#include <time.h>
#include "CCrazyEight.h"
#include "stdafx.h"
using namespace std;
void main()
{
// Calls Crazy eight and declares a variable, then runs the game
CCrazyEight E1;
E1.Menu();
}
CCard Header File
#pragma once
#include <iostream>
#include <time.h>
#include "stdafx.h"
using namespace std;
enum CardSuit { Hearts, Diamonds, Spades, Clubs };
class CCard
{
friend ostream & operator << (ostream &left, CCard right);
private:
int value;
CardSuit suit;
public:
// Constructor, sets default values to suit and value
CCard()
{
value = 1;
suit = Hearts;
}
// return thes value of the card
int GetValue()
{
return value;
}
// returns the suit of the card
CardSuit GetSuit()
{
return suit;
}
// Passes values to CDeck constructor, which then randomizes values for the cards
void InitCard(CardSuit s, int v)
{
suit = s;
if (v > 0 && v < 14)
{
value = v;
}
}
};
// outputs value and suit of the card
ostream & operator << (ostream &left, CCard right)
{
if (right.value < 11 && right.value > 1)
{
left << right.value;
}
else if (right.value == 1)
{
left << "Ace";
}
else if (right.value == 11)
{
left << "Jack";
}
else if (right.value == 12)
{
left << "Queen";
}
else if (right.value == 13)
{
left << "King";
}
left << " of ";
if (right.suit == Hearts)
{
left << "Hearts";
}
else if (right.suit == Diamonds)
{
left << "Diamonds";
}
else if (right.suit == Spades)
{
left << "Spades";
}
else if (right.suit == Clubs)
{
left << "Clubs";
}
return left;
}
CDeck Header File
#pragma once
#include <iostream>
#include "CCard.h"
#include <time.h>
using namespace std;
class CDeck
{
private:
CCard Deck[52];
int TopCard;
public:
// constructor
// randomizes the card numbers for suit and value
CDeck()
{
srand(time(NULL));
TopCard = 0;
for (int suit = 0; suit < 4; suit++)
{
for (int value = 1; value <= 13; value++)
{
Deck[((suit * 13) + value) - 1].InitCard((CardSuit)suit, value);
}
}
}
// shuffles the deck, completely randomizes the cards
void Shuffle()
{
TopCard = 0;
for (int y = 0; y < 10; y++)
{
for (int x = 0; x < 52; x++)
{
int SwapPosition = rand() % 52;
CCard temp;
temp = Deck[x];
Deck[x] = Deck[SwapPosition];
Deck[SwapPosition] = temp;
}
}
}
// Draw a card function that gives out a card when called for in crazy eight
CCard DrawCard()
{
if (TopCard > 51)
{
cout << "Deck is Empty." << endl;
}
else
{
return Deck[TopCard++];
}
}
};
CrazyEight header file
#pragma once
#include <iostream>
#include <time.h>
#include "CCard.h"
#include "CDeck.h"
#include "CPlayer.h"
using namespace std;
// Having an issue trying to get the computer to play something different
class CCrazyEight
{
private:
CDeck d1;
CCard c1;
int index;
CCard Topcard;
CPlayer Computer;
CPlayer Human;
public:
// constructor
// shuffles the deck to the played, and assigns the topcard to start the game
CCrazyEight()
{
d1.Shuffle();
c1 = d1.DrawCard();
Topcard = c1;
}
// plays the game, dealing cards to computer and human
void PlayGame()
{
cout << "Welcome to Crazy Eight!" << endl;
cout << "You get to go first!" << endl;
cout << endl;
for (int k = 0; k < 5; k++)
{
Computer.GetCard(d1.DrawCard());
Human.GetCard(d1.DrawCard());
}
}
// displays the topmost card
void TopCard()
{
cout << Topcard << endl;
}
// displays the cards for the human, and the options they can chose, and anything else they need about the game
void Menu()
{
PlayGame();
while (Human.NumCards() != 0 || Computer.NumCards() != 0)
{
cout << endl;
cout << "Top Card is: "; TopCard();
cout << endl;
cout << "You Have " << Human.NumCards() << " Cards Left." << endl;
cout << "Your play: " << endl;
cout << "0) Draw Card" << endl;
cout << Human << endl;
cin >> index;
if (index == 0)
{
Human.GetCard(d1.DrawCard());
Topcard = Topcard;
}
else if (index > 0 && index <= Human.NumCards())
{
Topcard = Human.CardPlayed(index, Topcard);
}
cout << endl;
Topcard = Computer.ComputerAI(Topcard);
cout << "Computer Played: " << Topcard << endl;
if (Computer.NumCards() > 0)
{
cout << "Computer has " << Computer.NumCards() << " Cards Left." << endl;
}
else
cout << "You Lose!" << endl;
}
}
};
CPlayer Header File
#pragma once
#include "CCard.h"
#include <time.h>
#include <iostream>
using namespace std;
class CPlayer
{
friend ostream & operator << (ostream &left, CPlayer right);
private:
CCard Hand[25];
CDeck d1;
int NumberOfCard;
bool Computer;
const int MaxHand = 26;
bool TopCard;
public:
// Constructor for the computer, sets computer to true
CPlayer(bool computer = true)
{
Computer = computer;
NumberOfCard = 0;
}
// Computers AI, checks and sees if they can play, if not then they draw until they can play, or they play a card that is playable******Easy mode**** Lol
CCard ComputerAI(CCard Topcard)
{
for (int i = 0; i < NumberOfCard; i++)
{
if (Hand[i].GetSuit() == Topcard.GetSuit() || Hand[i].GetValue() == 8 || Hand[i].GetValue() == Topcard.GetValue())
{
for (int i = 0; i < NumberOfCard; i++)
{
if (Topcard.GetSuit() == Hand[i].GetSuit() && Hand[i].GetValue() != 8)
{
NumberOfCard--;
return Hand[i];
}
}
for (int i = 0; i < NumberOfCard; i++)
{
if (Topcard.GetValue() == Hand[i].GetValue() && Hand[i].GetValue() != 8)
{
NumberOfCard--;
return Hand[i];
}
}
for (int i = 0; i < NumberOfCard; i++)
{
if (Hand[i].GetValue() == 8)
{
NumberOfCard--;
return Hand[i];
}
}
}
else
{
GetCard(d1.DrawCard());
}
}
}
// Is not the computer, sets computer to fasle. i.e. its the human
void Human()
{
Computer = false;
}
int NumCards()
{
return NumberOfCard;
}
// will hand out a card
bool GetCard(CCard NewCard)
{
if (NumberOfCard < MaxHand)
{
// Now is taking the card
Hand[NumberOfCard] = NewCard;
NumberOfCard++;
return true;
}
else
return false;
}
// subtracts the card that was played from the deck of the human user
CCard CardPlayed(int index, CCard Topcard)
{
if (CheckCard(index, Topcard) == true)
{
CCard TempTopcard = Hand[index - 1];
for (int i = index - 1; i <= NumberOfCard - 1; i++)
{
Hand[i] = Hand[i + 1];
}
NumberOfCard--;
return TempTopcard;
}
else
cout << "Not a valid Card." << endl;
return Topcard;
}
// checks the card and see's if it is playable or not
bool CheckCard(int index, CCard Topcard)
{
if (Hand[index - 1].GetSuit() == Topcard.GetSuit() || Hand[index - 1].GetValue() == 8 || Hand[index - 1].GetValue() == Topcard.GetValue())
{
return true;
}
else
return false;
}
};
// overloads the output to cout the card
ostream & operator << (ostream &left, CPlayer right)
{
for (int i = 0; i < right.NumberOfCard; i++)
{
left << i + 1 << ") " << right.Hand[i] << endl;
}
return left;
}
The program I've written creates a deck of card that can be shuffled and player that takes four cards and puts them into for corresponding columns.
I can display a single card by doing this:
int main()
{
Card card;
cout << card << endl;
}
The problem is, I want to display my player class, which is made up of a vector of cards and should display four cards and put them into four separate columns. But using this:
int main()
{
Deck deck;
deck.shuffle();
Player player;
cout << player;
}
does not display anything, in fact, it gives me an error.
How can I properly display my four cards in their corresponding four columns?
Also, here is the entire code I have so far, in case you want to go through it yourself:
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;
enum suits
{
diamond, club, heart, spade
};
class Card
{
private:
int rank;
suits suit;
public:
Card();
Card(suits, int);
int getRank() { return rank; }
suits getSuit() { return suit; }
void setRank(int rankvalue) { rank = rankvalue; }
void setSuit(suits suitvalue) { suit = suitvalue; }
};
ostream & operator<<(ostream &, Card);
Card::Card()
{
rank = 1;
suit = spade;
}
Card::Card(suits suitvalue, int rankvalue)
{
rank = rankvalue;
suit = suitvalue;
}
ostream & operator<<(ostream & out, Card aCard)
{
switch (int rank = aCard.getRank())
{
case 1: out << "Ace"; break;
case 11: out << "Jack"; break;
case 12: out << "Queen"; break;
case 13: out << "King"; break;
default: out << rank;
}
switch (suits suit = aCard.getSuit())
{
case diamond: out << " of Diamonds"; break;
case spade: out << " of Spades"; break;
case heart: out << " of Hearts"; break;
case club: out << " of Clubs"; break;
}
return out;
}
class RandomInteger
{
public:
RandomInteger();
unsigned int operator() (unsigned int max);
};
RandomInteger::RandomInteger()
{
srand(time(0));
}
unsigned int RandomInteger::operator()(unsigned int max)
{
unsigned int rval = rand();
return rval % max;
}
RandomInteger randomizer;
class Deck
{
Card cards[52];
int topCard;
public:
Deck();
void shuffle();
bool isEmpty() { return topCard <= 0; }
Card draw();
};
extern RandomInteger randomizer;
Deck::Deck()
{
topCard = 0;
for (int i = 1; i <= 13; i++)
{
Card c1(diamond, i), c2(spade, i), c3(heart, i), c4(club, i);
cards[topCard++] = c1;
cards[topCard++] = c2;
cards[topCard++] = c3;
cards[topCard++] = c4;
}
}
Card Deck::draw()
{
if (!isEmpty())
return cards[--topCard];
else
{
Card spadeAce(spade, 1);
return spadeAce;
}
}
void Deck::shuffle()
{
random_shuffle(cards, cards+52, randomizer);
}
class Player
{
public:
Player();
void print();
Card draw(Deck &);
typedef vector<Card> cards;
vector<cards> column;
};
Player::Player()
{
column.push_back(vector<Card>());
column.push_back(vector<Card>());
column.push_back(vector<Card>());
column.push_back(vector<Card>());
}
Card Player::draw(Deck & aDeck)
{
for (int i = 0; i < 4; i++)
column[i].push_back(aDeck.draw());
}
void Player::print()
{
cout << "Col 1 \t Col 2 \t Col 3 \t Col 4 \n";
bool more = true;
for (int j = 0; more; j++)
{
more = false;
for (int i = 0; i < 4; i++)
if (j < column[i].size())
{
cout << column[i][j] << "\t";
more = true;
}
else
cout << "\t\t";
cout << endl;
}
}
int main()
{
Deck deck;
deck.shuffle();
Player player;
while (!deck.isEmpty());
cout << player;
}
Sorry for the amateur question, I know it's something simple, and thanks in advance.
cout << player;
This would not work, because you've not overloaded operator<< for type Player. Please first implement this:
ostream & operator<<(ostream &out, const Player & player)
{
//your implementation
}
If it needs to access private members, then make this friend of Player.
I just noticed that your class Player has a function called print, maybe you would like to call it from operator<<(). If you call it, then I would suggest you to use the following function signature:
void print(ostream &out) const
{
//use `out`, instead of `cout` now!
}
And then call this from operator<<, as:
ostream & operator<<(ostream &out, const Player & player)
{
player.print(out);
return out;
}
Advantage of this implementation is that you can output to console as well as file also.
You need to implement the overloaded operator "<<" for Player:
ostream & operator<<(ostream &, Player&);
to output the relevant information exactly as it was done for Card class.
Well the first call works because of the code "you wrote" here:
ostream & operator<<(ostream &, Card);
in the second player class "you wrote" a print class. So im assuming you can just call player.print(). But I didn't go through print to see if it works.
You need to define an operator << for your Player class as well as your Card class
ostream & operator<<(ostream & out, Player& aPlayer) {
//print out each card in turn
out << "Col 1 \t Col 2 \t Col 3 \t Col 4 \n";
bool more = true;
for (int j = 0; more; j++)
{
more = false;
for (int i = 0; i < 4; i++)
if (j < aPlayer.column[i].size())
{
out << aPlayer.column[i][j] << "\t";
more = true;
}
else
out << "\t\t";
out << endl;
return out;
}