• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Random Cards -- Need code converted to Java, any help?

X-Istence

*
Political User
#1
I have the following C++ code, and i need it in Java as well. Can anyone point me towards a good way of making this into Java?

Code:
#include <iostream>
#include <vector>
#include <string>

int rndnmr (int high, int low) {
        return int ( rand() / (RAND_MAX + 1.0) * (high-low+1) + low );
}

class Cards {
        public: 
                int delt;
                Cards(int gdelt, std::string gtype, std::string gname) : delt(gdelt), type(gtype), name(gname) {}
                int output() {
                        std::cout << "Type: " << this->type << "\tName: " << this->name << "\n";
                        return 0;
                }
                
        private:
                std::string type; // This can be either: Spades, Hearts, Diomand, Club
                std::string name;
};

int main(int argc, char *argv[]) {

        srand( time(0) );
        srand( rand() + time(0) );
        
        std::vector<Cards> Deck;
        
        std::vector<std::string> type;
        
        type.reserve(4);
        type.push_back("Spade");
        type.push_back("Heart");
        type.push_back("Diamond");
        type.push_back("Club");
        
        std::vector<std::string> name;
        
        name.reserve(14);
        name.push_back("Ace");
        name.push_back("1");
        name.push_back("2");
        name.push_back("3");
        name.push_back("4");
        name.push_back("5");
        name.push_back("6");
        name.push_back("7");
        name.push_back("8");
        name.push_back("9");
        name.push_back("Jack");
        name.push_back("Queen");
        name.push_back("King");

        Deck.reserve(54);
        for (std::vector<std::string>::iterator a = type.begin(), b = type.end(); a!=b; ++a)
                for (std::vector<std::string>::iterator i = name.begin(), e = name.end(); i!=e; ++i)
                        Deck.push_back( Cards(0, (*a), (*i)) );
        
        Deck.push_back( Cards(0, "Joker #1", "Joker #1") );
        Deck.push_back( Cards(0, "Joker #2", "Joker #2") );
        
        int high = Deck.size() - 1;
        int low  = 0;
        
                       
        for (int x = rndnmr(high, low), cnt = 0; cnt < 20; x = rndnmr(high, low)) {
                if ( (Deck.at(x)).delt == 0 ) {
                        (Deck.at(x)).output();
                        (Deck.at(x)).delt = 1;
                        cnt++;
                }
                else {
                        continue;
                }
        }
        
        return 0;
}
 

X-Istence

*
Political User
#5
it basically creates several arrays. The first one creates the different faces that can exist. (Hearts, Diamonds, Clubs, and the other one), the other one is an array of all the different cards that can exist under the different faces.

It then generates the entire array which holds all the 54 cards. Then it randomly picks 20 of em.

second, it was C++, as it was using Classes.
 

Members online

No members online now.

Latest posts

Latest profile posts

Hello, is there anybody in there? Just nod if you can hear me ...
Xie
What a long strange trip it's been. =)

Forum statistics

Threads
61,961
Messages
673,239
Members
89,014
Latest member
sanoravies