I'm trying to create a card game with two players; you and the computer. I'm attempting to form a main ArrayList where each row holds a ArrayList of cards objects. Each row holds Diamonds, Clubs, Hearts and Spaces; therefore the main ArrayList has a maximum of four rows.
The card object is Cards(int cardNo, Rank rank, Suit suit, BufferedImage tempcardImage, boolean isFaceup, boolean isCompCard).
Before I added the following code the program worked well and produced the 5 cards layout[program output 1 of 5][1[]]1.The 5 layouts are for Diamonds,Clubs.Hearts,Spades and No Trumps as the trumps. Below the 4 x 5 grid of cards is another, making 52 cards. The player who wins a trick can look at the six cards.
I'm trying to write a procedure where the computer picks a card to lay down. Hence I wrote the following code.
The following code shows a number of errors when it tries to setup the ArrayLists. Any help would be appreciated, since this is my first question stackoverflow.
ArrayList<ArrayList<Cards>> suitsCards = new ArrayList<ArrayList<Cards>>();
for(int i=3;i>-1;i--){
suitsCards.add(i,new ArrayList<Cards>());
}
for(int i=3;i>-1;i--){
for(int k=0;k<combinedCardsList.size();k++){
if(combinedCardsList.get(k).suitValue()==i){
suitsCards.get(i).add(combinedCardsList.get(k));
}
}
}
for(int i=0;i<4;i++){
for(int k=0;k<suitsCards.get(i).size();i++){
System.out.print(" "+suitsCards.get(i).get(k).suitValue());
}
System.out.println();
}
Thee errors I get are as follows:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 3, Size: 0 at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:665) at java.util.ArrayList.add(ArrayList.java:477) at newcards.GUI.(GUI.java:443) at newcards.guiIntro.legOfGame(guiIntro.java:168) at newcards.guiIntro$2.actionPerformed(guiIntro.java:107) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)