Click here to Skip to main content
15,867,962 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not able to compile the program. I kept getting an error.

What I have tried:

#include "Card.hpp"

Card::Card(char v, char s)
//In the constructor, the first parameter represents the value of the card
// and the second parameter represents the suit of the card
//Use a member initializer list to assign starting values to the attributes
    :value{new char(v)}
    //The value of the card is represented as a char so that it can be digits (2 through 9),
    // as well as the (J)ack, (Q)ueen, (K)ing, and (A)ce; the 0 character represents the value 10
    ,suit{new char(s)}
    // The suit of the card must take on the values (D)iamond, (H)eart, (C)lub, and (S)pade
    ,playerId{new unsigned (0)}
    //Use 0 as the initial value for the playerId attribute;
    ,discard{new bool(false)}
{
}

Card::~Card() {
  delete value;
  delete suit;
  delete playerId;
  delete discard;

  value = nullptr;
  suit = nullptr;
  playerId = nullptr;
  discard = nullptr;
    //delete
}

const bool Card::isDiscard() {
    // discard flag is used to determine if the card
    // is currently in the player's discard pile or
    // is still active in the game??
}

const unsigned int Card::getPlayerId() {
  return *playerId;

}

const char Card::getSuit() {
  return *suit;

}

const char Card::getValue() {
  return *value;

}

void Card::setPlayerId(unsigned int p) {

}

void Card::flipDiscard() {
    //The flipDiscard method changes the value of the discard flag to its opposite value
    *discard = !discard; //confirm
}

char Card::intToSuit(int suit){
    char suits [4] = {'H', 'D', 'S', 'C'};
    return suits[suit];
}

char Card::intToValue(int value) {
    char ranks [13] = {'2', '3', '4', '5', '6', '7', '8', '9', '0', 'J', 'Q', 'K', 'A'};
    return ranks[value];
}

#ifndef GO_FISH_CARD_HPP
#define GO_FISH_CARD_HPP

class Card {

private:
    char* value;
    char* suit;
    unsigned* playerId;
    bool* discard;

public:
    Card(char v, char s);
    ~Card();
    const char getValue(); //returns the value of card
    const char getSuit(); //returns the suit of the card
    const unsigned getPlayerId(); // find the player id
    const bool isDiscard();
    void setPlayerId(unsigned p);
    void flipDiscard();
    static char intToSuit(int suit);
    static char intToValue(int value);
};

#endif //GO_FISH_CARD_HPP

#include "Card.hpp"
#include "Player.hpp"
#include <cstddef>
#include <iomanip>
#include <iostream>

using namespace std;

/*******************************************************************************
 *  Function prototypes
*******************************************************************************/

bool isGameOver(Card*[], const unsigned);
Card* assignRandomCard(Card*[], const unsigned, Player*);
void checkScore(Card*[], const unsigned, Player*, char);

/*******************************************************************************
 *  Description:
 *      Starting point of the program. Seed the RNG using the current time.
 *      Creates the deck of cards and 2 players. Contains logic to simulate
 *      Go Fish. Releases dynamic memory.
 *
 *  Input(s):
 *      N/A
 *
 *  Output:
 *      An integer that signals the exit code to the operating system (OS)
*******************************************************************************/

int main() {
    // seed RNG
    srand(time(0));

    // deck variables
    const unsigned DECK_SIZE = 52;
    Card* deck[DECK_SIZE];
    // TODO: create the deck of cards as an array of dynamic objects
    int count = 0;
    for(int r=0; r < 13; r++){
        for(int S=0; S < 4; S++){
            char v = Card::intToValue(r), s = Card::intToSuit(S);
            deck[count] = new Card(v, s);
//            Card::Card(v , s);
        count ++;
        }
    }

    // welcome message
    cout << setfill('*') << setw(50) << '\n';
    cout << "Welcome to my Go Fish game!\n";
    cout << setfill('*') << setw(50) << '\n' << endl;

    // TODO: create the two players dynamically

    cout << "Player 1, enter your name:";
    string* player1;
    cin >> *player1;
//    Player::Player(player1);

    cout << "Player 2, enter your name:";
    string* player2;
    cin >> *player2;
//    Player player2(*n2);
    // (label the variables as player1 and player2)

    // randomly assign 7 cards from the deck to each player
    Card* cardPtr = nullptr;
    for (unsigned cardNum = 0; cardNum < 7; cardNum++) {
        cardPtr = assignRandomCard(deck, DECK_SIZE, player1);
        checkScore(deck, DECK_SIZE, player1, cardPtr->getValue());
    }
    for (unsigned cardNum = 0; cardNum < 7; cardNum++) {
        cardPtr = assignRandomCard(deck, DECK_SIZE, player2);
        checkScore(deck, DECK_SIZE, player2, cardPtr->getValue());
    }

    // game loop
    char value = '\0';
    bool handFlag = false;
    Player* currentPlayer = player1;
    Player* otherPlayer = player2;
    while (!isGameOver(deck, DECK_SIZE)) {
        cout << "\n\n\n\n\n";
        cout << "The current player is " << currentPlayer->getName() << endl;
        cout << "Your score: " << currentPlayer->getScore() << endl;

        // print the player's hand, if possible
        handFlag = false;
        cout << "Your hand is: ";
        for (unsigned i = 0; i < DECK_SIZE; i++) {
            if (deck[i]->getPlayerId() == currentPlayer->getId() &&
                !deck[i]->isDiscard())
            {
                handFlag = true;
                cout << deck[i]->getValue() << '/' << deck[i]->getSuit() << ' ';
            }
        }

        // the player's hand is empty, let them know!
        if (!handFlag) {
            cout << "empty!\n";
        }

        // otherwise, let the player ask
        else {
            cout << endl;
            value = currentPlayer->ask(deck, DECK_SIZE);
        }

        // check if the other player has that card value
        if (handFlag && otherPlayer->check(deck, DECK_SIZE, value)) {
            // display message
            cout << "Nice, good guess!\n";

            // TODO: reassign all the cards from the other player
            //       that match the value

            // update score?
            checkScore(deck, DECK_SIZE, currentPlayer, value);
        }

        // otherwise, the player needs to go fish for a new card
        else {
            // display message
            cout << "Oops, go fish!\n";

            // get a new random card for the player
            cardPtr = assignRandomCard(deck, DECK_SIZE, currentPlayer);

            // update score?
            checkScore(deck, DECK_SIZE, currentPlayer, cardPtr->getValue());

            // if the card they got was the one they asked for, they go again
            if (cardPtr->getValue() == value) {
                cout << "Oh wait... you drew the card you wanted! Go again!\n";
                continue;
            }
        }

        // alternate the players
        if (currentPlayer == player1) {
            currentPlayer = player2;
            otherPlayer   = player1;
        }
        else {
            currentPlayer = player1;
            otherPlayer   = player2;
        }
    }

    // print out the final scores and determine the winner
    cout << "\n\n\n\n\n";
    cout << setfill('*') << setw(50) << '\n';
    cout << "Final scores!\n";
    cout << player1->getName() << " has " << player1->getScore() << " points\n";
    cout << player2->getName() << " has " << player2->getScore() << " points\n";
    if (player1->getScore() > player2->getScore()) {
        cout << player1->getName() << " wins!\n";
    }
    else if (player1->getScore() < player2->getScore()) {
        cout << player2->getName() << " wins!\n";
    }
    else {
        cout << "Wow, a tie, is that even possible?\n";
    }
    cout << setfill('*') << setw(50) << '\n';

    // TODO: release dynamic memory

    // terminate
    return 0;
}

bool isGameOver(Card*[], const unsigned){
    
}

Card* assignRandomCard(Card*[], const unsigned, Player*){
    
}

void checkScore(Card*[], const unsigned, Player*, char){

}

#include "Player.hpp"


int randomNum = 1000 + (rand() % 8999);

Player::Player(std::string n)
//the parameter represents the name of the player
//Use a member initializer list to assign starting values to the attributes
    :name{new std::string(n)}
    ,id{new unsigned int(randomNum)}
    //The id must be initialized to a random value between 1000 and 9999 (inclusive)
    ,score{new unsigned(0)}
{

}

Player::~Player() {
  delete name;
  delete id;
  delete score;

  name = nullptr;
  id = nullptr;
  score = nullptr;
    //delete

}

const unsigned int Player::getScore() {
    return *score;
}

std::string Player::getName() {
    return *name;
}

const unsigned int Player::getId() {
    return *id;
}

const char Player::ask(int) {
    
}

const bool Player::check(int) {
    

}

void Player::updateScore() {
    
    *score = *score + 1;//confirm this
}

#include "string"
#include <string>

#ifndef GO_FISH_PLAYER_HPP
#define GO_FISH_PLAYER_HPP


class Player {
private:
    unsigned int* id;
    std::string* name;
    unsigned* score;

public:
    Player(std::string n);
    ~Player();

    const unsigned getId();
    std::string getName();
    const unsigned getScore();

    void updateScore();

    const char ask(deck : Card*[], const unsigned DECK_SIZE);
    const bool check(deck : Card*[], const unsigned DECK_SIZE, char value);
};


#endif //GO_FISH_PLAYER_HPP
Posted
Updated 9-Apr-22 23:29pm
Comments
Patrice T 9-Apr-22 22:39pm    
And you plan to tell us what is the error and where it is ?
Dave Kreskowiak 10-Apr-22 0:36am    
All that code and you forgot to tell us what the errors are.
OriginalGriff 10-Apr-22 1:44am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information.

I think you should change some variable types and revise your constructor. It makes no sense to allocate single values:
C++
// in the header -

class Card
{
private:
   unsigned playerId;
   bool discard;
   char suit;
   char value;
};

// the constructor :

Card::Card( char v, char s )
   : value( v )
   , suit( s )
   , playerId( 0 )
   , discard( false )
{
}

/////////////////////////////////////////////
// the player class also needs to be revised

class Player
{
private:
    std::string name;
    unsigned id;
    unsigned score;
};


Player::Player( std::string n, int random )
    : name( n )
    , id( random )
    , score( 0 )
{
}
 
Share this answer
 
Comments
merano99 10-Apr-22 5:33am    
+5, see comment in my solution
Rick has already commented on the ugly constructors. Of course you have to the deconstructors
are then also improved. The detail with the random number could be solved without parameters.
Under C++ there are also much better ways to generate random numbers.
C++
unsigned randomNum() {
   // TODO: replace rand()
   return (1000 + (rand() % 8999));
}

//the parameter represents the name of the player
//Use a member initializer list to assign starting values to the attributes
Player::Player(std::string n): name(n), score(0)
//	:name{ new std::string(n) }
//	, id{ new unsigned int(randomNum) }
//	, score{ new unsigned(0) }
{
  // The id must be initialized to a random value between 1000 and 9999 (inclusive)
  id = randomNum();
}

Player::~Player() {
	// delete name; delete id; delete score;
}

According to the player.hpp header, deck is currently called when the ask() and check() methods are called as
pass parameters. In the header, the data type for this is initially wrong. Even if you see him
corrected in the class definition, there is no suitable implementation in player.cpp.
C++
//const char Player::ask(int) {
const char Player::ask(Card* deck[], const unsigned DECK_SIZE){
// TODO:
}

//const bool Player::check(int) {
const bool Player::check(Card* deck[], const unsigned DECK_SIZE){
// TODO:
}

But it would be better to use the approach of the existing implementers.
One possibility would be to declare deck as a static member variable.

There are a lot of other problems, e.g.
C++
cardPtr = assignRandomCard(deck, DECK_SIZE, player1);

The argument player1 was declared as std::string, but the pointer is expected
to an instance of Player.

Since these are all obviously things that a compiler will find without problems
I'm not analyzing it any further and am waiting for the code to be revised.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900