User talk:Ashokdeepali

TUESDAY, NOVEMBER 15, 2011

Java Cards Game - (SevenHeart) Hey friends...

I am there again.. this time with A Computer Game.. Cards Game, Java Cards Game. SevenHeart is the cards game which is developed fully in Java. Its a very light weight, fast and easy to use application. Many people asks how to develop simple cards game in java? So here's the answer! You need to have Java installed on your machine version greater than Java 5. Just download it and double click on the file and you are on, Play the game.

Game rules are pretty simple, they are as follows: 1. There are total four players. Three are computer players and one is you. 2. Enter your name at the start of application where input dialog opens. 3. When application is started cards are shuffled and distributed equally to all players. 4. You have to make a first move. 5. There are total four categories in cards. Each category contains 7 of it. You can put any seven if you have   or just click on any other card and your chance will pass on to next user. 6. If you have 7 from any of them you can just click on it. The rule is simple, cards putting has to start with 7 of any category and after that you have to follow the series. 7. You can or you have to put card imidiat before or imdiat later number card only which is already there in the column for that particular series. For ex. there is alreay a 7 and 6 hearts cards, then you can put either 5 hearts card or 8 hearts card. 8. Who ever finishes his cards first is the winner.

Isn't it pretty simple!!!

Complete code of this project uploaded on below link: http://sourceforge.net/p/sevenheart/home/SevenHeart/

Following is the detailed design and development of this application:

This game is fully developed in the Core Java. As it is using AWT and SWING components, except java nothing else is required. We have one program called mk.java. This is the main program. This file contains one more class named as CustomAction. This class is used for event handling. CustomAction class implements MouseListener. This program requires all 52 cards images and 1 image for cards back. All are packed in one JAR file along with all class files and source files. Extract the JAR file and run mk.class. Our main class mk.java contains following methods

mk ... This is the constructor init1 .. This method is used to initialize all global variables. public void actionPerformed(ActionEvent e)  .. This is the MouseListeners's public void draw .. This method actually draws the components on the Panel. public void shuffle .. This method is used to shuffle the String array of card names. public void distribute .. This method reads the 13 cards for 4 times from shuffled array and puts it into four different arrays. public static void main .. This is the main method of the class. Here we created the instance of class, which causes to invoke the constructor of the class. Constructor invokes the calls init1 method.

Below is the full code of program.

=
===================================================================

=
===================================================================

import java.awt.*; import java.io.File; import javax.swing.*; import java.awt.event.*; //in K -A reverse array i.e a[0][0]=K

/* * This is the simple Cards Game..Fully javabased game. * @author Ashok Adsul * @version v1.0 @14-Nov-2011 * Visit : http://ashokadsul.blogspot.com * Email : ashokadsul1987@gmail.com */

class mk implements ActionListener{ public static JLabel arr[][]=new JLabel[13][4];

JLabel userName = new JLabel; JLabel com1 = new JLabel("Ashok A"); JLabel com2 = new JLabel("Deepali A"); JLabel com3 = new JLabel("Atul A"); JButton replay = new JButton("  Replay   "); public static JLabel u1[]=new JLabel[13]; public static JLabel u2[]=new JLabel[13]; public static JLabel u3[]=new JLabel[13]; public static JLabel u4[]=new JLabel[13];

public static String strArr[][] = new String[13][4]; public static String strU1[] = new String[13]; public static String strU2[] = new String[13]; public static String strU3[] = new String[13]; public static String strU4[] = new String[13]; String[] strCards = null;

String str = null; public static JFrame f;   JPanel jp; JLabel l1,l2,l3,l4; ImageIcon i1,i2; String s,ext=".gif"; String addr="PIC\\"; File files = new File(addr);

mk { init1; }   public void init1 { System.out.println("############# Inside init1 method. ......"+addr); strCards = files.list;  //Lists all images from PIC\\ directory.. for(int x=0; x<strCards.length; x++){ //System.out.println(" ["+x+"] = "+strCards[x]); }       shuffle;  //Shuffle the Cards once in a lifetime of particular game. distribute;  //Distributes the 13-13 cards to each player. i1=new ImageIcon(addr+"\\others\\11.gif"); i2=new ImageIcon(addr+"\\others\\22.gif"); jp = new JPanel; jp.setBackground(Color.GREEN); f=new JFrame; l1=new JLabel(i1); l2=new JLabel(i1); l3=new JLabel(i2); l4=new JLabel(i2); jp.setLayout(null); l1.setBounds(100,600,500,100); l2.setBounds(100,0,500,100); l3.setBounds(0,100,100,500); l4.setBounds(600,100,100,500); userName.setBounds(2, 650, 100, 30); com1.setBounds(2, 50, 100, 30); com2.setBounds(602, 50, 100, 30); com3.setBounds(602, 650, 100, 30); replay.setBounds(602, 620, 100, 30); replay.addActionListener(this); f.add(jp); f.setVisible(true); f.setSize(750,750); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setAlwaysOnTop(true); //f.setResizable(false); draw;    //Actual cards drawing on screen.. jp.add(l1); jp.add(l2); jp.add(l3); jp.add(l4); jp.add(userName); jp.add(com1); jp.add(com2); jp.add(com3); jp.add(replay); str = JOptionPane.showInputDialog(null, "Enter your name please !: ", "Seven Hearts user", 1); if(str!=null){ userName.setText(str); }else{ userName.setText("Guest User"); }   }//end of init1..

public void actionPerformed(ActionEvent e){ System.out.println("*******************************************************") ; System.out.println("     User wants to play again      "); System.out.println("*******************************************************") ; shuffle; distribute; draw; }   //This method actually draws the cards on screen //as a labels. public void draw{ int z=13; for(int i=0;i<13;i++) { for(int j=0;j<4;j++) { /* if(i==3){ if(j==0) s="A"; if(j==1) s="B"; if(j==2) s="C"; if(j==3) s="D"; arr[i][j]=new JLabel; arr[i][j].setBounds((j+1)*100+20,490-30*i,100,100); jp.add(arr[i][j]); }else{*/ if(j==0) s="A"; if(j==1) s="B"; if(j==2) s="C"; if(j==3) s="D"; //arr[i][j]=new JLabel(new ImageIcon(addr+s+z+ext)); arr[i][j]=new JLabel; arr[i][j].setBounds((j+1)*100+20,490-30*i,100,100); jp.add(arr[i][j]); //}           }            z--; }       int m=13; for(int i=0;i<13;i++) { String s1="A"; //u1[i]=new JLabel(new ImageIcon(addr+s1+m+ext)); //strU1 //System.out.println("User 1 Cards = "+strU1[i]); u1[i]=new JLabel(new ImageIcon(addr+strU1[i])); u1[i].setBounds(480-30*i,600,100,100); //u1[i].setName(s1+m); u1[i].setName(strU1[i].substring(0, strU1[i].indexOf("."))); u1[i].addMouseListener(new CustomAction); jp.add(u1[i]); m--; }       int n=13; for(int i=0;i<13;i++) { String s1="B"; //u2[i]=new JLabel(new ImageIcon(addr+s1+n+ext)); u2[i]=new JLabel; u2[i].setIcon(new ImageIcon("PIC\\others\\back.gif")); u2[i].setBounds(600,480-30*i,100,100); jp.add(u2[i]); n--; }       int o=13; for(int i=0;i<13;i++) { String s2="C"; //u3[i]=new JLabel(new ImageIcon(addr+s2+o+ext)); u3[i]=new JLabel; u3[i].setIcon(new ImageIcon("PIC\\others\\back.gif")); u3[i].setBounds(480-30*i,0,100,100); jp.add(u3[i]); o--; }       int p=13; for(int i=0;i<13;i++) { String s1="D"; //u4[i]=new JLabel(new ImageIcon(addr+s1+p+ext)); u4[i]=new JLabel; u4[i].setIcon(new ImageIcon("PIC\\others\\back.gif")); u4[i].setBounds(0,480-30*i,100,100); jp.add(u4[i]); p--; }           }//end of draw..

/* * This method will shuffle the string array of cards * and reorganise the cards ordering in the same array.. */   public void shuffle{ System.out.println("Inside shuffle method.. shuffling cards....."); if(strCards!=null){ int rawRandomNumber; int min = 0; int max = 51; for(int i=0; i<52; i++){ String temp = strCards[i]; rawRandomNumber = (int) (Math.random * (max - min + 1) ) + min; String temp_2 = strCards[rawRandomNumber]; strCards[rawRandomNumber]=temp; strCards[i]=temp_2; }//end for loop }//end if(strCards!=null) for(int j=0; j<strCards.length; j++){ //System.out.println("New cards["+j+"] = "+strCards[j]); }   }  //end of shuffle...

/* * This method is called to distribute the cards equally to all * players including three computer and one manual user. */       public void distribute{ System.out.println("######### Inside distribute method...."); for(int usr1=0; usr1<strU1.length; usr1++){   //User 1, for loop strU1[usr1] = strCards[usr1]; }//end of for loop.. for(int usr2=0; usr2<strU2.length; usr2++){   //User 2, for loop strU2[usr2] = strCards[usr2+(1*13)]; }//end of for loop.. for(int usr3=0; usr3<strU3.length; usr3++){   //User 3, for loop strU3[usr3] = strCards[usr3+(2*13)]; }//end of for loop.. for(int usr4=0; usr4<strU4.length; usr4++){   //User 4, for loop strU4[usr4] = strCards[usr4+(3*13)]; }//end of for loop.. }//end of distribute..

/* * This is the main method of the class... */       public static void main(String arg[]) { new mk; }//end of main method.. }//Class mk ends

/* * This is the Customized class for mouse Event handling * as and when user clicks on his cards the event is detected and * according the action is taken. */ class CustomAction implements MouseListener{ public void mouseClicked(MouseEvent e) { Object id = (Object)e.getSource; if(id instanceof JLabel){ JLabel tmpLabel =(JLabel)id; System.out.println("Mouse clicked event occured on "+tmpLabel.getName+" "+tmpLabel.getLabelFor); //tmpLabel = new JLabel; //tmpLabel.setText("Hi"); //tmpLabel.setIcon(new ImageIcon("PIC\\others\\back.gif")); boolean flag = false; String name = tmpLabel.getName;

if(name.indexOf("A")!=-1){ String v = name.substring(1); int index = Integer.parseInt(v); int block = 0; if(mk.strArr[6][block]==null){ if(index==7){ mk.arr[index-1][block].setIcon(tmpLabel.getIcon); mk.strArr[index-1][block] = name; flag=true; }else{ //JOptionPane.showInternalMessageDialog(mk.f, "Before 7 you cant put any other card!","Required 7", JOptionPane.INFORMATION_MESSAGE); JOptionPane.showMessageDialog(null, "Before 7 you cant put any other card!","Required 7", JOptionPane.ERROR_MESSAGE); }               }else if(index > 7){ if(mk.strArr[index-2][block]!=null){ mk.arr[index-1][block].setIcon(tmpLabel.getIcon); mk.strArr[index-1][block] = name; flag=true; }                  }else if(index < 7){ if(mk.strArr[index][block]!=null){ mk.arr[index-1][block].setIcon(tmpLabel.getIcon); mk.strArr[index-1][block] = name; flag=true; }               }                System.out.println("************ A block " +v); }//end A block if(name.indexOf("B")!=-1){ String v = name.substring(1); int index = Integer.parseInt(v); int block = 1; if(mk.strArr[6][block]==null){ if(index==7){ mk.arr[index-1][block].setIcon(tmpLabel.getIcon); mk.strArr[index-1][block] = name; flag=true; }else{ JOptionPane.showMessageDialog(null, "Before 7 you cant put any other card!","Required 7", JOptionPane.ERROR_MESSAGE); }               }else if(index > 7){ if(mk.strArr[index-2][block]!=null){ mk.arr[index-1][block].setIcon(tmpLabel.getIcon); mk.strArr[index-1][block] = name; flag=true; }                  }else if(index < 7){ if(mk.strArr[index][block]!=null){ mk.arr[index-1][block].setIcon(tmpLabel.getIcon); mk.strArr[index-1][block] = name; flag=true; }               }                /*                String v = name.substring(1); System.out.println("************ B block " +v); mk.arr[Integer.parseInt(v)-1][1].setIcon(tmpLabel.getIcon); mk.strArr[Integer.parseInt(v)-1][1] = name; flag=true; */           }            if(name.indexOf("C")!=-1){ String v = name.substring(1); int index = Integer.parseInt(v); int block = 2; if(mk.strArr[6][block]==null){ if(index==7){ mk.arr[index-1][block].setIcon(tmpLabel.getIcon); mk.strArr[index-1][block] = name; flag=true; }else{ JOptionPane.showMessageDialog(null, "Before 7 you cant put any other card!","Required 7", JOptionPane.ERROR_MESSAGE); }               }else if(index > 7){ if(mk.strArr[index-2][block]!=null){ mk.arr[index-1][block].setIcon(tmpLabel.getIcon); mk.strArr[index-1][block] = name; flag=true; }                  }else if(index < 7){ if(mk.strArr[index][block]!=null){ mk.arr[index-1][block].setIcon(tmpLabel.getIcon); mk.strArr[index-1][block] = name; flag=true; }               }                /*                String v = name.substring(1); System.out.println("************ C block " +v); mk.arr[Integer.parseInt(v)-1][2].setIcon(tmpLabel.getIcon); mk.strArr[Integer.parseInt(v)-1][2] = name; flag=true; */           }            if(name.indexOf("D")!=-1){ String v = name.substring(1); int index = Integer.parseInt(v); int block = 3; if(mk.strArr[6][block]==null){ if(index==7){ mk.arr[index-1][block].setIcon(tmpLabel.getIcon); mk.strArr[index-1][block] = name; flag=true; }else{ JOptionPane.showMessageDialog(null, "Before 7 you cant put any other card!","Required 7", JOptionPane.ERROR_MESSAGE); }               }else if(index > 7){ if(mk.strArr[index-2][block]!=null){ mk.arr[index-1][block].setIcon(tmpLabel.getIcon); mk.strArr[index-1][block] = name; flag=true; }                  }else if(index < 7){ if(mk.strArr[index][block]!=null){ mk.arr[index-1][block].setIcon(tmpLabel.getIcon); mk.strArr[index-1][block] = name; flag=true; }               }                /*                String v = name.substring(1); System.out.println("************ D block " +v); mk.arr[Integer.parseInt(v)-1][3].setIcon(tmpLabel.getIcon); mk.strArr[Integer.parseInt(v)-1][3] = name; flag=true; */           }            if(flag){ tmpLabel.setIcon(null); tmpLabel.removeMouseListener(this); }       }//end if        putComputerCard; }//end mouseClicked..

public void mousePressed(MouseEvent e) { }

public void mouseReleased(MouseEvent e) { }

public void mouseEntered(MouseEvent e) { }

public void mouseExited(MouseEvent e) { }

/* * User defined method for computer player to put * cards into the list. */   public void putComputerCard{ System.out.println("**** putComputerCard.. method"); int counterA =0; int counterB =0; int counterC =0; int counterD =0; /*       for(int i=0; i<mk.strU1.length; i++){ if(mk.strU1[i].indexOf("A")!=-1){ counterA++; }else if(mk.strU1[i].indexOf("B")!=-1){ counterB++; }else if(mk.strU1[i].indexOf("C")!=-1){ counterC++; }else if(mk.strU1[i].indexOf("D")!=-1){ counterD++; }       }//end for loop */

boolean user1_flag = true; for(int x=0; x<mk.strU2.length; x++){ System.out.println("        ... "+mk.strU2[x]); if(mk.strU2[x]!=null){ user1_flag = false; if(compUser(mk.strU2[x].substring(0, mk.strU2[x].indexOf(".")))){ mk.strU2[x]=null; mk.u2[x].setIcon(null); break; }           }//end if        }//end for if(user1_flag){ JOptionPane.showMessageDialog(null, "Congratulations Ashok A you won !!!","Congratulations", JOptionPane.ERROR_MESSAGE); }

boolean user2_flag = true; for(int x=0; x<mk.strU3.length; x++){ if(mk.strU3[x]!=null){ user2_flag = false; if(compUser(mk.strU3[x].substring(0, mk.strU3[x].indexOf(".")))){ mk.strU3[x]=null; mk.u3[x].setIcon(null); break; }           }//end if        }//end for if(user2_flag){ JOptionPane.showMessageDialog(null, "Congratulations 'Deepali A' you won !!!","Congratulations", JOptionPane.ERROR_MESSAGE); }

boolean user3_flag = true; for(int x=0; x<mk.strU4.length; x++){ if(mk.strU4[x]!=null){ user3_flag = false; if(compUser(mk.strU4[x].substring(0, mk.strU4[x].indexOf(".")))){ mk.strU4[x]=null; mk.u4[x].setIcon(null); break; }           }//end if        }//end for if(user3_flag){ JOptionPane.showMessageDialog(null, "Congratulations 'Atul A' you won !!!","Congratulations", JOptionPane.ERROR_MESSAGE); }   }//end putComputerCard.. public boolean compUser(String name){ boolean flag = false; System.out.println("################# Inside compUser name = "+name); if(name.indexOf("A")!=-1){ String v = name.substring(1); int index = Integer.parseInt(v); int block = 0; if(mk.strArr[6][block]==null){ if(index==7){ mk.arr[index-1][block].setIcon(new ImageIcon("PIC\\"+name+".gif")); mk.strArr[index-1][block] = name; flag=true; }else{ //JOptionPane.showInternalMessageDialog(mk.f, "Before 7 you cant put any other card!","Required 7", JOptionPane.INFORMATION_MESSAGE); //JOptionPane.showMessageDialog(null, "Before 7 you cant put any other card!","Required 7", JOptionPane.ERROR_MESSAGE); }               }else if(index > 7){ if(mk.strArr[index-2][block]!=null){ mk.arr[index-1][block].setIcon(new ImageIcon("PIC\\"+name+".gif")); mk.strArr[index-1][block] = name; flag=true; }                  }else if(index < 7){ if(mk.strArr[index][block]!=null){ mk.arr[index-1][block].setIcon(new ImageIcon("PIC\\"+name+".gif")); mk.strArr[index-1][block] = name; flag=true; }               }                //System.out.println("************ A block " +v); }//end A block if(name.indexOf("B")!=-1){ String v = name.substring(1); int index = Integer.parseInt(v); int block = 1; if(mk.strArr[6][block]==null){ if(index==7){ mk.arr[index-1][block].setIcon(new ImageIcon("PIC\\"+name+".gif")); mk.strArr[index-1][block] = name; flag=true; }else{ //JOptionPane.showMessageDialog(null, "Before 7 you cant put any other card!","Required 7", JOptionPane.ERROR_MESSAGE); }               }else if(index > 7){ if(mk.strArr[index-2][block]!=null){ mk.arr[index-1][block].setIcon(new ImageIcon("PIC\\"+name+".gif")); mk.strArr[index-1][block] = name; flag=true; }                  }else if(index < 7){ if(mk.strArr[index][block]!=null){ mk.arr[index-1][block].setIcon(new ImageIcon("PIC\\"+name+".gif")); mk.strArr[index-1][block] = name; flag=true; }               }                /*                String v = name.substring(1); System.out.println("************ B block " +v); mk.arr[Integer.parseInt(v)-1][1].setIcon(tmpLabel.getIcon); mk.strArr[Integer.parseInt(v)-1][1] = name; flag=true; */           }            if(name.indexOf("C")!=-1){ String v = name.substring(1); int index = Integer.parseInt(v); int block = 2; if(mk.strArr[6][block]==null){ if(index==7){ mk.arr[index-1][block].setIcon(new ImageIcon("PIC\\"+name+".gif")); mk.strArr[index-1][block] = name; flag=true; }else{ //JOptionPane.showMessageDialog(null, "Before 7 you cant put any other card!","Required 7", JOptionPane.ERROR_MESSAGE); }               }else if(index > 7){ if(mk.strArr[index-2][block]!=null){ mk.arr[index-1][block].setIcon(new ImageIcon("PIC\\"+name+".gif")); mk.strArr[index-1][block] = name; flag=true; }                  }else if(index < 7){ if(mk.strArr[index][block]!=null){ mk.arr[index-1][block].setIcon(new ImageIcon("PIC\\"+name+".gif")); mk.strArr[index-1][block] = name; flag=true; }               }                /*                String v = name.substring(1); System.out.println("************ C block " +v); mk.arr[Integer.parseInt(v)-1][2].setIcon(tmpLabel.getIcon); mk.strArr[Integer.parseInt(v)-1][2] = name; flag=true; */           }            if(name.indexOf("D")!=-1){ String v = name.substring(1); int index = Integer.parseInt(v); int block = 3; if(mk.strArr[6][block]==null){ if(index==7){ mk.arr[index-1][block].setIcon(new ImageIcon("PIC\\"+name+".gif")); mk.strArr[index-1][block] = name; flag=true; }else{ //JOptionPane.showMessageDialog(null, "Before 7 you cant put any other card!","Required 7", JOptionPane.ERROR_MESSAGE); }               }else if(index > 7){ if(mk.strArr[index-2][block]!=null){ mk.arr[index-1][block].setIcon(new ImageIcon("PIC\\"+name+".gif")); mk.strArr[index-1][block] = name; flag=true; }                  }else if(index < 7){ if(mk.strArr[index][block]!=null){ mk.arr[index-1][block].setIcon(new ImageIcon("PIC\\"+name+".gif")); mk.strArr[index-1][block] = name; flag=true; }               }                /*                String v = name.substring(1); System.out.println("************ D block " +v); mk.arr[Integer.parseInt(v)-1][3].setIcon(tmpLabel.getIcon); mk.strArr[Integer.parseInt(v)-1][3] = name; flag=true; */           }         return flag; }//end compUser... }//end of CustomAction.class