This site requires JavaScript, please enable it in your browser!
Greenfoot back
young.henry
young.henry wrote ...

2017/4/18

java.lang.IllegalArgumentException: bound must be positive

young.henry young.henry

2017/4/18

#
Hey guys I'm having some issues with this error, any help would be appreciated
young.henry young.henry

2017/4/18

#
    public void fillArray()
    {
        for(int i ; i < BOARD_SIZE/2; i++)
        {
            Card randomCard = deck.deal();
            Card cardCopy = new Card(randomCard.getCardImage());
            board[getRandomPosition()] = randomCard;
            board[getRandomPosition()] = cardCopy;
            //System.out.println();

        }
    }
Heres the code with the error :)
danpost danpost

2017/4/18

#
You are not initializing the for loop counter 'i'. Try changing line 3 to this:
for (int i = 0; i < BOARD_SIZE/2; i++)
young.henry young.henry

2017/4/18

#
Once changing that line it still came up with the same error, here is the code for the entire class :)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.Color;

/**
 *  Object: Player is to turn over two cards at a time tring to obtain a match.
 *  In the case of a match, the cards remain face-up. If no match, the cards are turned
 *  back over. Game continues until all cards are turned face-up.
 */
public class Level  extends World
{
    private GreenfootImage bg;  // background image
    private Card[] board;  // holds cards for game
    private Deck deck; // the deck from which the cards are dealt.
    private int numOver; // total number of cards face-up.
    private int numShowing; // number of cards showing for the current play
    private Card[] cardsShowing; // holds the cards showing for the current play
    private Label label1;
    
    private static final int BOARD_SIZE = 16; //Number of cards on the Memory Board
    private static final Color bgColor = Color.LIGHT_GRAY; //Board color
    /**
     * Constructor for objects of class MemoryWorld.
     */
    public Level()
    {    
        // Create a new world with 1000 x 600 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1);
        setBackground(new GreenfootImage("Background.jpg"));
        //fills background of board
        bg = getBackground();         
        bg.setColor(bgColor);
        bg.fill();      
        
        //keeps track of cards on board
        board = new Card[BOARD_SIZE];
        
        //keeps track of Cards for current play
        cardsShowing = new Card[2];
        
        numOver = 0;  // total number of cards face-up.
        numShowing = 0; // number of cards showing for the current play
        deck = new Deck();
        
        fillArray(); 
        setup();

    }
    
    /**
     *  Gets BOARD_SIZE/2 cards from the Deck. Creates a copy of each of these cards. Randomly
     *  places these cards in the board array created for this purpose. 
     */
    public void fillArray()
    {
        for(int i = 0; i < BOARD_SIZE/2; i++)
        {
            Card randomCard = deck.deal();
            Card cardCopy = new Card(randomCard.getCardImage());
            board[getRandomPosition()] = randomCard;
            board[getRandomPosition()] = cardCopy;
            //System.out.println();

        }
    }
    
        /**
         * Sets the visual board.
   
         */
    public void setup()
    {
        final int SQUARE_SIZE = 3;    //number of rows and columns
        int x = 80; 
        int y = 80;
        int index = 0;
        for(int row = 0; row < SQUARE_SIZE;row++)
        {
            x = 80;
            for(int col = 0; col < SQUARE_SIZE; col++)
            {
                board[index].turnOver();    //comment if you want cards to be face-up 
                //board[index].showFace();  //uncomment if you want cards to show
                addObject(board[index], x, y);
                index++;
                x += 80;
            }
            y += 110;
        }
        

        addObject(label1, 120 ,470 );
        
        
    }

   
    /**
     *  Chooses a random empty position in the board array..
     *  @return a random empty position in the board array.
     */
    public int getRandomPosition()
    {
        int randPlace = Greenfoot.getRandomNumber(BOARD_SIZE);
        while (board[randPlace] != null)
        {
            randPlace = Greenfoot.getRandomNumber(BOARD_SIZE); 
        }
        //System.out.print(randPlace + " ");
        return randPlace;
    }
    
    /**
     * Places this card in the array the holds the cards of the current play (max of 2)
     * Increments the current number of cards showing.
     * @param c the Card object currently face-up on this turn.
     */
    public void recordCardShowing(Card c)
    {
        cardsShowing[numShowing] = c;        
        numShowing++;
    }
    
    /**
     * Checks to see is the cards in the cardsShowing array (cards showing on current play)
     * have the same image.
     * @return true if cards in cardsShowing array have the same image, false otherwise.
     */
    public boolean checkMatch()
    {
        return cardsShowing[0].getCardImage().equals(cardsShowing[1].getCardImage());
    }
    
    /**
     * The game.
     * Check if there are two cards currently showing. If so, check to see if they match.
     * If so, flash screen red, increment total number of cards showing by 2. If the cards 
     * do not match, turn the cards over.
     * In either case,  the array elements of cardsShowing are set to null and 
     * number of cards currently showing is set to 0.
     * 
     * Check for a win (are all cards face-up? If so, game is over.
     */
    public void act()
    {
        if (numShowing == 2)
        {
            Greenfoot.delay(50);
             if (checkMatch())
             {
                 bg.setColor(Color.RED);  
                 bg.fill();
                 Greenfoot.playSound("cowbell.wav");
                 Greenfoot.delay(10);
                 bg.setColor(bgColor);  
                 bg.fill();
                 numOver +=2;

             }
             else
             {
                 Greenfoot.playSound         ("no.wav");
                 cardsShowing[0].turnOver();
                 cardsShowing[1].turnOver();
             }
            cardsShowing[0]=null;
            cardsShowing[1]=null;;
            numShowing = 0;
        }
        
        //check win
        if(numOver == BOARD_SIZE)
        {
            //System.out.println("WIN");
            Greenfoot.playSound("tada.wav");
            bg.setColor(Color.RED);  
            bg.fill();
            Greenfoot.stop();
        }

        
        
   }


}
danpost danpost

2017/4/18

#
The only major problem I could find at first glance over is that you are not creating a Label object before trying to add it into the world. One minor issue is the value of SQUARE_SIZE assigned at line 72 which does not coincide with the number of cards held by 'board'. Please make appropriate changes, then clear the terminal window and re-compile the project. Then re-produce the error and copy/paste the terminal window error output here along with the revised class code.
young.henry young.henry

2017/4/19

#
So I removed the label as I will add that at a later date, I also changed the SQUARE_SIZE back to what I originally had it as.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.Color;

/**
 *  Object: Player is to turn over two cards at a time tring to obtain a match.
 *  In the case of a match, the cards remain face-up. If no match, the cards are turned
 *  back over. Game continues until all cards are turned face-up.
 */
public class Level  extends World
{
    private GreenfootImage bg;  // background image
    private Card[] board;  // holds cards for game
    private Deck deck; // the deck from which the cards are dealt.
    private int numOver; // total number of cards face-up.
    private int numShowing; // number of cards showing for the current play
    private Card[] cardsShowing; // holds the cards showing for the current play
    private Label label1;
    
    private static final int BOARD_SIZE = 16; //Number of cards on the Memory Board
    private static final Color bgColor = Color.LIGHT_GRAY; //Board color
    /**
     * Constructor for objects of class MemoryWorld.
     */
    public Level()
    {    
        // Create a new world with 1000 x 600 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1);
        setBackground(new GreenfootImage("Background.jpg"));
        //fills background of board
        bg = getBackground();         
        bg.setColor(bgColor);
        bg.fill();      
        
        //keeps track of cards on board
        board = new Card[BOARD_SIZE];
        
        //keeps track of Cards for current play
        cardsShowing = new Card[2];
        
        numOver = 0;  // total number of cards face-up.
        numShowing = 0; // number of cards showing for the current play
        deck = new Deck();
        
        fillArray(); 
        setup();

    }
    
    /**
     *  Gets BOARD_SIZE/2 cards from the Deck. Creates a copy of each of these cards. Randomly
     *  places these cards in the board array created for this purpose. 
     */
    public void fillArray()
    {
        for(int i = 0; i < BOARD_SIZE/2; i++)
        {
            Card randomCard = deck.deal();
            Card cardCopy = new Card(randomCard.getCardImage());
            board[getRandomPosition()] = randomCard;
            board[getRandomPosition()] = cardCopy;
            //System.out.println();

        }
    }
    
        /**
         * Sets the visual board.
   
         */
    public void setup()
    {
        final int SQUARE_SIZE = 4;    //number of rows and columns
        int x = 80; 
        int y = 80;
        int index = 0;
        for(int row = 0; row < SQUARE_SIZE;row++)
        {
            x = 80;
            for(int col = 0; col < SQUARE_SIZE; col++)
            {
                board[index].turnOver();    //comment if you want cards to be face-up 
                //board[index].showFace();  //uncomment if you want cards to show
                addObject(board[index], x, y);
                index++;
                x += 80;
            }
            y += 110;
        }
        
        
        
    }

   
    /**
     *  Chooses a random empty position in the board array..
     *  @return a random empty position in the board array.
     */
    public int getRandomPosition()
    {
        int randPlace = Greenfoot.getRandomNumber(BOARD_SIZE);
        while (board[randPlace] != null)
        {
            randPlace = Greenfoot.getRandomNumber(BOARD_SIZE); 
        }
        //System.out.print(randPlace + " ");
        return randPlace;
    }
    
    /**
     * Places this card in the array the holds the cards of the current play (max of 2)
     * Increments the current number of cards showing.
     * @param c the Card object currently face-up on this turn.
     */
    public void recordCardShowing(Card c)
    {
        cardsShowing[numShowing] = c;        
        numShowing++;
    }
    
    /**
     * Checks to see is the cards in the cardsShowing array (cards showing on current play)
     * have the same image.
     * @return true if cards in cardsShowing array have the same image, false otherwise.
     */
    public boolean checkMatch()
    {
        return cardsShowing[0].getCardImage().equals(cardsShowing[1].getCardImage());
    }
    
    /**
     * The game.
     * Check if there are two cards currently showing. If so, check to see if they match.
     * If so, flash screen red, increment total number of cards showing by 2. If the cards 
     * do not match, turn the cards over.
     * In either case,  the array elements of cardsShowing are set to null and 
     * number of cards currently showing is set to 0.
     * 
     * Check for a win (are all cards face-up? If so, game is over.
     */
    public void act()
    {
        if (numShowing == 2)
        {
            Greenfoot.delay(50);
             if (checkMatch())
             {
                 bg.setColor(Color.RED);  
                 bg.fill();
                 Greenfoot.playSound("cowbell.wav");
                 Greenfoot.delay(10);
                 bg.setColor(bgColor);  
                 bg.fill();
                 numOver +=2;

             }
             else
             {
                 Greenfoot.playSound         ("no.wav");
                 cardsShowing[0].turnOver();
                 cardsShowing[1].turnOver();
             }
            cardsShowing[0]=null;
            cardsShowing[1]=null;;
            numShowing = 0;
        }
        
        //check win
        if(numOver == BOARD_SIZE)
        {
            //System.out.println("WIN");
            Greenfoot.playSound("tada.wav");
            bg.setColor(Color.RED);  
            bg.fill();
            Greenfoot.stop();
        }

        
        
   }


}
java.lang.IllegalArgumentException: bound must be positive at java.util.Random.nextInt(Random.java:388) at greenfoot.Greenfoot.getRandomNumber(Greenfoot.java:146) at Deck.deal(Deck.java:39) at Level.fillArray(Level.java:57) at Level.<init>(Level.java:44) at Home.<init>(Home.java:23) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at greenfoot.core.Simulation.newInstance(Simulation.java:617) at greenfoot.platforms.ide.WorldHandlerDelegateIDE.lambda$instantiateNewWorld$7(WorldHandlerDelegateIDE.java:430) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:502) at greenfoot.core.Simulation.maybePause(Simulation.java:305) at greenfoot.core.Simulation.runContent(Simulation.java:218) at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2017/4/19

#
The problem code is in your Deck class, line 39, in the 'deal' method. Show class code for help.
young.henry young.henry

2017/4/20

#
Here is the class code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;

public class Deck  extends Actor
{
    // Strings used to form filenames for the card images.
    private static final String[] DENOMINATIONS = {"1","2","3","4","5","0"};
    private static final String[] SUITS = {"Card"};
    
     
    private List<Card> cards;  // holds playing cards
    
    /**
     * Default constructor initializes and fills the list of Card objects.
     */
    public Deck()
    {
        cards = new ArrayList<Card>();
        for (int d = 0; d < DENOMINATIONS.length; d++)
        {
            for(int s = 0; s < SUITS.length; s++)
            {
                String cardImage = DENOMINATIONS[d] + SUITS[s] + ".jpg";
                Card c = new Card(cardImage);
                cards.add(c);
            }
        }
        
    }
    
    /**
     * Chooses and returns a random ccard object fr.om this deck.
     * The card is removed from the deck.
     * @return a random Card object
     */
    public Card deal()
    {
        int randCard = Greenfoot.getRandomNumber(cards.size());
        Card c = cards.remove(randCard);
        //System.out.print(c.getCardImage());
        
        return c;
        
    }
    
    
    /**
     * No action
     */
    public void act() 
    {
        //do nothing
    }   
    
    
}
danpost danpost

2017/4/20

#
You are only creating 6 cards; but, trying to deal 16 of them. Add a couple more to SUITS.
You need to login to post a reply.