notfreshprince
notfreshprince

Reputation: 117

How to fill a 2D array with their appropriate row and column numbers?

Java beginner here, been stuck on 2D arrays for some time now.

So I want to create a 2D array to represent a deck of cards, the array size is to be [4,13]: 4 suits, 13 possible values within each suit.

I've got this:

private int[][] deckIndex = new int[4][13];

public void populate deck()
{}

But I'm not sure how to properly populate the 2D array with each value.

Thanks!

Upvotes: 0

Views: 1984

Answers (4)

blalasaadri
blalasaadri

Reputation: 6198

First of all, you'll have to decide what exactly you want to have in the array. Numbers for the numbered cards, OK, but what about the faces? One option would be to just have the numbers 0 through 13. In that case you could simply use two nested for loops. That would look something like this:

for(int suit=0; suit<cards.length; suit++) {
    for(int card=0; card<cards[0].length; card++) {
        cards[suit][card] = card;
    } 
} 

A more object oriented way would however be too have a Card class which holds two enums - one for the suit and one for the value. That could look something like the following:

public class Card {
    private final SuitEnum suit;
    private final ValueEnum value;

    public Card(SuitEnum suit, ValueEnum value) {
        this.suit = suit;
        this.value = value;
    } 

    // getters
} 

public enum SuitEnum {
    SPADES, HEARTS, DIAMONDS, CLUBS
} 

public enum ValueEnum {
    ACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING
} 

With that you could do something like this:

List<List<Card>> cards = new ArrayList<List<Card>>();
for(SuitEnum suit : SuitEnum.values()) {
    List<Card> innerList = new ArrayList<Card>();
    for(ValueEnum value : ValueEnum.values()) {
        innerList.add(new Card(suit, value));
    } 
    cards.add(innerList);
} 

Notice that I switched to 2D lists here. Much more flexible and in many cases to be preferred over arrays.

Upvotes: 0

singhakash
singhakash

Reputation: 7919

You can do

private int[][] deckIndex = new int[4][13];

public void deck() {
    int count = 1;
    for (int i = 0; i < deckIndex.length; i++) {
        for (int j = 0; j < deckIndex[i].length; j++)
        deckIndex[i][j] = count++;
     count=1;
    }


}

By this approach each row is considered to be each color set.For ex

deckIndex[0] represents all the clubs
deckIndex[1] represents all the diamond
deckIndex[2] represents all the spade
deckIndex[3] represents all the heart

Upvotes: 0

aman rastogi
aman rastogi

Reputation: 186

You can do following : 
for(int i = 0; i < a.length; i++){
    for(int j = 0; j < a[0].length; j++){
        a[i][j] = j+1;
    }
 }

Upvotes: 0

user4668606
user4668606

Reputation:

public void populateDeck(){ //NOTE: spaces are not allowed names 
    for(int i = 0 ; i < deckIndex.length ; i++)
        for(int j = 0 ; j < deckIndex[i].length ; j++)
            deckIndex[i][j] = i * 13 + j;
}

public int suitNo(int card){
    return card / 13;
}

public int value(int card){
    return card % 13;
}

Though i would recommend a more oop-way of representing the cards than Integervalues.

Upvotes: 1

Related Questions