Search function?

I have a program that creates 52 cards, shuffles them, then displays the deck to the screen. Now i have a tricky part! I would like to enter 5 or more cards then do a search of the deck and return the deck with those entered cards in their respective positions generated by the MT19937 number generator.basically:

1- Please enter your cards (as characters).
2- Seed init_genrand(time(NULL)).
3- Genrate numbers based on time.
4- Search deck for card positions.
5- Return deck in shuffled order based on step 1.
6- Option to do another check.

If there are any willing helpers to give me advice im sure i can help others in the same way :)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
*******************************************************************************
// cardplay-1/main.cpp -- Very simple program to deal cards.
// Summary:     Reads a number and then "deals" that many cards.


#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
#include "card.h"

#include "deck.h"

//================================================== main
int main() 

{ 
    int numOfCards;   
  

    Deck deck;
    
    while (cin >> numOfCards ) // Input number for how many cards to deal.
	{
		
        deck.shuffle();
        
        cout << "";
        for (int cardNum=0; cardNum<numOfCards; cardNum++) 
		{
            Card c = deck.dealOneCard();  
            string suit = c.getSuit();
            string face = c.getFace();
            cout << face << suit << " ";
        }
        cout << endl;
    }

    return 0();
}//end main
*****************************************************************************
// cardplay-1/deck.cpp


#include <cassert>
#include <cstdlib>
#include <string>
#include <algorithm>
#include <mt19937ar.c>

using namespace std;

#include "card.h"
#include "deck.h"

//=========================================== Constructor
Deck::Deck() 
{
    // Initialize the array to the ints 0-51
    for (int i=0; i<52; i++) 
	{
        _cards[i] = Card(i);
    }
    shuffle();
    _nextCardIndex = 0;  // index of next available card
}    


//================================================== deal
//  Action    : Returns random Card.

Card Deck::dealOneCard() 
{
    assert(_nextCardIndex >= 0 && _nextCardIndex<52);
    return _cards[_nextCardIndex++];
}


//================================================ shuffle
//  Action    : Shuffles Deck.
//  Returns   : 
void Deck::shuffle() 
{
	init_genrand(time(NULL));
    // Shuffle by exchanging each element randomly  
    for (int i=0; i<(52-1); i++)
	{
        int genrand_real1 = i + (genrand_int32() % (52-i));  //error here!!
        swap(_cards[i], _cards[genrand_real1]);
    }
   
 _nextCardIndex = 0;
}
********************************************************************************
// cardplay-1/card.cpp


#include <string>
using namespace std;

#include "card.h"

//================================================= static constants
const string Card::CARD_FACES[] = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
                                  
const string Card::CARD_SUITS[] = {"C","D","H","S"};



//================================================= Constructor
Card::Card() 
{
    _card = 0;  
}    


//================================================= Constructor
Card::Card(int card) 
{
    _card = card;
}    
    

//================================================== getFace
//  Action    : Returns face value of card.
//  Returns   : a string representing card face: "A", "2", ETC........

string Card::getFace() const 
{
    return CARD_FACES[_card%13];
}//end getFace


//================================================== getSuit
//  Action    : Returns suit of a card value.
//  Returns   : a string "S" (Spades), "H", (Hearts),"C" (Clubs), or "D" (Diamonds).

string Card::getSuit() const 
{ 
    return CARD_SUITS[_card/13];
}//end getSuit
*****************************************************************************
// cardplay-1/card.h

#ifndef CARD_H
#define CARD_H

class Card {
    public:
        Card();
        Card(int card);
        string getSuit() const;
        string getFace() const;
       
    private:
        int _card;  // range 0..51

        static const string CARD_FACES[];
        static const string CARD_SUITS[];
};  

#endif
************************************************************************
// cardplay-1/deck.h

#ifndef DECK_H
#define DECK_H

class Deck {
    public:
        Deck();
        Card dealOneCard();
        void shuffle();
        
    private:
        Card _cards[52];
        int  _nextCardIndex;
};    

#endif
[output][/output]
Sorry, I don't understand what you want. ¿Could you provide an example output?

1
2
3
4
5
6
7
    // Shuffle by exchanging each element randomly  
    for (int i=0; i<(52-1); i++)
	{
        int genrand_real1 = i + (genrand_int32() % (52-i));  //error here!!
        swap(_cards[i], _cards[genrand_real1]);
    }
   
¿what is the error?
instead of the program returning a basic shuffled deck of cards, i would like to change the program to accept card input then display the deck in order based on card input.

p.s:there is no error with the above code
¿what do you mean with 'order based on card input'?

#include <mt19937ar.c> You shouldn't include sources, but header files.
well lets say i tell the program i have ??????? cards...
i would like the program to return a deck which has those cards in the order i have chosen...
for instance....

1) Enter your choice of cards " Ah Kc Kd 10s 8h".
2) Now generate shuffles.
3) Return a deck minus those cards entered above...

Maybe its like backward thinking...but to me its like creating a permutation of the deck only showing the cards "WITHOUT" the ones entered in (step 1).

That should work. And "erase" them from the deck is easy.
"
ne555 (1684) Oct 7, 2011 at 1:42am
That should work. And "erase" them from the deck is easy."

Can you explain why it should work?
And also erasing cards from deck you say is easy, maybe you should complete what i need then i can stop this thread!!
Suppose that you start with the deck sorted.
2h 3h 4h 5h ...
knowing the position of a given card is just an operation then.

Now you want to "erase" Kd. With "erase" I mean put it somewhere, like the beggining or end of the deck. (a swap)
Then shuffle the rest of the deck
Well you could put it at the end of the deck but that wouldn't make sense because if i held the Kd it cant be at the end of the deck as well.....since there is only 1 Kd in the deck!!

Is using the next permutation cpp file like shuffling?
So if i can use that c++ program to say create a deck of 52 cards.
Use the next permutation program on that deck it would repeat back to the user different orders of the deck am i correct? Could an rng algorithm be used to decide those orders?
Last edited on
Topic archived. No new replies allowed.