Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

IIn C++ using a stack class (using a linked list or array) create a card game pr

ID: 3754122 • Letter: I

Question

IIn C++ using a stack class (using a linked list or array) create a card game program. The game must programmed and played as followed:

1) There is only one deck of cards with 50 cards in it. Each card has a vaule of 0-5. (Must use a stack for deck of cards. Cards must be randomized using a random # fucntion and the stack must be bult using the stack push function )

2) The game requires two players.

3) Cards are drawn off the top of the deck.

4) How cards are played:

If a card with a vaule of 0 is drawn, player gets minus 5 points and their turn is over.

If a card with a vaule of 1 is drawn, players gets 1 ppont and gets to draw again.

If a card with a value of 2 is drawn, player gets 2 points and a random card is put back on top of the deck and their turn is over.

If a card with a vaule of 3 to 5 is drawn, player gets the points and their turn is over.

5) First player to 50 wins! (If the deck of cards runs out, add another 50 cards)

Explanation / Answer

// I have used Turbo C++ compiler. Some compilers may not support predefined header files like //<stdlib.h> or <time.h> . Look into that.

#include<iostream.h>

#include <stdlib.h> // header file for rand() and srand() functions

#include <time.h>

#define max 50

class stack // a stack class

{

private:

int cards[max];

int top;

public:

stack()

{

top=-1;

initialise();

}

void initialise() // to push the random card values

{

int value;

srand(time(0)); //to use current time as a seed for genrating random number in rand() function

while(top!=max-1)

{

value = (rand() % (5 - 0 + 1)) + 0; //(rand() % (upper – lower + 1)) + lower

//is used to generate random numbers between the range of lower and upper

//Here upper = 5 and lower = 0

push(value);

}

}

int isEmpty()

{

if(top==-1)

return 1;

else

return 0;

}

int isFull()

{

if(top==(max-1))

return 1;

else

return 0;

}

int push(int n)

{

if(isFull())

{

return 0;

}

++top;

cards[top]=n;

return n;

}

int pop()

{

int data;

if(isEmpty())

{

initialise();

}

data=cards[top];

--top;

return data;

}

};

class player // a class to manage players and their score

{

public:

int score;

player()

{

score=0;

}

};

void game(player a, stack b) // a function to play game

{

int value;

value = b.pop();

if(value==0)

{

a.score-=5;

}

else if(value==1)

{

a.score+=value;

game(a,b);

}

else if(value==2)

{

a.score+=value;

b.push( (rand()%6) );

}

else

{

a.score+=value;

}

}

int main(){

  

stack deck1;

player p1,p2;

int flag=0;

do

{

game(p1 , deck1);

game(p2 , deck1);

if(p1.score>=50)

{

cout<<"Player 1 wins with score of "<<p1.score;

flag=1;

}

else if(p2.score>=50)

{

cout<<"Player 2 wins with score of "<<p2.score;

flag=1;

}

}

while(flag==0);

return 0;

}