1

I've fixed it exactly how I want it now. If anyone else has the same issue I think this is the easiest and most efficient way of trying to set up a deck of cards. You can pick out individual cards using a random variable in deck[random][0] and deck[random][1]. Thanks for all the help, here is my code:

public class NewDeck {

    public static void main(String[] args) {

        String[] suits = new String[] { "Clubs", "Diamonds", "Spades", "Hearts" };

        String[] faces = new String[] { "Ace", "King", "Queen", "Jack" };

        String[][] deck = new String[suits.length * (faces.length + 9)][2];

        int a = 0;

        for (String y : suits) {

            for (String x : faces) {

                deck[a][0] = x;
                deck[a][1] = y;
                a++;

            }

        }

        for (String y : suits) {

            for (int p = 2; p < 11; p++) {

                deck[a][1] = y;
                String pp = Integer.toString(p);
                deck[a][0] = pp;
                a++;

            }

        }

        for (int p = 0; p < deck.length; p++) {

            System.out.print(deck[p][0] + " of ");
            System.out.println(deck[p][1]);

        }

    }

}
3
  • it will give you a compilaton error - deck.add(suits[suit], faces[face]); , deck is not a map instance. Commented Jul 31, 2013 at 10:46
  • Why don't you create a Class Card which has two properties "Suit" and "Face"? This is what Java is all about! Commented Jul 31, 2013 at 10:50
  • @ tech-idiot: I get the error on my for loop saying: 'length cannot be resolved or is not a field' And an error on - deck.add(suits[suit], faces[face]); - saying 'The type of expression must be an Array but is resolved to a list' Commented Jul 31, 2013 at 10:51

3 Answers 3

2

You should add parameter types to your strings and create a Pair class. Note that you will need a Java compiler of version 1.5 or higher for the generics.

class Pair {

     private final String face;  
     private final String suit;  

     Pair(String suit, String face) {
         this.face = face; 
         this.suit = suit;             
     }
     @Override
     public String toString() {
        return "(" + suit + ", " + face + ")";
     }
}

Then you can use this Pair class as follows, using the appropriate List methods get and size:

List<Pair> deck = new ArrayList<Pair>();

List<String> suits = new ArrayList<String>();
suits.add("Hearts");
suits.add("Diamonds");
suits.add("Clubs");
suits.add("Spades");

List<String> faces = new ArrayList<String>();
faces.add("Ace");
faces.add("King");
faces.add("Queen");
faces.add("Jack");

for(int suit = 0; suit < suits.size(); suit++){

    for(int face = 0; face < faces.size(); face++){

        deck.add(new Pair(suits.get(suit), faces.get(face)));             

    }

}

If you override the toString method of Pair you can also System.out.println(deck) to get your desired string representation of the ArrayList.

Sign up to request clarification or add additional context in comments.

2 Comments

Worked perfectly! But I'm afraid I don't understand all your code fully, so I think I may need to learn a bit more java before moving onto making games that I'm not capable of doing at the moment. But with what you've given me, I might be able to carry on making the game, I think I can only learn by trying things I haven't done before. Thanks a million.
Since generics were introduced only in java 1.5 there are still many tutorials and books for Java Collections out there which are out of date. I think an okay place to start may be here (short and contains furhter links) en.wikipedia.org/wiki/Generics_in_Java. Perhaps find a good up-to-date Java book?
0

Probably you need to change this

        deck.add(suits[suit], faces[face]);             

with

        deck.add(suits[suit] + faces[face]);             

as your idea is to concatenate elements from the list and add it the deck list.

Comments

0

You can not store two values at one index of List. But what you can do is

Option 1
Store concatenated \string like

Change

   deck.add(suits[suit], faces[face]);   

with

   deck.add(suits[suit] + "," + faces[face]);  

then output deck[2] = suit, face

Option 2 Change list of string to list of Object and this object contains detail of suit and face

public class Deck {
    private String suit;
    private String face;

// getter and setters;
}

and list become

List<Deck> deck

and add entry as

deck.add(new Deck(suits[suit], faces[face]))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.