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

Discussion in 'Web Design & Coding' started by X-Istence, Sep 23, 2004.

  1. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    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;
    }
     
  2. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    YOU asking a question about coding? :blink:
     
  3. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    Java is a confusing a pain.
     
  4. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    I dont really understand the C code enough :p
     
  5. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    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.
     
  6. Dan04

    Dan04 OSNN Addict

    Messages:
    158
  7. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
    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