1

私は1つの問題で立ち往生しています。これが私のコードです。と がclass Cardありclass deckOfCardsます。私の問題は関数straightsortFunc. mainという名前で作成したベクターを並べ替えたいと思いfiveHandます。多くの例を見ましたが、私の場合、これを正しく行う方法がわかりません。

次のようなエラーがあり、宣言されiていません:j

#include <iostream>

using namespace std;


class Card
{
    private:
        int m_suit;
        int m_face;
    public:
        Card(int face, int suit);
        static string suits[];
        static string faces[];
        static string toString(string s_face, string s_suit);
        int getFace();
        void setFace(int face);
        int getSuit();
        void setSuit(int suit);
};

Card::Card(int face, int suit)
{
    m_face = face;
    m_suit = suit;
} 
string Card::faces[] = {"Ace", "Deuce", "Three", "Four", "Five", 
"Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

string Card::suits[] = {"hearts", "diamonds", "clubs", "spades"};

int Card::getFace(){return m_face;}
void Card::setFace(int face){m_face = face;}
int Card::getSuit(){return m_suit;}
void Card::setSuit(int suit){m_suit = suit;}

string Card::toString(string s_face, string s_suit)
{
    string card = s_face + "\tof\t " + s_suit;
    return card;
}




#include <iostream>     // cout
#include <algorithm>    // random_shuffle
#include <vector>       // vector
#include <ctime>        // time
#include <cstdlib> 
#include "Card.h"

using namespace std;

class deckOfCards
{
    private:
        vector<Card> deck;

    public:
        deckOfCards();
        static int count;
        static int next;
        vector<Card>& getDeck() {return deck;}
        void shuffle(vector<Card>& deck);
        Card dealCard();
        bool moreCards();
        void pairs(vector<Card>& fiveHand);
        void threeOfKind(vector<Card>& fiveHand);
        void fourOfKind(vector<Card>& fiveHand);
        void flush(vector<Card>& fiveHand);
        void straight(vector<Card>& fiveHand);
        bool sortFunc(vector<Card>&  fiveHand[i], vector<Card>&  fiveHand[j]);

};

int deckOfCards::count = 0;
int deckOfCards::next = 0;

deckOfCards::deckOfCards()
{
    const int FACES = 13;
    const int SUITS = 4;
    for (int suit = 0; suit < SUITS; suit++)
        {
            for (int face = 0; face < FACES; face++)
                {
                    Card card = Card(face, suit); //card created and initialized
                    deck.push_back(card);
                }
        }

}

void deckOfCards::shuffle(vector<Card>& deck)
{
    srand(static_cast<unsigned int>(time(0)));  
    random_shuffle(deck.begin(), deck.end());
}

Card deckOfCards::dealCard()
{
    Card nextCard = deck[next];
    next++;
    return nextCard;
}

bool deckOfCards::moreCards()
{
    const int FIVE_CARD = 5;
    if (count < FIVE_CARD)
    {
        count++;
        return true;
    }
    else
        return false;
}

void deckOfCards::pairs(vector<Card>& fiveHand)
{
    int pair = 0;
    const int MAX_CARDS = 5;
    for (int i = 0; i < MAX_CARDS; i++)
    {
        int j = i+1;
        for (j; j < MAX_CARDS; j++)
        {
            if (fiveHand[i].getFace() == fiveHand[j].getFace())
                pair++;
        }
    }
    if (pair == 1)
    {
        cout << "You have one pair!" << endl;
    }
    else if (pair == 2)
    {
        cout << "You have two pairs!" << endl;
    }
}


void deckOfCards::threeOfKind(vector<Card>& fiveHand)
{
    if (((fiveHand[0].getFace() == fiveHand[1].getFace())&&(fiveHand[1].getFace() == fiveHand[2].getFace()))
        ||((fiveHand[1].getFace() == fiveHand[2].getFace())&&(fiveHand[2].getFace() == fiveHand[3].getFace()))
        ||((fiveHand[2].getFace() == fiveHand[3].getFace())&&(fiveHand[3].getFace() == fiveHand[4].getFace())))
        {
            cout << "Your hand contains three cards of the same faces!" << endl;
        }

}

void deckOfCards::fourOfKind(vector<Card>& fiveHand)
{
    if (((fiveHand[0].getFace() == fiveHand[1].getFace())
        &&(fiveHand[1].getFace()== fiveHand[2].getFace())
        &&(fiveHand[2].getFace()== fiveHand[3].getFace()))
        ||((fiveHand[1].getFace() == fiveHand[2].getFace())
        &&(fiveHand[2].getFace() == fiveHand[3].getFace())
        &&(fiveHand[3].getFace() == fiveHand[4].getFace())))
    {
        cout << "Your hand contains four cards of the same faces!" << endl;
    }   
}

void deckOfCards::flush(vector<Card>& fiveHand)
{
    if ((fiveHand[0].getSuit() == fiveHand[1].getSuit())
        &&(fiveHand[1].getSuit() == fiveHand[2].getSuit())
        &&(fiveHand[2].getSuit() == fiveHand[3].getSuit())
        &&(fiveHand[3].getSuit() == fiveHand[4].getSuit()))
        {
            cout << "Congradulations!!! You have a flush!!!" << endl;
        }   
}

void deckOfCards::straight(vector<Card>& fiveHand)
{
    //sort cards based on faces
    sort(fiveHand.begin(), fiveHand.end(), sortFunc(vector<Card>&  fiveHand[i], vector<Card>&  fiveHand[j])); 
    if ((fiveHand[1].getFace() == (fiveHand[0].getFace()+1))
        &&(fiveHand[2].getFace() == (fiveHand[1].getFace()+1))
        &&(fiveHand[3].getFace() == (fiveHand[2].getFace()+1))
        &&(fiveHand[4].getFace() == (fiveHand[3].getFace()+1)))
        {
            cout << "You're so lucky!!! You have a straight!" << endl;
        }
}

bool deckOfCards::sortFunc(vector<Card>&  fiveHand[i], vector<Card>&  fiveHand[j])
{
    if (fiveHand[i].getFace() < fiveHand[j].getFace())
        return true;
    else
        return false;
}



#include "deckOfCards.h"


using namespace std;

int main(int argc, char** argv) 
{
    deckOfCards cardDeck; // create DeckOfCards object
    vector<Card> fiveHand;
    cardDeck.shuffle(cardDeck.getDeck()); // shuffle the cards in the deck

    cout << "Your cards are: \n" << endl;
    while (cardDeck.moreCards() == true)
    {
        Card currCard = cardDeck.dealCard();
        fiveHand.push_back(currCard);
        string face = Card::faces[currCard.getFace()];
        string suit = Card::suits[currCard.getSuit()];
        string card = Card::toString(face,suit);
        cout << card << endl;
    }
    cout << endl;
    cardDeck.pairs(fiveHand);
    cardDeck.threeOfKind(fiveHand);
    cardDeck.fourOfKind(fiveHand);
    cardDeck.flush(fiveHand);
    cardDeck.straight(fiveHand);

    return 0;
}
4

3 に答える 3

1

ベンの答えに追加するには、彼が提案した変更に加えてCard::getFace、おそらくCard::getSuitconst メソッドも作成する必要があります。const Cardそうしないと、そのようなのインスタンスでこれらのメソッドを呼び出すことができませんsortFunc

int Card::getFace() const { return m_face; }
int Card::getSuit() const { return m_suit; }

ベンのアイデアのような自由な関数を作成するオプションがあることに注意してください。sortFuncまたは、内部で静的関数にすることもできますdeckOfCards

class deckOfCards
{
public:
    // ...
    static bool sortFunc(const Card &lhs, const Card &rhs);
};

sortFuncこれは、 で使用することを意図したコードでより適切に通信できる場合がありclass deckOfCardsます。

最後に、sortFunc安定した比較を行うように変更する必要があります。

bool deckOfCards::sortFunc(const Card &lhs, const Card &rhs)
{
  return !(rhs.getFace() < lhs.getFace());
}

への呼び出しは次のようにstd::sortなります。

std::sort(fiveHand.begin(), fiveHand.end(), deckOfCards::sortFunc);
于 2013-11-13T02:29:40.130 に答える
1

sortFunc() is declared wrong, you have:

 bool deckOfCards::sortFunc(vector<Card>&  fiveHand[i], vector<Card>&  fiveHand[j])

Where i and j is undeclared, the correct function declaration should be:

 //if you want to call sortFunc like sortFunc(deck[0], deck[1])
 bool deckOfCards::sortFunc(Card& first, Card& second)
 //if you want to call sortFunc like sortFunc(deck0, deck1), then
 bool deckOfCards::sortFunc(vector<Card>& first, vector<Card>& second)

If you want to std::sort vector<Card>s, its best to overload the < operator:

friend bool operator<(const Card &first, const Card &second); //to Card class
bool operator<(const Card &first, const Card &second)
{
    if (first.getFace() < second.getFace())
        return true;
    return false;
}
于 2013-11-13T02:15:55.757 に答える
0

問題の一部は、sortFunc2 つのベクトルを使用することであり、それを使用して単一のベクトルをソートしようとしています。次のように定義する必要があります。

bool sortFunc(const Card &first, const Card &second)
{
    if (first.getFace() < second.getFace())
        return true;
    else
        return false;
}

2 番目の問題: への呼び出しsortが正しくありません。関数 likesortFuncを引数として渡すときは、括弧やパラメーターを含めません。次のように、関数名を変数名のように扱うだけです。

sort(fiveHand.begin(), fiveHand.end(), sortFunc);

sortFuncまた、メンバー関数ではなくスタンドアロン関数として定義していることにも注意してください。上記の呼び出しをsortFuncメンバー関数として実行しようとしても、その型のオブジェクトがないとメンバー関数を呼び出すことができず、使用できるオブジェクトがないため、機能sortしませんdeckOfCards

于 2013-11-13T02:21:48.413 に答える