changing values..

Is there a way i could change this main function to allow for card values instead of numbers? So that when shuffling the deck it returns a random deck with those cards values in the order i type them at runtime, eliminating the "enter how many cards to deal" to ""enter cards e.g:2s,6d etc" then shuffle,then return deck. Its purposely for random scenarios. I have my main program below if someone could point me in the right direction id be over da moon :)

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
// main.cpp 
/***************************************************************************************/
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#include "card.h"
#include "deck.h"

int main() 

{ 
    int numOfCards;   

    Deck deck;

    while (cin >> numOfCards ) 
	{
		 
		
        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();
}


First how are you representing your card ? A number for value and a number for suit ? If yes, then you need a mapping from the user input of S (Spade), H (Heart), C (Club), D (Diamond) to the equivalent suit number.
im sorry but what do you mean by mapping from the user? :)
this is the rest of program if it helps:

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
#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 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 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 


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
// cardplay-1/deck.cpp


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

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 = genrand_int32();  // 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( unsigned ( time (NULL) ) );        
	
    // Shuffle by exchanging each element randomly  
    for (int i=0; i<(52); i++)
	{
        int randnum = i + (genrand_int32() % (52-i));  
        swap(_cards[i], _cards[randnum]);
    }
 
 _nextCardIndex = 0;
}
Last edited on
eliminating the "enter how many cards to deal" to ""enter cards e.g:2s,6d etc"


Actually I don't quite get what you want. How about some example based on above?

E.g
if user enter 2s,6d

what should the program do?
Topic archived. No new replies allowed.