2 Replies - 529 Views - Last Post: 06 June 2014 - 04:42 PM Rate Topic: -----

#1 devg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 14

filling an array with an object

Posted 06 June 2014 - 04:18 PM

im trying to create an array filled with the object Card. It keep throwing the exception that the "deck is empty". im not sure why that's happening bec the code for filling the array seems fine. any help would be appreciated. thanks!

public class Deck {
	private Card[] deck;
	private CardPile cardPile;
	private int numCards;
	
	public Deck() throws InvalidDataException{
		this.deck = new Card[52];
		//too keep track of where it should be placed in the array
				int position = 0;
				for (int i =0; i<13; i++){
					for (int j = 0; j<4; j++){
						deck [position] = new Card(i);
						position++;
						}
						}
				
	}
public Card removeCard() throws Exception {
		// if no more cards throw Exception
				if (numCards > 0) {
					Card removedCard = deck[0];
					// System.out.println("About to remove card " +
					// removedCard.toString());
					deck[0] = deck[numCards - 1]; // move last one into this vacated
													// position
					numCards--;
					return removedCard; // return card that was removed
				} else {
					// System.out.println("deck is empty");
					throw new Exception("deck is empty");
				}
	}

public Game(int numPlayers) throws Exception {
		this.numPlayers = numPlayers;
		 aDeck = new Deck();
		// max number of players is 7. minimum is 2.
		if (numPlayers < 2 || numPlayers > 7) {
			throw new InvalidDataException("Invalid number of players");
		}
		
		
		// this.aDeck.shuffle(); //shuffle the Deck
		//create an array so can loop through it to set up each player
		 players = new Players[numPlayers];

		
		for (int i = 0; i < this.numPlayers; i++) {
			players[i] = new Players(); 
		}
		// if players>4 each player gets 5 cards otherwise they get 7.
		if (numPlayers < 4) {
			for (int j = 0; j < this.numPlayers; j++) {
				for (int k = 0; k < 7; k++) {

					Card aCard = this.aDeck.removeCard();
					players[j].addCard(aCard);
				}

			}
		}
		// if there is more than 4 players then they only get 5 cards each
		else {
			for (int j = 0; j < this.numPlayers; j++) {
				for (int k = 0; k < 5; k++) {

					Card aCard = this.aDeck.removeCard();
					players[j].addCard(aCard);
					}
				}
			}

This post has been edited by smohd: 06 June 2014 - 04:29 PM
Reason for edit:: Code tags addes, please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: filling an array with an object

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: filling an array with an object

Posted 06 June 2014 - 04:32 PM

it is clear that you are checking if numCards is greater than 0, but I dont see anywhere where you updated the value of it, so it will be default to 0.
Was This Post Helpful? 0
  • +
  • -

#3 devg  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 06-June 14

Re: filling an array with an object

Posted 06 June 2014 - 04:42 PM

that helps! thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1