Mitchell Fudge
Mitchell Fudge

Reputation: 1

I need a way to store a deck of cards into an array without hard coding them

The real problem is trying to create and store the Card objects in my array. If i just needed to store strings or doubles for example i would be able to figure it out. Im pretty new at this and this is my first stack overflow question so bear with me. It might be worth noting i will eventually need to randomly generate random ints to pass through my card objects to get random cards.

This is my card class,

public class Card
{   
    private int value;
    private int suit;
    private String valueN;    
    private String valueS;
    Card(int value, int suit)
{
        this.value = value;
        this.suit = suit;
}   
    public String valueNumber()
{    
    switch(value)
{
      case 1:
              valueN = "Ace";
              break;
      case 2:
              valueN = "Two";
              break;
      case 3:
              valueN = "Three";
              break;
      case 4:
              valueN = "Four";
              break;
      case 5:
              valueN = "Five";
              break;
      case 6:
              valueN = "Six";
              break;
      case 7:
              valueN = "Seven";
              break;
      case 8:
              valueN = "Eight";
              break;
      case 9: 
              valueN = "Nine";
              break;
      case 10:
              valueN = "Ten"; 
              break;
      case 11:
              valueN = "Jack";
              break;
      case 12:
              valueN = "Queen";
              break;
      case 13:
              valueN = "King"; 
              break;
}      
        return valueN;
    }
    public String suitNumber()
{
    switch(suit)
{    
      case 1: 
              valueS = "Hearts";
              break;
      case 2:
              valueS = "Clubs";
              break;
      case 3:       
              valueS = "Spades";
              break;        
      case 4:        
              valueS = "Diamonds";
}                    
        return valueS;
}  
    public String getValueN() 
{
    return this.valueN;
}
   public String getSuitS()
{
    return this.valueS;
}
  public int getValue()
{
    return this.value;
}
  public int getSuit()
{
    return this.suit;
}
   public String toString() 
{
        return valueNumber() + " of " + suitNumber();       
}
}

Do i need to use those accessor methods? Ive been trying to solve this for an embarrassing amount of time and still dont know if i should use a regular array or a 2d array or something new altogether.

I should mention this is for a school assignment and i dont know if stack overflow has anything asking questions pertaining to assignments but i assure you this is a very small part of this assignment.

This is my deck class and my best attempt at creating the loop and array i need but it gets out of bounds exception error and is probably wrong.

public class Deck
{
    Deck ()
    {
        int index = 0;
        Card deck [] = new Card [51]; 
            
            for (int value = 1; value <= 13; value++)
            {
                for (int suit = 0; suit <= 3; suit++)
                {
                  deck[index] = new Card (value, suit);
                  index++;
                }           
            }

    
    }   
}   

I feel as though i need to make use and perhaps modify my accessor methods from my card class in my Deck constructor.

Upvotes: 0

Views: 170

Answers (0)

Related Questions