| Author |
create a hand of cards
|
Daniel Cipriano
Greenhorn
Joined: Feb 02, 2011
Posts: 14
|
|
HI GUYS
i have a method that generate 5 cards, so my doubt is how i can pass the five cards to a list immediately, like an arrayList with 5 elements
there is possible? how?
thanks in advice is my first post ehehe
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
Welcome to the ranch ! Next time, please don't write your subject all in capital letters. (check this FAQ)
A few comments about your code :
1. You are using instance variables (rand, nameF...) in your method. It makes it difficult to follow, and is prone to undesirable side effects.
2. You are using a while loop with a return statement. I guess you wanted to use an "if" instead.
3. You can lighten your code a bit by using a local variable "Card card = deckOfCards.get(rand);" and use that variable afterwards.
So... You want something like this ?
1. Make a new list. For example, List<Card> hand = new ArrayList<Card>();
2. Make a loop with a counter going up to 6
3. For each iteration:
3.1. Get a card : Card card = pickRandomCard(); (make sure the card is not already picked)
3.2. Add the card the the list : hand.add(card);
Can you figure out the code from that algorithm ?
|
[My Blog]
All roads lead to JavaRanch
|
 |
Daniel Cipriano
Greenhorn
Joined: Feb 02, 2011
Posts: 14
|
|
hi Christophe, first of all, thanks
i have some questions
you suggest that i can change this "Card card = deckOfCards.get(rand);, but if i did, how i can do the incrementation of rand++?
another question, i try a loop for create an arrayList but if i did something like the fallowing script i receive The operator + is undefined for the argument type(s) Card, int, so, there is another alternative?
thanks for your help
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
Try to tell in plain English what you are doing here : "Pick a card randomly, then add a counter to it, then add 1 to it". Does it make sense to add a counter to a card ? No. You have to increment the counter after adding a card. You could use a for loop instead of a while:
you suggest that i can change this "Card card = deckOfCards.get(rand);, but if i did, how i can do the incrementation of rand++?
Why do you think that you need to put an incrementation in a method call ? You can simply call "rand++;" in the end. (Well, you shouldn't need that variable anymore.)
|
 |
Daniel Cipriano
Greenhorn
Joined: Feb 02, 2011
Posts: 14
|
|
yep, problem solved, one more question please, the last one
now i have four lists, one for each player (hand), the big problem is now, i need to compare each list to the suit card moved to the first player. All players must assist to the suit, and the bluff is not permit
so, i need create while loops and check the four array List, or exist a best solution?
do you understand what i try explain?
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
do you understand what i try explain?
No. I'm not a gambler I don't understand the "All players must assist to the suit, and the bluff is not permit " part.
|
 |
Daniel Cipriano
Greenhorn
Joined: Feb 02, 2011
Posts: 14
|
|
King game
first rule
1st hand- No tricks – The aim is not to win tricks. The dealer plays any card and all the other players must follow that suit unless they do not hold any card of that suit. The winner of the trick is the highest card of the suit played at the start of the trick or the highest trump, if any was declared for that hand.
thanks for your patience
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
|
A Player holds a set of Cards. It shouldn't be difficult to check for each Player if, according to your rule, he has a card he can throw. A method like "Card throwCard(Card cardToMatch)". All you'd have to do is loop through the cards that the player is holding.
|
 |
Daniel Cipriano
Greenhorn
Joined: Feb 02, 2011
Posts: 14
|
|
hi Christophe
i tried a method to making what you suggest, but i have some problems with the implementation
i have the code above, no error, but when i call the method validCard i receive the error " card cannot be resolved to a variable", and the same for the others arguments
so, my question is, how i can pass the arguments, there is any solution for that?
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
Hi Daniel,
First of all, a general rule : your methods should be self explanatory. By this, I mean that I should understand what the validCard method does. But I can't It validates a card, ok. But what about the "i" parameter ? What is it ? What is the meaning of "i-10" ? What about the suit() method ? What is its purpose ? Does it return an int ? Cleaning the code a bit will make things easier for you afterward.
About your compiler problem, you are trying to access some variables from an inner class. I need to know where that code is, and where the variables it's trying to access are. Is it the ActionListener of a button ? If yes, I can't understand where the "card" comes from.
|
 |
Daniel Cipriano
Greenhorn
Joined: Feb 02, 2011
Posts: 14
|
|
i did a same changes in the script, it is almost done i think
i have four blocks of code (one for each player) like that
at the moment if the player assist, the sysout is the correct, but if he did bluff sometimes the message is you must assist but another none message is showed. Another problem is that i need much methods because of this line if(pn70.getText().equals (pn50.getText())) {, if the first player to assist is the third this line must be update where is pnXX
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
|
It's difficult to understand what you are aiming at. Once thing about the validCard method : why are you passing two hands of cards ? Also, I think that you should avoid passing Swing components like that. Pass a String instead.
|
 |
Daniel Cipriano
Greenhorn
Joined: Feb 02, 2011
Posts: 14
|
|
hi Christophe
now i can do what i want, but another problem again
with the fallowing method i can verify if the player two - pn70.getText() assists or no to the player one JTextField pn50, but as you said it is much better compare strings, and my objective is a generic method, to avoid replicate the method N times, for third player, four player,....
and four block like that in another class
if you can help me, i really appreciate, thanks
|
 |
Christophe Verré
Sheriff
Joined: Nov 24, 2005
Posts: 14670
|
|
One step at a time You could change your method to :
and call it like :
|
 |
 |
|
|
subject: create a hand of cards
|
|
|