Results 1 to 3 of 3
  1. #1
    Renxx is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default 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());
       }
    
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default 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.

  3. #3
    coasterguy10 is offline Member
    Join Date
    Sep 2011
    Posts
    21
    Rep Power
    0

    Default 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

  1. Networked Card Game.
    By T-Prime in forum Networking
    Replies: 9
    Last Post: 07-26-2011, 01:22 AM
  2. Card Game
    By abby0910 in forum New To Java
    Replies: 1
    Last Post: 07-24-2010, 01:38 AM
  3. please help me with this card game
    By noobinoo in forum New To Java
    Replies: 13
    Last Post: 03-28-2010, 03:07 PM
  4. 4x4 2D array card game,need solving
    By highschool in forum New To Java
    Replies: 6
    Last Post: 02-25-2010, 04:36 PM
  5. card game Rummy
    By javafox in forum New To Java
    Replies: 4
    Last Post: 03-14-2009, 04:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •