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

X-Istence

*
Political Access
Joined
5 Dec 2001
Messages
6,498
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;
}
 
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.
 
are you going to be frequently accessing items in the list/set/map or need rapid iterative access?

I would probably try the HashSet or something like that

Java Sets
 

Members online

No members online now.

Latest profile posts

Also Hi EP and people. I found this place again while looking through a oooollllllldddd backup. I have filled over 10TB and was looking at my collection of antiques. Any bids on the 500Mhz Win 95 fix?
Any of the SP crew still out there?
Xie wrote on Electronic Punk's profile.
Impressed you have kept this alive this long EP! So many sites have come and gone. :(

Just did some crude math and I apparently joined almost 18yrs ago, how is that possible???
hello peeps... is been some time since i last came here.
Electronic Punk wrote on Sazar's profile.
Rest in peace my friend, been trying to find you and finally did in the worst way imaginable.

Forum statistics

Threads
62,015
Messages
673,494
Members
5,621
Latest member
naeemsafi
Back