View Single Post
  #1 (permalink)  
Old 05-04-04, 10:26 PM
bmw2213 bmw2213 is offline
Newbie Coder
 
Join Date: Jan 2004
Location: Kirksville, MO
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Little Help please

this is my method to playRound. I get an error because for some reason, the method won't just draw one card from the deck, but rather continually goes until the deck runs out, giving an ArrayIndexOutOfBoundsException. Any ideas on why its looping?

Code:
private void playRound(String playerTurn) {
	Card topCard = playedDeck.getTopCard();
	Card drawnCard;
        Card cardToPlay;
        int newSuit;
	boolean draw = false;
	for (int i = 0; i < numPlayers; i++) {
		if (player[i].getName() == playerTurn){
			draw = player[i].playTurn(topCard);
			if (!draw) {
                            	drawnCard = deck.getCard();
                                player[i].insertCardInHand(drawnCard);
			}
                        else {
                            cardToPlay = player[i].selectCard(topCard);
                            if (cardToPlay.getRank() == 8) {
                                newSuit = player[i].chooseSuit();
                                topCard = new Card(newSuit, 8);
                                System.out.println(topCard.getSuit());
                            }
                            playedDeck.playCard(cardToPlay);
                        }
                        if (player[i].getHand().getNumCards() == 0) {
                            win = true;
                        }
                        else {
                            if (i + 1 < numPlayers) 
                            playerTurn = player[i+1].getName();
                            topCard = playedDeck.getTopCard();
                        }
		}
	}
        
        
}


Code:
public Card getCard() {
// 'Deals' one card from the top of the deck, card is not deleted
//pre: deck is not empty
return deck[top--];
}
Reply With Quote