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!
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()? } } }
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
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..
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.
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.
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.
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.
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