Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post

    Filling a Finite Array

    Hello all,
    I am creating a game whereby two players are dealt 5 cards at random from a pack and have to take turns flipping them over; guessing whether or not the next card will be higher or lower.

    I have created the following code:

    Code:
    import java.util.ArrayList;
    import java.util.Random;
    
    public class PackOfCards
    {
        private ArrayList<Card> cards;
        private Random rnd;
    
        /**
         * Constructor for objects of class PackOfCards
         */
        public PackOfCards()
        {
            // initialise instance variables
            cards = new ArrayList<Card>();
            rnd = new Random();
            fillPack();
        }
        
        private void fillPack() {
    	
            for (int i=0; i<52; i++) {
                cards.add(new Card(i));
            }
        }
    
        /**
         * Select a card at random from the pack
         * 
         * @return     the card that was selected, or null if the pack is empty
         */
        public Card selectCard()
        {
            if (cards.size() > 0) {
                return cards.remove(rnd.nextInt(cards.size()));
            } else {
                return null;
            }
        }
    }
    and

    Code:
    public class Player
    {
        private String name;
        private Card[] card = new Card[5];
        private int score = 0;
        
        /**
         * Contructor for objects of class Player.
         * @param A String which initialises the name of the player.
         */
        public Player(String name)
        {
            this.name = name;
        }
        
        /**
         * Gets the score of the player.
         * @return An integer value representing the score of the player.
         */
        public int getScore()
        {
            return score;
        }
        
        /**
         * Gets the name of the player.
         * @return A String representing the name of the player.
         */
        public String getName()
        {
            return name;
        }
        
        /**
         * Gives the player 5 cards from the pack.
         */
        public void fill()
        {
            int i = 0;
            while(i < card.length) {
                card.add(PackOfCards.selectCard());
                card = new PackOfCards(selectCard());
                new PackOfCards();
    THE ABOVE LINES COLOURED RED INDICATE WHERE I AM HAVING TROUBLE
                i++;
            }
        }
    }
    What I am trying to achieve from the code in red is to construct a PackOfCards and fill the player's array with 5 cards from the pack.

    The above two classes are not all the classes in my program, but are the two relevant to my problem.

    ANY HELP IS APPRECIATED

    Thanks.

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    Zwolle, The Netherlands
    Posts
    1,120
    Thanks
    2
    Thanked 31 Times in 31 Posts
    You have an array where there are 5 slots for one card each.
    Create one PackOfCards and use that in your loop. You shouldn't take a new PackOfCards each time you are trying to select a card. When you select a card from the pack, you have to specify where the card should go (in which slot)

    So:

    Before the loop:
    // this gives a new deck of cards
    PackOfCards deck = new PackOfCards();

    then loop through your hand to fill each slot with a card:

    int i = 0;
    while(i < card.length) {
    card[i] = deck.selectCard();
    }

    you might want to check for null if you do this, because the deck might be empty. In that case, you should create a new deck to continue
    I am the luckiest man in the world

  • Users who have thanked Roelf for this post:

    webguy08 (11-18-2008)

  • #3
    Regular Coder
    Join Date
    Mar 2008
    Posts
    136
    Thanks
    39
    Thanked 1 Time in 1 Post
    Thanks for that!


  •  

    Posting Permissions

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