Results 1 to 4 of 4
- 11-12-2007, 01:08 PM #1
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
make a variable name from a string?
Hi all.
My first post... looking fo help on a school project.
I'm making a backjack game. I have a Card object that keeps the info about value, suit and card name. I want to make a Deck now with an arrayList containing all the cards I'll need. I don't want to spend so much time writing out the declaring, creating and arrayList adding for EACH card..
soo I tried to make a for each loop that would make up the names of the variable from 2 more arrayLists containing Strings for suit and card name...
umm.. I'll post some code and maybe that will explain better.. bha, it's hard for me to explain..
Thanks for any help you can give!!
Java Code:suitNames = new ArrayList<String>(); suitNames.add("spades"); suitNames.add("clubs"); suitNames.add("diamonds"); suitNames.add("hearts"); cardNames = new ArrayList<String>(); cardNames.add("Ace"); cardNames.add("King"); cardNames.add("Queen"); cardNames.add("Jack"); cardNames.add("Ten"); cardNames.add("Nine"); cardNames.add("Eight"); cardNames.add("Seven"); cardNames.add("Six"); cardNames.add("Five"); cardNames.add("Four"); cardNames.add("Three"); cardNames.add("Two"); int count = 13; int value = 11; for (String suitName : suitNames) { for (String cardName : cardNames) { switch (count) { case 13: value = 11; break; case 12: case 11: case 10: case 9: value = 10; break; case 8: value = 9; break; case 7: value = 8; break; case 6: value = 7; break; case 5: value = 6; break; case 4: value = 5; break; case 3: value = 4; break; case 2: value = 3; break; case 1: value = 2; break; default: break; } ??(suitName + cardName) = new Card(cardName, suitName, value);?? } count = 13; }Last edited by JavaBean; 11-12-2007 at 01:12 PM. Reason: Code placed inside [code] tag.
- 11-12-2007, 04:49 PM #2
You are on the right track. I think the switch statement is not needed though. This is how I would do it....
For this to work, you would have to reverse the order in which you add your cardNames to the ArrayList. The card at index zero would be a 'two' and so on.Java Code:Deck d = new Deck(); for(int i = 0; i < suitNames.size(); i++){ for(int j = 0; j < cardNames.size(); j++){ Card c = new card(cardNames.get(j), suitNames.get(i), j + 2); d.addCard(c); } }
- 11-13-2007, 06:32 AM #3
Member
- Join Date
- Nov 2007
- Posts
- 2
- Rep Power
- 0
thx for the tip Ninja!
Problem with it tho... (I forgot to mention this earlier)
i need to give each card it's own object name so I can use this included method:
Java Code:/** * Add a single card to the deck. * @param a Card object */ public void addCard(Card newCard) { deck.add(newCard); // add a card to the deck }
- 11-13-2007, 03:54 PM #4
Similar Threads
-
how to Parse int to a string variable
By raj reddy in forum Java ServletReplies: 10Last Post: 01-09-2009, 07:41 PM -
how to Parse int to a string variable (pls hlp)
By raj reddy in forum Threads and SynchronizationReplies: 5Last Post: 06-10-2008, 06:32 AM -
Help with variable assigment to String
By silvia in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:43 AM -
I can't seem to pass the value of a string variable into a string array
By mathias in forum Java AppletsReplies: 1Last Post: 08-03-2007, 10:52 AM -
String Variable
By Eric in forum Advanced JavaReplies: 1Last Post: 06-06-2007, 04:30 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks