Reputation: 2240
(Forewarning - I know there are questions similar to this but I believe mine is different as I wish to store the suits and ranks in different arrays. If the community feels otherwise, then I will happily remove the question.)
I am trying to generate a deck of cards randomly (aka shuffled). In the function generateCard I am generating a single rank and a single suit. So it generates one single card essentially. I haven't included any returns yet because I don't know how to return two different values back especially as they are of different data types. I really want to do it this way and so if answers could resist the urge to suggest more efficient or standard ways of doing this I would appreciate it. I am a beginner and understanding how I can make things that don't work, work, really helps me.
So in summary. My question is, how do I firstly return two items of different data types? And then secondly, collect the returns separately and store them in two different arrays (deckSuitArray & deckRankArray)?
Here is the code I have:
package texasHoldem;
import java.util.Random;
public class SingleRound{
public static void main(String[] args) {
char[] deckSuitArray = new char[51];
int[] deckRankArray = new int[51];
for(int i = 0; i < 53; i++){
generateCard();
//wish to cycle though arrays storing random cards at different positions
}
}
public static void generateCard(){ //will remove void
Random ran = new Random();
char suit = '0';
int randomRank = ran.nextInt(13)+1;
System.out.println(randomRank);
int randomSuit = ran.nextInt(4)+1;
switch (randomSuit){
case 1: suit = 'C'; break;
case 2: suit = 'S'; break;
case 3: suit = 'D'; break;
case 4: suit = 'H'; break;
}
System.out.println(suit);
}
}
Upvotes: 1
Views: 1457
Reputation: 15842
Your Card should be a class. Card.java
should look like the following. Add equals method.
enum Suit {HEART, DIAMONDS, SPADES, CLUBS}
enum Rank {TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE}
public class Card {
Rank rank;
Suit suit;
public Card (Rank r, Suit s) {rank = r; suit = s;} //public constructor
@Override
public String toString() {
return rank.toString() + " " + suit.toString();
}
}
Then you can make a Deck.java
public class Deck {
private List<Card> cards;
public Deck() {
cards = new ArrayList<>();
for (Suit s : Suit.values()) {
for (Rank r : Rank.values()) {
cards.add(new Card(r, s));
}
}
}
public void shuffle() {
Collections.shuffle(cards);
}
}
Added shuffling to the Deck class. This shuffle is a very good shuffle. It efficiently randomizes the order of cards.
Upvotes: 5
Reputation: 7347
Java can only return one thing, so that option is gone. As you already said you don´t want the standard way but keep the two arrays.
So there would be another option, just pass the arrays to the method and use them in the method and store the stuff there, instead of returning something.
...
for (int i = 0; i < 53; i++) {
generateCard(deckSuitArray, deckRankArray);
// wish to cycle though arrays storing random cards at different
// positions
}
...
generateCard(char[] deckSuitArray, int[] deckRankArray) {
...
deckSuitArray[someRandomIndex] = suit;
deckRankArray[someRandomIndex] = randomRank;
...
}
as another sidenote, format your code properly. It will be much easier to read if you do it.
Upvotes: 0
Reputation: 3453
Java has no implicit tuples.
However there are several ways to return " two items of different data types"
Return arguments. Pass two objects and fill them inside method.
void f(T1 t1, T2 t2) {
t1 = ...
t2 = ...
}
Upvotes: 0