Results 1 to 3 of 3
Thread: Card game, problem with array
- 01-06-2012, 08:11 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
Card game, problem with array
The takeCard() method gives me an ArrayIndexOFbounds Exception. Im too tired or too newbie to figure out how to fix it.
The takeCard() method should work like this:
-Remove a card from Deck.
-Add it to the Hand
-Set removed card from deck to null;
Could anyone help me or give a hint ;).
Java Code:package prax; import java.util.Random; /** * * DECK CLASS * */ class Deck { private Card [] deck = new Card[36]; private int cardsInDeck = deck.length; private Random ran = new Random(); Deck() { for (int i = 0; i < deck.length; i++) { deck[i] = new Card(); } } public int getCardsLeftInDeck() { return cardsInDeck; } public Card [] getDeck() { return deck; } public void shuffle() { Card temp = new Card(); int randomCardNr; for (int i = 0; i < deck.length; i ++) { randomCardNr = ran.nextInt(deck.length); temp = deck[i]; deck[i] = deck[randomCardNr]; deck[randomCardNr] = temp; } } public void printDeck() { for (int i = 0 ; i < deck.length; i++) { System.out.println("Card nr " +(i +": ") +deck[i].getCardValue()); } } public void removeCard() { deck[cardsInDeck] = null; cardsInDeck--; } } /** * * HAND CLASS * */ class Hand { private int cardsInHand; private Card [] hand = new Card[5]; Hand() { } public void takeCard(Deck d) { this.hand[cardsInHand] = d.getDeck()[d.getCardsLeftInDeck()]; cardsInHand++; d.removeCard(); } public int cardsInHand() { return cardsInHand; } } /** * * CARD CLASS * */ class Card { static int cardID; private int cardValue; public int getCardValue() { return cardValue ; } Card() { cardValue = cardID++; } } public class prax5_pokker { public static void main(String[] args) { Hand rene = new Hand(); Deck deck = new Deck(); deck.printDeck(); rene.takeCard(deck); System.out.println(deck.getCardsLeftInDeck()); } }
- 01-06-2012, 08:33 PM #2
Re: Card game, problem with array
Please post the full text of the error message. it has useful information about the exception
Print out the values of all the variables used in the statement where the error occurred to see which variable is past then of the array.
Then figure out how the variable got that value.
- 01-07-2012, 08:18 AM #3
Member
- Join Date
- Sep 2011
- Posts
- 21
- Rep Power
- 0
Re: Card game, problem with array
I think line 17 is your problem. You want the array to loop through the 36 "cards" to assign them a value, but because you are starting the loop at 0 and going up to deck.length, which equals 36, you are actually asking the loop to run 37 times because 0 is inclusive. Because your array does not have 37 elements, when the loop hits that 37th element, you are trying to access something outside of your array, causing the ArrayIndexOutOfBounds error.
Similar Threads
-
Networked Card Game.
By T-Prime in forum NetworkingReplies: 9Last Post: 07-26-2011, 12:22 AM -
Card Game
By abby0910 in forum New To JavaReplies: 1Last Post: 07-24-2010, 12:38 AM -
please help me with this card game
By noobinoo in forum New To JavaReplies: 13Last Post: 03-28-2010, 02:07 PM -
4x4 2D array card game,need solving
By highschool in forum New To JavaReplies: 6Last Post: 02-25-2010, 03:36 PM -
card game Rummy
By javafox in forum New To JavaReplies: 4Last Post: 03-14-2009, 03:53 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks