This is a c++ program. The game company, Silly Little Games, has come up with a
ID: 3901324 • Letter: T
Question
This is a c++ program. The game company, Silly Little Games, has come up with a new card game for you to simulate. It uses a special set of cards that contain the numbers 1 to 13 and there are four copies of each card in the deck. The first person to play all the cards in their hand wins. The game is played as follows: The cards are shuffled and placed into a stack Each player is dealt 7 cards from the stack in a round-robin fashion and their cards are placed into their queue The next card in the deal stack is placed into the discard stack For their turn, each player plays the next card in his/her queue. If the card the player plays is HIGHER in number than the one on the top of the discard stack, the player's turn is over. If the card the player plays is EQUAL in number to the one on the top of the discard stack, the player must then take one card from the deal stack and the player's turn is over. If the player's card is LOWER in number than the one on the discard stack, the player must take two cards from the deal stack and the player's turn is over. If the deal stack runs out of cards, "turn over" the discard stack and continue the game. Note that you must keep the same card on the top of the discard stack, so you will need to hold onto that card and push it back onto the discard stack before continuing the game. The first player to run out of cards wins the game. Offer to repeat the game as many times as desired. Notes: There are only two players. You may ask for names or simply call them by whatever names your game chooses. Remember to design your user interface carefully. You must use a stack for the shuffled cards, a stack for the discard pile and a queue for each player - all of type int.
There are 5 files 'stack.h','stack.cpp','queue.h','queue.cpp','main.cpp' The stack and queue public classes should have destructor, constructor, copy constructor and operator assignment. The stack public class should only contain the "pop","push","isEmpty","peek" . Other privates can be added. The queue public class should only "enqueue","dequeue","peek","isEmpty". Other privates can be added. This program should be written in c++.
Explanation / Answer
Answer: See the code for Stack and Queue below:
1. Stack class:
Stack.h
------------------------------------------
#ifndef STACK_H_
#define STACK_H_
#include <iostream>
#define MAX_CARDS 52
using namespace std;
class Stack {
private:
int storage[MAX_CARDS];
int top;
int count;
public:
Stack();
virtual ~Stack();
Stack(const Stack&);
void operator=(const Stack&);
void push(int);
int pop();
bool isEmpty();
int peek();
};
#endif /* STACK_H_ */
-------------------------------------------
Stack.cpp:
-----------------------------------------
#include "Stack.h"
//Constructor
Stack::Stack() {
top = -1;
count=0;
}
//Destructor
Stack::~Stack() {
// TODO Auto-generated destructor stub
}
//Copy constructor
Stack::Stack(const Stack &st)
{
this->top = st.top;
this->count=st.count;
this->storage=st.storage;
}
//overloaded assignment operator
void Stack::operator=(const Stack &st)
{
this->top = st.top;
this->count=st.count;
this->storage=st.storage;
}
//isEmpty function
bool Stack::isEmpty()
{
return (top==-1);
}
//push function
void Stack::push(int x)
{
if(top!=MAX_CARDS)
{
storage[++top]=x;
count++;
}
else
cerr<<"Overflow"<<endl;
}
//pop function
int Stack::pop()
{
int temp;
if(!isEmpty())
{
temp=storage[top--];
count--;
return temp;
}
else
{
cerr<<"Underflow"<<endl;
return NULL;
}
}
-------------------------------------------
Queue class:
Queue.h
-----------------------------------------------
#ifndef QUEUE_H_
#define QUEUE_H_
#include <iostream>
#define MAX_CARDS 52
using namespace std;
class Queue {
private:
int storage[MAX_CARDS];
int front;
int rear;
int count;
public:
Queue();
virtual ~Queue();
Queue(const Queue&);
void operator=(const Queue&);
bool isEmpty();
void enqueue(int);
int dequeue();
int peek();
};
#endif /* QUEUE_H_ */
------------------------------------
Queue.cpp:
----------------------------------------
#include "Queue.h"
//constructor
Queue::Queue() {
front = -1;
rear = -1;
count = 0;
}
//destructor
Queue::~Queue() {
// TODO Auto-generated destructor stub
}
//copy constructor
Queue::Queue(const Queue &q)
{
this->front=q.front;
this->rear=q.rear;
this->count=q.count;
this->storage=q.storage;
}
//overloaded assignment operator
void Queue::operator=(const Queue &q)
{
this->front=q.front;
this->rear=q.rear;
this->count=q.count;
this->storage=q.storage;
}
//isEmpty() function
bool Queue::isEmpty()
{
return (count==0);
}
//enqueue function
void Queue::enqueue(int x)
{
if(count!=MAX_CARDS)
{
rear=(rear+1)%MAX_CARDS;
storage[rear]=x;
count++;
}
else
{
cerr<<"Overflow"<<endl;
}
}
//dequeue function
int Queue::dequeue()
{
int temp;
if(!isEmpty())
{
temp=storage[front];
front=(front+1)%MAX_CARDS;
count--;
return temp;
}
else
{
cerr<<"Underflow"<<endl;
return NULL;
}
}
//peek function
int Queue::peek(){
return storage[front];
}
----------------------------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.