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.