Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-17-2008, 11:46 PM   PM User | #1
webguy08
Regular Coder

 
Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
webguy08 is an unknown quantity at this point
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.
webguy08 is offline   Reply With Quote
Old 11-18-2008, 06:47 AM   PM User | #2
Roelf
Senior Coder

 
Join Date: Jun 2002
Location: Zwolle, The Netherlands
Posts: 1,110
Thanks: 2
Thanked 28 Times in 28 Posts
Roelf is on a distinguished road
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
Roelf is offline   Reply With Quote
Users who have thanked Roelf for this post:
webguy08 (11-18-2008)
Old 11-18-2008, 06:17 PM   PM User | #3
webguy08
Regular Coder

 
Join Date: Mar 2008
Posts: 136
Thanks: 39
Thanked 1 Time in 1 Post
webguy08 is an unknown quantity at this point
Thanks for that!
webguy08 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:08 AM.


Advertisement
Log in to turn off these ads.