|
-
October 12th, 2009, 08:50 AM
#1
ArrayList help
I'm making a hanafuda game (Japanese type card game).
Code:
public static void main(String[] args) {
ArrayList<Card> deck = new ArrayList<Card>();
Card janbright = new Card();
janbright.month = "January";
janbright.flower = "Pine";
deck.add(janbright);
Card janpoemscr = new Card();
janpoemscr.month = "January";
janpoemscr.flower = "Pine";
deck.add(janpoemscr);
Card janbasic1 = new Card();
janbasic1.month = "January";
janbasic1.flower = "Pine";
deck.add(janbasic1);
etc etc, this goes on for 48 cards. So then I do
Code:
Collections.shuffle(deck);
And now, I want to pass 8 cards in to player1's hand, 8 to the table, and 8 to player2's hand. All of these collections have constantly changing sizes so I need something (i guess) like 3 arraylists, one for player1, one for player2, one for the table.
So my question is - Now that I have shuffled the deck, how do i 'select' index 0 through index 7, then index 8 through 15, then 16 through 23, and pass the objects in those indexes in to the other arraylists? Because the deck has been shuffled, I know that I have those indexes, but I dont know which object is in each index.
-
October 12th, 2009, 09:34 AM
#2
Re: ArrayList help
 Originally Posted by wotsits
Because the deck has been shuffled, I know that I have those indexes, but I dont know which object is in each index.
Well, that's the whole idea with shuffling, isn't it? 
Before the shuffle you knew where each object was because you put them there. After the shuffle you don't anymore because the objects have been moved around randomly.
-
October 12th, 2009, 09:39 AM
#3
Re: ArrayList help
yeah, i get ya. the problem is that i want to say to the array, "take the first five objects that you get when you point to indexes 1,2,3,4,5,etc, or take the first 5 cards, and pass them in to the player1 array" but i dont know how?
-
October 12th, 2009, 10:36 AM
#4
Re: ArrayList help
 Originally Posted by wotsits
yeah, i get ya. the problem is that i want to say to the array, "take the first five objects that you get when you point to indexes 1,2,3,4,5,etc, or take the first 5 cards, and pass them in to the player1 array" but i dont know how?
You added 48 Card objects to the ArrayList. This means it's now an array with 48 index positions (going from 0 thru 47). You can get each one using the get method of ArrayList, like
Card c0 = deck.get(0);
Card c42 = deck.get(42);
Card c48 = deck.get(48); // error - out of bounds exception
This won't remove any objects though, just read them.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|