Results 1 to 9 of 9
Thread: Filling Arrays??
- 11-04-2010, 04:36 AM #1
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
Filling Arrays??
hey guy working on a project to create a deck of cards.so far this is what i have..if anyone could help with my question of once i declared and initialized the array how do i actually fill it ..
Java Code:public class Card { String faces,suit; public Card (String f, String s) { faces =f; suit = s; } public String toString(){ return (faces +" "+ "of" +" "+ suit); } }
Java Code:import java.util.Random; public class DeckOfCards { String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }; String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; Card[] deck = new Card[52]; public void Shuffle(){ Random lal = new Random(); } }
- 11-04-2010, 04:50 AM #2
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
- 11-04-2010, 04:54 AM #3
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
Thanks
D, can you explain more to me how i would assign the random to fill the deck?reading alot and having trouble applying the knowledge and test friday..this is the review.
- 11-04-2010, 05:03 AM #4
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
i'm a noob so i'm only guessing
but you will probably need 2 for loops. one to check the card and one to check the face you then assign the .. god i dunno how to explain it..
deck[i] = faces[random something]
but you will need to loop through the cards and the suits. you will also need to check if that card and suit is already in the deck array. if so continue until it finds one that isnt. do this until all 52 cards have been shuffled.
- 11-04-2010, 05:10 AM #5
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
well i must be a noob/1,000,000..How did you get better as a programmer?
- 11-04-2010, 05:17 AM #6
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
uhh i've been studying Actionscript and PHP for a while on and off now.
i watched alot of Video tutorials. there's a series on java i'm watching on youtube (series of 87 tutorials for beginners) that has really helped with Java
then there's about 30 of advaned java
im up to about # 50
- 11-04-2010, 05:20 AM #7
Senior Member
- Join Date
- Oct 2010
- Location
- Newark,nj
- Posts
- 111
- Rep Power
- 0
and your able to just apply.??.Im pretty good syntax wise, its just translating english into code
- 11-04-2010, 05:27 AM #8
Member
- Join Date
- Nov 2010
- Posts
- 90
- Rep Power
- 0
google is my friend.
like your cards.. you're best to write dot points of what needs to happen, then think about it in PC terms.
1. i need 52 card
2. i can't repeat a card.
3. they need to be shuffled.
...
so to me i see
1. i need arrays with 52 elements (or 52 variables :P)
2.need to add if statmenets and checks to make sure they are not repeated.
3. will need a loop to go through it all.
then i work piece by piece
oh and flow charts are good showing the loops and things :)
- 11-04-2010, 06:51 AM #9
Member
- Join Date
- Oct 2010
- Posts
- 94
- Rep Power
- 0
Hi bgreen,
In addition to maknib's advice on how to solve the problem:
- add a method to the deck that populates the Deck with cards in 'normal' order using two loops (one for suits, one for faces)
- add a method to the deck that shuffles the deck in one loop with something like:
Adding the methods to the deck class makes it easy to reorder and shuffle at whim.Java Code:for (i = 0; i < 52; i++) { j = random; // needs refinement card = deck[i]; deck[i] = deck[j]; deck[j] = card; }
You could even consider to use the populate method in the constructor of your deck class so after creation of a new deck object it is immediately filled and order.
Cheers,
ErikI'm new to Java but I like to help where ever I can. :)
Similar Threads
-
problem filling list with values from CSV feed
By shy_ted in forum New To JavaReplies: 7Last Post: 10-12-2010, 10:19 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
I have a problem filling out this method
By kira137 in forum New To JavaReplies: 6Last Post: 10-11-2009, 01:43 AM -
Filling 2D Array
By Nakira in forum New To JavaReplies: 3Last Post: 11-12-2008, 12:43 PM -
Beginner Java graphics - filling concentric circles with color
By GenkiSudo in forum New To JavaReplies: 4Last Post: 09-13-2008, 11:07 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks