Need help with outputting and deleting elements in an array for a War card game

Hi I was wondering if anyone could help me out with this problem I'm having with my program in terms of getting the card to be removed from the array as well as the choice so that user doesn't pick it. Bellow is the code for the program. Hopefully you guys can help me figure out what can fix this issue because I have no clue what to do.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include "Card.h"
#include "Deck.h"
#include "Player.h"
#include "HumanPlayer.h"
using namespace std;
int main()
{
   Deck theDeck; // create a deck
   theDeck.shuffle();
   // create two players
   HumanPlayer player1(theDeck); // kay 8/19/98, was Player
   Player player2(theDeck);
   while (!theDeck.isEmpty() || (!player1.handEmpty() && !player2.handEmpty())) {
       Card card1 = player1.draw();
       cout << "Player 1 plays: " << card1 << endl;
       Card card2 = player2.draw();
       cout << "Player 2 plays: " << card2 << endl;
       if (card1.getRank() == card2.getRank()) { // tie
           player1.addPoints(1);
           player2.addPoints(1);
           cout << "Players tie\n\n";
       }
       else if (card1.getRank() > card2.getRank()) {
           player1.addPoints(2);
           cout << "Player 1 wins this round\n\n";
       }
       else {
           player2.addPoints(2);
           cout << "Player 2 wins this round\n\n";
       }
      
       if (!theDeck.isEmpty()) {
           // replace the cards drawn
           player1.replaceCard(theDeck);
           player2.replaceCard(theDeck);
       }
       else {
           player1.cardsLeft();
           player2.cardsLeft();
       }
   }
   cout << "Player 1 score: " << player1.score() << endl;
   cout << "Player 2 score: " << player2.score() << endl;
   if (player1.score() > player2.score())
       cout << "Player 1 wins\n";
   else if (player2.score() > player1.score())
       cout << "Player 2 wins\n";
   else
       cout << "It is a tie\n";
   return 0;
}


Random.h

1
2
3
4
5
6
7
8
9
10
#ifndef RANDOM_H
#define RANDOM_H

class RandomInteger {
    public:
        RandomInteger();
        unsigned int operator()(unsigned int max);
}; // RandomInteger

#endif 


Random.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdlib.h>       // for rand()
#include <time.h>         // kay 8/20/98, for time()

#include "Random.h"

using namespace std;

RandomInteger::RandomInteger()
{
    srand(time(0));       // kay 8/20/98
} // constructor

unsigned int RandomInteger::operator()(unsigned int max)
{
    unsigned int rval = rand();
    return rval % max;
} // operator()

RandomInteger randomizer;


Player.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef PLAYER_H
#define PLAYER_H

#include "Card.h"
#include "Deck.h"
class Player {
protected:     // kay 8/20/98, was private
   int size = 3;
   Card myCards[3];
   int myScore;
   int removedCard;
   int remainingCards = 3;
public:
   Player(Deck &);
   Card draw(); // another draw()!
   void addPoints(int howMany) { myScore += howMany; }
   int score() { return myScore; }
   void replaceCard(Deck &aDeck) { myCards[removedCard] = aDeck.draw(); }
   void cardsLeft() { remainingCards -= 1;}
   bool handEmpty() { return remainingCards <= 0; }
}; // Player
#endif 


Player.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Player.h"
#include "Deck.h"
#include "Random.h"

using namespace std;

extern RandomInteger randomizer;

Player::Player(Deck & aDeck)
{
    myScore = 0;
    for (int i = 0; i < 3; i++)
        myCards[i] = aDeck.draw();
    removedCard = 0;
} // constructor

Card Player::draw()
{
    removedCard = randomizer(3);
    return myCards[removedCard];
} 


HumanPlayer.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef HUMANPLAYER_H
#define HUMANPLAYER_H

#include "Player.h"
#include "Card.h"
#include "Deck.h"

class HumanPlayer: public Player {
public:
    HumanPlayer(Deck &deck): Player(deck) {} // kay 8/20/98
                                             // calls base constructor
    Card draw(); // another draw()!
}; // HumanPlayer

#endif 


HumanPlayer.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

#include "HumanPlayer.h"
#include "Card.h"

using namespace std;

Card HumanPlayer::draw()
{
    cout << "You currently hold in your hand:" << endl;
    cout << "a) " << myCards[0] << endl;
    cout << "b) " << myCards[1] << endl;
    cout << "c) " << myCards[2] << endl;
    cout << "Which one do you want to play? ";

    char answer[80];
    removedCard = -1;
    while (removedCard == -1) {
        cin >> answer;
        if (answer[0] == 'a')
            removedCard = 0;
        else if (answer[0] == 'b')
            removedCard = 1;
        else if (answer[0] == 'c')
            removedCard = 2;
        if (removedCard != -1)
            return myCards[removedCard];
        cout << "Please specify a, b, or c\n";
    }
} 


Deck.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef DECK_H
#define DECK_H

#include "Card.h"

class Deck {
    Card cards[52];       // deck of 52 cards
    int topCard;          // 1 + index of next available card
public:
    Deck();
    // shuffle uses the generic algorithm random_shuffle from STL
    void shuffle();
    bool isEmpty() { return topCard <= 0; }
    Card draw();          // return next card
}; // Deck

#endif 


Deck.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <algorithm>

#include "Card.h"
#include "Deck.h"
#include "Random.h"

using namespace std;

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;
    }
} // constructor

Card Deck::draw()
{
    if (!isEmpty())
        return cards[--topCard];
    else { 
        Card spadeAce(spade, 1);
        return spadeAce;
    }
} // draw

void Deck::shuffle()
{
    random_shuffle(cards, cards+52, randomizer);
} 


Card.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef CARD_H
#define CARD_H

#include <iostream>

using namespace std;

enum suits { diamond, club, heart, spade };

class Card {
    // private by default
    int      rank;        // rank of card
    suits    suit;        // suit of card
public:
    // constructors
    Card();               // initialize a card with default values
    Card(suits, int);     // initialize a card with given values
    // accessors
    int getRank() { return rank; } // an in-line function
    suits getSuit() { return suit; }
    // mutators
    void setRank(int rv) { rank = rv; }
    void setSuit(suits sv) { suit = sv; }
}; 
 
ostream & operator<<(ostream &, Card );

#endif 


Card.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "Card.h"

using namespace std;

Card::Card()
{
    rank = 1;
    suit = spade;
} // constructor

Card::Card(suits sv, int rv)
{
    rank = rv;
    suit = sv;
} // constructor

// output a textual presentation of a Card
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;
} 
Last edited on
Topic archived. No new replies allowed.