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

#
1
2
3
4
5
6
7
8
9
10
11
12
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:
1
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 :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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.