jeudi 8 septembre 2016

Generate and store deck of cards in '2' arrays?

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




Aucun commentaire:

Enregistrer un commentaire