- 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;
}