Results 1 to 9 of 9
  1. #1
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default 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();
    
    		
    	}
    }

  2. #2
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by Bgreen7887 View Post
    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();
    
    		
    	}
    }
    im guessing you mean the deck array?

    i believe you would use a For loop

    for(int i = 0; i < array.length; i++){
    //in here use your random to assign the card to the deck[i];
    }

  3. #3
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    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.

  4. #4
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    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.

  5. #5
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    well i must be a noob/1,000,000..How did you get better as a programmer?

  6. #6
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by Bgreen7887 View Post
    well i must be a noob/1,000,000..How did you get better as a programmer?
    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

  7. #7
    Bgreen7887 is offline Senior Member
    Join Date
    Oct 2010
    Location
    Newark,nj
    Posts
    111
    Rep Power
    0

    Default

    and your able to just apply.??.Im pretty good syntax wise, its just translating english into code

  8. #8
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    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 :)

  9. #9
    venerik is offline Member
    Join Date
    Oct 2010
    Posts
    94
    Rep Power
    0

    Default

    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:

    Java Code:
      for (i = 0; i < 52; i++) {
        j = random; // needs refinement
        card = deck[i];
        deck[i] = deck[j];
        deck[j] = card;
      }
    Adding the methods to the deck class makes it easy to reorder and shuffle at whim.

    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,
    Erik
    I'm new to Java but I like to help where ever I can. :)

Similar Threads

  1. problem filling list with values from CSV feed
    By shy_ted in forum New To Java
    Replies: 7
    Last Post: 10-12-2010, 10:19 AM
  2. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  3. I have a problem filling out this method
    By kira137 in forum New To Java
    Replies: 6
    Last Post: 10-11-2009, 01:43 AM
  4. Filling 2D Array
    By Nakira in forum New To Java
    Replies: 3
    Last Post: 11-12-2008, 12:43 PM
  5. Replies: 4
    Last Post: 09-13-2008, 11:07 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •