Shuffle Code
- I added some shuffle code. It is in the following listing in blue.
- http://www.cplusplus.com/reference/algorithm/random_shuffle/
Code
// // main.cpp // Nov28Cards // #include <iostream> #include <vector> #include <string> using namespace std; enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }; enum Rank { ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING }; struct Card { Suit suit; Rank rank; Card(); Card(Suit suit, Rank rank); void print(); bool equals (const Card &c2) const; bool isGreater (const Card &c2) const; }; // -------------------------------------------------- // Card methods // -------------------------------------------------- bool Card::isGreater(const Card &c2) const { if (suit > c2.suit) return true; if (suit < c2.suit) return false; return (rank > c2.rank); } Card::Card() { suit = CLUBS; rank = ACE; } Card::Card(Suit suit, Rank rank) { this->suit = suit; this->rank = rank; } void Card::print () { vector <string> suits (4); vector <string> ranks (14); suits[0] = "Clubs"; suits[1] = "Diamonds"; suits[2] = "Hearts"; suits[3] = "Spades"; ranks[1] = "Ace"; ranks[2] = "2"; ranks[3] = "3"; ranks[4] = "4"; ranks[5] = "5"; ranks[6] = "6"; ranks[7] = "7"; ranks[8] = "8"; ranks[9] = "9"; ranks[10] = "10"; ranks[11] = "Jack"; ranks[12] = "Queen"; ranks[13] = "King"; cout << ranks[rank] << " of " << suits[suit] << endl; } bool Card::equals(const Card &c2) const { return (suit == c2.suit && rank == c2.rank); } // -------------------------------------------------- // other methods // -------------------------------------------------- bool equals (const Card &c1, const Card &c2) { return (c1.suit == c2.suit && c1.rank == c2.rank); } vector <Card> deck (52); void printDeck(vector <Card> &deck) { for (int i=0; i<deck.size(); i++) { deck[i].print(); } } // -------------------------------------------------- // Binary search // -------------------------------------------------- int findBisect (const Card &card, const vector<Card> &deck, int low, int high) { cout << low << ", " << high << endl; // debug if (high < low) return -1; int mid = (low + high) / 2; if (equals (deck[mid], card)) return mid; if (deck[mid].isGreater(card)) { return findBisect (card, deck, low, mid-1); } else { return findBisect(card, deck, mid+1, high); } } int myrandom(int i) { return rand() % i; } // -------------------------------------------------- // start here // -------------------------------------------------- int main(int argc, const char * argv[]) { Card threeOfClubs (CLUBS, THREE); threeOfClubs.print(); Card jack (DIAMONDS, JACK); jack.print(); cout << threeOfClubs.equals(jack) << endl; cout << equals (threeOfClubs, jack) << endl; Card another3clubs (CLUBS, THREE); cout << threeOfClubs.equals(another3clubs) << endl; cout << jack.isGreater(threeOfClubs) << endl; // nested loop int i = 0; for (Suit suit = CLUBS; suit <=SPADES; suit = Suit(suit+1)) { for (Rank rank=ACE; rank<=KING; rank = Rank(rank+1)) { deck[i].rank = rank; deck[i].suit = suit; i++; } } printDeck(deck); cout << findBisect(deck[24], deck, 0, 51); Card fake (DIAMONDS, Rank(15)); cout << findBisect (fake, deck, 0, 51); srand(time(0)); random_shuffle(deck.begin(), deck.end(), myrandom); printDeck(deck); return 0; }
No comments:
Post a Comment