Welcome to Dream.In.Code
Become a Java Expert!

Join 150,199 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,993 people online right now. Registration is fast and FREE... Join Now!




ArrayList issue.

 
Reply to this topicStart new topic

ArrayList issue.

injakari
24 Sep, 2008 - 12:51 PM
Post #1

New D.I.C Head
*

Joined: 23 Sep, 2008
Posts: 7

Okay. I have two classes, Card, and DeckOfCards. My Card class constructs a card and such, then my DeckOfCards class constructs an ArrayList of the deck. In my Card class, my variables are char values for my cards representing (A,2,3,4,5,6,7,8,9,T,J,Q,K) and String suits (obvious). Problem is my ArrayList in DeckOfCards isn't recognizing values or suits. I don't think I have to redeclare them...here's what I got.

CODE
import java.util.*;

public class DeckOfCards
{
    ArrayList<Card> deck = new ArrayList<Card>(); // an ArrayList to hold card objects

    public DeckOfCards() // default constructor – creates an empty deck
    {
        deck = new ArrayList<Card>();
    }
    public void resetDeck() // puts 52 cards into the deck and shuffles them
    {
        for (int i = 0; i < values.length; i++)
        {
            for (int j = 0; j < suits.length; j++)
            {
                Card card = new Card(values[i], suits[j]);
                deck.add(card);
            }// Not sure about this...
            // I guess implement shuffle()?
        }
    }
}

Any ideas?
User is offlineProfile CardPM
+Quote Post

pbl
RE: ArrayList Issue.
24 Sep, 2008 - 02:02 PM
Post #2

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
We don't see your Card class
But it is defined as:

CODE

class Card {
    String suit;
    char value;

    // constructor
    Card(char value, String suit) {
        this.value = value;
        this.suit = suit;
    }
}


No reason for your program not to work
User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: ArrayList Issue.
24 Sep, 2008 - 03:17 PM
Post #3

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
pbl are you sure?

CODE

public void resetDeck() // puts 52 cards into the deck and shuffles them
    {
        for (int i = 0; i < values.length; i++)
        {
            for (int j = 0; j < suits.length; j++)
            {


in the above code i don't see how values and suits have been defined or initialized in this method
User is offlineProfile CardPM
+Quote Post

Mach1Guy
RE: ArrayList Issue.
24 Sep, 2008 - 03:53 PM
Post #4

D.I.C Head
Group Icon

Joined: 4 Dec, 2006
Posts: 79



Thanked: 4 times
Dream Kudos: 25
My Contributions
could you post your Card class
User is offlineProfile CardPM
+Quote Post

pbl
RE: ArrayList Issue.
24 Sep, 2008 - 05:08 PM
Post #5

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(Mach1Guy @ 24 Sep, 2008 - 04:17 PM) *

pbl are you sure?

CODE

public void resetDeck() // puts 52 cards into the deck and shuffles them
    {
        for (int i = 0; i < values.length; i++)
        {
            for (int j = 0; j < suits.length; j++)
            {


in the above code i don't see how values and suits have been defined or initialized in this method


He said

QUOTE

my variables are char values for my cards representing (A,2,3,4,5,6,7,8,9,T,J,Q,K) and String suits (obvious).


I assumed they were defined somewhere and he didn't want to post all the code.

The error was not a calling the constructor of Card but had to do with the ArrayList..

Better to post ALL your code



User is offlineProfile CardPM
+Quote Post

injakari
RE: ArrayList Issue.
25 Sep, 2008 - 07:01 AM
Post #6

New D.I.C Head
*

Joined: 23 Sep, 2008
Posts: 7

Heh, well I was writing it wrong, but I figured it out. The way I had it needed for the variables to be declared in the method. Got it to work another way. But I do have another question dealing with this program. In my Card class I want to write a compareTo method to compare two cards but I'm not really sure how it should be written. I know I need it to compare two values and return negative or positive showing lower or higher. And if they are both the same compare by suits. A<2<...<Q<K, and Clubs<Diamonds<Spades<Hearts.

CODE
    String[] suits = {"Clubs", "Diamonds", "Spades", "Hearts"};
        char[] values = {'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'};
        
        this.number = number;
        this.suit = suits[(number/13)%4];
        this.value = values[number%13];


I think it might deal with the mod division to determine it's number? I dunno. Any guidence?
User is offlineProfile CardPM
+Quote Post

pbl
RE: ArrayList Issue.
25 Sep, 2008 - 02:22 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(injakari @ 25 Sep, 2008 - 08:01 AM) *

Heh, well I was writing it wrong, but I figured it out. The way I had it needed for the variables to be declared in the method. Got it to work another way. But I do have another question dealing with this program. In my Card class I want to write a compareTo method to compare two cards but I'm not really sure how it should be written. I know I need it to compare two values and return negative or positive showing lower or higher. And if they are both the same compare by suits. A<2<...<Q<K, and Clubs<Diamonds<Spades<Hearts.

CODE
    String[] suits = {"Clubs", "Diamonds", "Spades", "Hearts"};
        char[] values = {'A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'};
        
        this.number = number;
        this.suit = suits[(number/13)%4];
        this.value = values[number%13];


I think it might deal with the mod division to determine it's number? I dunno. Any guidence?


You will need more than that. You have to evaluate if you have a flush where the As can be 1 or 14...
Actually you have to compare to hands not 2 cards.

6 years ago, I was then a little be more a beginner in Java, I wrote a Carribean poker game. As I say, I was a beginnein Java. I would probably today recode the same thing in a different way that is for sure. For example I would use an ArrayList to shuffle the cards. Anyhow... the game is at

www.pblinc.ca/poker

and you can download the source from there. So you can have a look at my method that evaluates hands.

Sorry but the comments are probably in French. Variables names are probably in English.

The game is an Applet and is available at
User is offlineProfile CardPM
+Quote Post

injakari
RE: ArrayList Issue.
25 Sep, 2008 - 09:05 PM
Post #8

New D.I.C Head
*

Joined: 23 Sep, 2008
Posts: 7

Well, it's not comparing a hand but just single cards. And the Ace counts only as one. I'm gonna use this base to write a program of a card game my girlfriend and I came up with so she can play it if there aren't three other people, lol.
User is offlineProfile CardPM
+Quote Post

pbl
RE: ArrayList Issue.
26 Sep, 2008 - 12:55 PM
Post #9

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
QUOTE(injakari @ 25 Sep, 2008 - 10:05 PM) *

Well, it's not comparing a hand but just single cards. And the Ace counts only as one. I'm gonna use this base to write a program of a card game my girlfriend and I came up with so she can play it if there aren't three other people, lol.

Did you play with it ?
You'll see that it compares hand
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:55AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month