Results 1 to 13 of 13
Thread: [SOLVED] Stuck in need of help!
- 09-23-2008, 03:11 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
[SOLVED] Stuck in need of help!
I'm trying to write a program using two seperate classes to make a deck of cards. One will create the cards assigning a number 0-51 for each card along with a value (Ace-King) and a suit. Then the other class will construct an arraylist that creates the deck. I'm not sure how to go about doing this...I can use the system.out.print to make the deck, or I think maybe with arrays. But I don't know how to use arraylists. :confused:
Thanks for any help!
-
I'd give Google a try looking for an ArrayList tutorial. Have a look here as some of these links look promising. Then why not come back if you have any questions after reading this?
java arraylist tutorial collections - Google Search
- 09-23-2008, 03:38 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
Okay, I see it's like using an array but you can continue adding to it or remove items. Which is really cool! But I'm not seeing how I would construct the card in one class then use that to make the arraylist in another. I could prolly do it in the same class (which I did with an array) but I guess the seperate classes is throwing me off... here's my code for my array of the deck.
public class Deck
{
public static void main(String[] args)
{
String[] suit = { "Clubs", "Diamonds", "Spades", "Hearts" };
String[] value = { "A", "2", "3", "4", "5", "6", "7", "8", "9",
"T", "J", "Q", "K" };
int SUIT = suit.length;
int VALUE = value.length;
int D = VALUE * SUIT;
// initialize deck
ArrayList deck = new String[D];
for (int i = 0; i < SUIT; i++)
{
for (int j = 0; j < VALUE; j++)
{
deck[VALUE*i + j] = value[j] + " of " + suit[i];
}
}
// print deck
for (int i = 0; i < D; i++)
{
System.out.println(deck[i]);
}
}
}
- 09-23-2008, 03:45 AM #4
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
Sorry, this is the right code, I'm trying to make an arraylist.
public class Deck
{
public static void main(String[] args)
{
String[] suit = { "Clubs", "Diamonds", "Spades", "Hearts" };
String[] value = { "A", "2", "3", "4", "5", "6", "7", "8", "9",
"T", "J", "Q", "K" };
int SUIT = suit.length;
int VALUE = value.length;
int D = VALUE * SUIT;
// initialize deck
String[] deck= new String[D];
for (int i = 0; i < SUIT; i++)
{
for (int j = 0; j < VALUE; j++)
{
deck[VALUE*i + j] = value[j] + " of " + suit[i];
}
}
// print deck
for (int i = 0; i < D; i++)
{
System.out.println(deck[i]);
}
}
}
- Sorry, this is the right code, I'm trying to make an arraylist.
- 09-23-2008, 03:55 AM #6
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
I know, I'm trying to write an arraylist now using a tutorial. What I'm asking is how I would define an object in one class then use that to create an arraylist in a seperate class. Like would I need to import the class I wrote into it or is there some other way? (Sorry if I'm not making sense...)
-
You simply create a Card class, then in your Deck class, have an ArrayList<Card> and start adding Cards to this ArrayList.
- 09-23-2008, 04:02 AM #8Java Code:
static void cardList(int numHands,int cardsPerHand){ List<String> deck,hand; //52-card deck Srting[] suit=new String[]{"S","H","D","C"}; String[] rank=new String[]{"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; deck=new ArrayList<String>(); for(int i=0;i<deck.length;i++){ for(int j=i;j<deck.length;j++){ deck.add(rank[i]+"-"+suit[j]); } } Collections.shuffle(deck);//shuffles the heap of the crads //dealing the cards for(int i=0;i<numHands;i++){ hand=deck.subList(deck.size()-cardsPerHand,deck.size()); System.out.println(hand); //hand.clear();//removes the sub-list from the list } }
-
- 09-23-2008, 04:06 AM #10
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
Heh, I don't believe that code means anything to my program.
- 09-23-2008, 04:08 AM #11
shitttttttt,i was hurrying
LOL
Java Code:public void cardList(int numHands,int cardsPerHand){
-
Good. Keep studying and learning. We'll help you as we can, but you'll gain much more by attempting as much as you can on your own.
- 09-23-2008, 04:18 AM #13
Member
- Join Date
- Sep 2008
- Posts
- 21
- Rep Power
- 0
Okay I got this. Now if I had a Card class, think this would work?
import java.util.ArrayList;
public class DeckOfCards
{
ArrayList<Card> deck = new ArrayList();
Card cards[] = new Card[52];
for(int i = 0; i < 4; i++)
{
for(int j = 0; j < 13; j++)
{
cards[i * 13 + j] = new Card(j + 1, i + 1);
deck.add(cards[i * 13 + j]);
}
}
}
Similar Threads
-
stuck on an assignment
By starchildren3317 in forum New To JavaReplies: 11Last Post: 11-20-2008, 12:03 AM -
Stuck in need of help!
By Zombie_Leg! in forum New To JavaReplies: 1Last Post: 09-23-2008, 03:22 AM -
Stuck on Two Questions, Please Help
By sylo18 in forum New To JavaReplies: 5Last Post: 03-11-2008, 02:03 AM -
musically stuck cry for help 2
By geork in forum New To JavaReplies: 0Last Post: 02-07-2008, 03:09 PM -
musically stuck
By geork in forum New To JavaReplies: 1Last Post: 02-06-2008, 10:44 PM
Bookmarks