Dear Programmers I am stuck in middle of Game. I have planned all the Game but while writing its code i am stucked.
Here is my first class which is working fine. it created 52 objects of Card
package testing; import javax.swing.ImageIcon; public class Cards { public Cards(int a, int b){ this.cardvalue = a; this.color = b; this.isTrump = false; this.isUsed = false; this.trumpCard = 0; this.isDiscard = false; switch(a){ case 11: this.facevalue = 1; break; case 12: this.facevalue = 2; break; case 13: this.facevalue = 3; break; case 14: this.facevalue = 4; break; default : this.facevalue = 0; } switch(b){ case 1: this.colorName = "Spade"; this.CardImage= new ImageIcon(getClass().getResource("/testing/Cards/Spade/Spade ("+a+").jpg")); break; case 2: this.colorName = "Heart"; this.CardImage= new ImageIcon(getClass().getResource("/testing/Cards/Heart/Heart ("+a+").jpg")); break; case 3: this.colorName = "Diamond"; this.CardImage= new ImageIcon(getClass().getResource("/testing/Cards/Diamond/Diamond ("+a+").jpg")); break; default : this.colorName = "Club"; this.CardImage= new ImageIcon(getClass().getResource("/testing/Cards/Club/Club ("+a+").jpg")); } } public void setIsTrump(){ this.isTrump = true; } public int getCardValue(){ return this.cardvalue; } public int getColor(){ return this.color; } public int getFaceValue(){ return this.facevalue; } public boolean getisTrump(){ return this.isTrump; } public String getColorName(){ return this.colorName; } public ImageIcon getCardImage(){ return this.CardImage; } public ImageIcon getBackSide(){ return backSide; } public boolean getIsUsed(){ return this.isUsed; } public void setIsUsed(boolean a){ this.isUsed = a; } public void setIsUsed(){ this.isUsed = true; } public void setTrumpCard(int a){ this.trumpCard = a; } public int getTrumpCard(){ return this.trumpCard; } public void setIsDiscard(){ this.isDiscard = true; } private boolean isUsed; private String colorName; private int cardvalue; private int color; // 1 For Spade 2 For Heart 3 For Diamond 4 For Club private int facevalue; private boolean isTrump; private ImageIcon CardImage; private int trumpCard; private boolean isDiscard; private ImageIcon backSide = new ImageIcon(getClass().getResource("/testing/Cards/Backside.jpg")); }
The Que Class which hold 52 cards and shuffle is
package testing; public class Que { Que(int size){ this.size = size; q = new Cards[size+1]; getloc = putloc = 0; } void put(Cards x){ if(putloc == q.length-1) {System.out.println("Que is full !!"); return;} putloc++; q[putloc]=x; } Cards get(){ if(getloc==putloc){System.out.println("Que is Empty !!");} getloc++; return q[getloc]; } int quesize(){ return this.q.length; } void QueShuffle(){ for(int k=0;k<3;k++){ for (int i = q.length - 2; i > 0; i--) { //Min + (int)(Math.random() * ((Max - Min) + 1)) int n = 1 + (int)(Math.random()*size); Cards temp = q[i]; q[i] = q[n]; q[n] = temp; } } } public Cards q[]; private int getloc,putloc,size; }
Now look another class which deals the Cards between 4 Players
package testing; public class CardsInHand { public CardsInHand(){ totalCards = new Que(52); int a =1; int b=2; for (int j = 0;j<52;j++){ if(j==13||j==26||j==39) a++; if(b==15) b=2; totalCards.put(new Cards(b,a)); b++; } totalCards.QueShuffle(); } public static void Deal(Cards[] arr){ setPlayers(arr); } private static void setPlayers(Cards[] arr){ for(int i =0;i<13;i++){ arr[i] = totalCards.get(); } sortingarr(arr); } private static void Sorting(Cards arr[]){ Cards[] temp = new Cards[13]; int Location = 0; for(Cards x: arr){ if(x.getColorName()=="Spade"){ temp[Location] = x; Location++; } } for(Cards x: arr){ if(x.getColorName()=="Heart"){ temp[Location] = x; Location++; } } for(Cards x: arr){ if(x.getColorName()=="Club"){ temp[Location] = x; Location++; } } for(Cards x: arr){ if(x.getColorName()=="Diamond"){ temp[Location] = x; Location++; } } //Sorting for(int i=0;i<13;i++){ arr[i] = temp[i]; } } private static void sortingarr(Cards[] arr){ Sorting(arr); NumberSorting(arr); } private static void NumberSorting(Cards arr[]){ int S=0;int C=0;int D=0;int H=0; for(Cards x: arr){ if(x.getColorName()=="Spade") S++; else if(x.getColorName()=="Heart") H++; else if(x.getColorName()=="Club") C++; else D++; } if(S>0){ Cards temp = arr[S-S]; for(int k=0;k<S;k++) for(int i=0;i<S-1;i++){ if(arr[i].getCardValue()<arr[i+1].getCardValue()){ temp = arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } } if(H>0){ Cards temp = arr[S+H-H]; for(int k=S+H-H;k<S+H;k++) for(int i=S+H-H;i<S+H-1;i++){ if(arr[i].getCardValue()<arr[i+1].getCardValue()){ temp = arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } } if(C>0){ Cards temp = arr[S+H+C-C]; for(int k=S+H+C-C;k<S+H+C;k++) for(int i=S+H+C-C;i<S+H+C-1;i++){ if(arr[i].getCardValue()<arr[i+1].getCardValue()){ temp = arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } } if(D>0){ Cards temp = arr[S+H+C+D-D]; for(int k=S+H+C+D-D;k<S+H+C+D;k++) for(int i=S+H+C+D-D;i<S+H+C+D-1;i++){ if(arr[i].getCardValue()<arr[i+1].getCardValue()){ temp = arr[i]; arr[i]=arr[i+1]; arr[i+1]=temp; } } } } private static Que totalCards; }
Another Class is Players which will set Attributes to the Players
package testing; import java.awt.event.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JLabel; public class Players { // ------ Constructor ------- // public Players(){ for(int i=0;i<13;i++){ this.remainingSpade[i] = true; this.remainingHeart[i] = true; this.remainingClub[i] = true; this.remainingDiamond[i] = true; } for(int i=0;i<4;i++){ this.rightOpCutSuit[i]= false; this.leftOpCutSuit[i] = false; this.frontOpCutSuit[i] = false; } this.coatCaution = false; this.gcCaution = false; this.numberOfTrumpExist = 13; this.openingBet =0; this.respondingBet =0; } // ------------- END CONSTRUCTOR ---------------------- // ---------------------- PLAYING LOGIC ------------------------- // public void play(){ this.getJLabels()[1].setIcon(this.getHand()[1].getBackSide()); this.getPlayerJLabel().setIcon(this.getHand()[1].getCardImage()); // Some Playing Logics to Choose A Card by analysing Class Attributes } // -------------------- END PLAYING LOGIC ----------------------- // // ------------------- JLABELS AND CARDS-HAND SETTING FUNCTIONS ----------------------// // --------------------------- INSTANCE FUNCTIONS ------------------------------------// // --------------------------SET AND GET FUNCTIONS -----------------------------------// public void setHand(){ CardsInHand.Deal(this.testhand); } public Cards[] getHand(){ return this.testhand; } public void setObjectForPlay(Cards obj){ this.temp = obj; } public Cards getObjectForPlay(){ return this.temp; } public static void setMainDeck(){ int p=210; for(int i=0;i<4;i++){ jLabelDeck[i] = new JLabel(); jLabelDeck[i].setIcon(Deck.getPlayers()[1].getHand()[i].getBackSide()); jLabelDeck[i].setBounds(p, 260, 80, 140); jLabelDeck[i].setOpaque(true); ShowCards.getlpane().add(jLabelDeck[i], new Integer(i), 0); p=p+30; } } public void setPlayerJLabel(int i){ this.playerJLabel = jLabelDeck[i]; } public JLabel getPlayerJLabel(){ return this.playerJLabel; } public void setJLabels(int a, int b, int changer){ loadJLabels(this.playerJLabels, a,b,changer); } private void loadJLabels(JLabel[] arr, int a, int b, int changer){ int k = changer; int x = a; int y = b; for(int i=0;i<13;i++){ arr[i] = new JLabel(); arr[i].setIcon(this.getHand()[i].getCardImage()); arr[i].setBounds(x, y, 170, 100); arr[i].setOpaque(true); ShowCards.getlpane().add(arr[i], new Integer(i), 0); if(k == 1) x = x+30; else if(k == 2) y = y+30; } } public JLabel[] getJLabels(){ return this.playerJLabels; } public void setTrickStarter(boolean a){ this.trickStarter = a; } public boolean getTrickStarter(){ return this.trickStarter; } //---------------------END SET AND GET FUNCTIONS------------------------------// // // // // //////////////////////////////////////////////////////////////////////////////// // ------------------- Start Of Logical Functions -------------------------- // public void loadLogicalFunctions(Cards[] arr){ /// <------ MAIN LOGIC (INSTANCE FUNCTION) this.scoreInHand = getScoreInHand(arr); getCardOfEachSuit(arr, this.cardsOfEachSuit); this.isBalanceHand = getIsBalanceHand(arr); this.longestSuit = getLongestSuit(arr); this.numberOfTrump = getNumberOfTrump(arr); this.smallestSuit = getSmallestSuit(arr); this.strongestSuit = getStrongestSuit(arr); this.weakestSuit = getWeakestSuit(arr); } private int getScoreInHand(Cards[] arr){ //// <----- FUNCTIONS - 1 int temp = 0; for(Cards x: arr){ temp = temp + x.getFaceValue(); } return temp; } private int getStrongestSuit(Cards[] arr){ int strong=0; int S=0;int C=0;int D=0;int H=0; for(Cards x: arr){ if(x.getColorName()=="Spade") S = S + x.getFaceValue(); else if(x.getColorName()=="Heart") H = H + x.getFaceValue(); else if(x.getColorName()=="Club") C = C + x.getFaceValue(); else D = D + x.getFaceValue(); } int[] temp = {S,H,D,C}; int max = temp[0]; for(int i=0;i<4;i++){ if(max<temp[i]){ max=temp[i]; strong = i; } } return strong+1; } private int getWeakestSuit(Cards[] arr){ //// <----- FUNCTIONS - 2 int weak=0; int S=0;int C=0;int D=0;int H=0; for(Cards x: arr){ if(x.getColorName()=="Spade") S = S + x.getFaceValue(); else if(x.getColorName()=="Heart") H = H + x.getFaceValue(); else if(x.getColorName()=="Club") C = C + x.getFaceValue(); else D = D + x.getFaceValue(); } int[] temp = {S,H,D,C}; int min = temp[0]; for(int i=0;i<4;i++){ if(min>temp[i]){ min=temp[i]; weak = i; } } return weak+1; } private int getLongestSuit(Cards[] arr){ //// <----- FUNCTIONS - 3 int Longest = 0; int[] temp = new int[4]; getCardOfEachSuit(arr, temp); int max = temp[0]; for(int i=0;i<4;i++){ if(max<temp[i]){ max=temp[i]; Longest = i; } } return Longest+1; } private int getSmallestSuit(Cards[] arr){ //// <----- FUNCTIONS - 4 int Smallest = 0; int[] temp = new int[4]; getCardOfEachSuit(arr, temp); int max = temp[0]; for(int i=0;i<4;i++){ if(max>temp[i]){ max=temp[i]; Smallest = i; } } return Smallest+1; } private boolean getIsBalanceHand(Cards[] arr){ //// <----- FUNCTIONS - 5 int S=0;int C=0;int D=0;int H=0; for(Cards x: arr){ if(x.getColorName()=="Spade") S++; else if(x.getColorName()=="Heart") H++; else if(x.getColorName()=="Club") C++; else D++; } if((S<=4&&S>=3)&&(H<=4&&H>=3)&&(D<=4&&D>=3)&&(C<=4&&C>=3)) return true; else return false; } private void getCardOfEachSuit(Cards[] arr, int[] array){ //// <----- FUNCTIONS - 6 int S=0;int C=0;int D=0;int H=0; for(Cards x: arr){ if(x.getColorName()=="Spade") S++; else if(x.getColorName()=="Heart") H++; else if(x.getColorName()=="Club") C++; else D++; } array[0] = S; array[1] = H; array[2] = D; array[3] = C; } private int getNumberOfTrump(Cards[] arr){ //// <----- FUNCTIONS - 7 int temp = 0; for(Cards x: arr){ if(x.getisTrump() == true) temp = temp+1; } return temp; } //////////////////////////////////////////////////////////////////////////////////////// // // //------------------------VARIABLES SECTION START-------------------------------------// // // //////////////////////////////////////////////////////////////////////////////////////// //private static Robots[] Robot = new Robots[3]; // private JLabel playerJLabel; private static JLabel[] jLabelDeck = new JLabel[4]; private JLabel Dick ; private boolean trickStarter; // To Get Who Will Start The Trick private Cards[] testhand = new Cards[13]; protected Cards temp; private JLabel[] playerJLabels = new JLabel[13]; private int playerJLabelLocation; private int[] cardsOfEachSuit = new int[4]; // ok private boolean[] remainingSpade = new boolean[13]; // private boolean[] remainingHeart = new boolean[13]; // private boolean[] remainingDiamond = new boolean[13]; // private boolean[] remainingClub = new boolean[13]; // private int scoreInHand; // ok private int longestSuit; // ok private int smallestSuit; // ok private int strongestSuit; // ok private int weakestSuit; // ok private boolean isBalanceHand; // ok private int numberOfTrump; // ok private int numberOfTrumpExist; // private boolean[] rightOpCutSuit = new boolean[4]; // private boolean[] frontOpCutSuit = new boolean[4]; // private boolean[] leftOpCutSuit = new boolean[4]; // private int openingBet; // private int respondingBet; // private boolean gcCaution; // private boolean coatCaution; // private static CardsInHand card; }
A Class human which set human attributes and playing function
package testing; import java.awt.event.*; import javax.swing.JLabel; public class Human extends Players{ @Override public void play(){ synchronized(Deck.getPlayers()[0]){ try{ Deck.getPlayers()[0].wait();} catch(InterruptedException exc){ // }} for(int i=0 ; i <13;i++){ getJLabels()[i].addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { int counter = evt.getClickCount(); int Index = getIndex(getJLabels(), (JLabel)evt.getSource()); getJLabels()[Index].setIcon(getHand()[Index].getBackSide()); getPlayerJLabel().setIcon(getHand()[Index].getCardImage()); setObjectForPlay(getHand()[Index]); } }); } temp = getObjectForPlay(); } static int getIndex(JLabel[] arr, JLabel obj){ int index=0; for(JLabel cmp: arr){ if(cmp == obj) break; index++; } return index; } }
A Class Called Deck Class Which Creates 4 Players in which 4th Player will be a Hu
package testing; import javax.swing.JLabel; public class Deck { public Deck(){ card = new CardsInHand(); for(int i=0;i<3;i++){ Player[i] = new Players(); Player[i].setHand(); } Player[3] = new Human(); Player[3].setHand(); Player[0].setJLabels(60, 20,1); Player[1].setJLabels(30, 130,2); Player[2].setJLabels(480, 130,2); Player[3].setJLabels(60, 600,1); Players.setMainDeck(); for(int i = 0;i<4;i++){ Player[i].setPlayerJLabel(i); } } public static void startGame(){ Deck dock = new Deck(); for(int j=0;j<13;j++) for(int i=3;i>=0;i--){ Player[i].play(); } } /* public static void deckResult(){ for(Cards x : deck) if(x.getColorName() != currentColor){ if(x.getisTrump()) x.setTrumpCard(x.getCardValue()); else x.setIsDiscard(); } }*/ public static Players[] getPlayers(){ return Player; } public static void setIsTrump(int Color){ for(Cards x: Player[0].getHand()){ if (x.getColor() == Color ) x.setIsTrump(); } for(Cards x: Player[1].getHand()){ if (x.getColor() == Color ) x.setIsTrump(); } for(Cards x: Player[2].getHand()){ if (x.getColor() == Color ) x.setIsTrump(); } for(Cards x: Player[3].getHand()){ if (x.getColor() == Color ) x.setIsTrump(); } } private static CardsInHand card; private static int currentColor; private static Players[] Player = new Players[4]; }
The Frame Class Is
package testing; import javax.swing.JFrame; import javax.swing.JLayeredPane; import javax.swing.JLabel; import java.awt.BorderLayout; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.BoxLayout; import java.awt.event.*; public class ShowCards { private JFrame frame = new JFrame(); private static JLayeredPane lpane = new JLayeredPane(); private static JButton button = new JButton("Start GAME!!"); public ShowCards() { frame.setPreferredSize(new Dimension(1200, 740)); frame.setLayout(new BorderLayout()); frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.LINE_AXIS)); frame.add(lpane, BorderLayout.CENTER); frame.add(button); lpane.add(button, new Integer(14), 0); button.setBounds(1060, 620, 100, 70); button.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { buttonActionPerformed(evt); } }); lpane.setBounds(30, 30, 270, 270); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } private void buttonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: Deck.startGame(); } public static JLayeredPane getlpane(){ return lpane; } }
And Finally The Main Class is ..
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package testing; /** * * @author Dell */ public class MainClass extends javax.swing.JFrame { /** * Creates new form MainClass */ public MainClass() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { jButton1 = new javax.swing.JButton(); jLabel1 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/testing/Cards/Backside.jpg"))); // NOI18N jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); jLabel1.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N jLabel1.setText("Press To DEAL!"); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addGap(173, 173, 173) .addComponent(jButton1)) .addGroup(layout.createSequentialGroup() .addGap(198, 198, 198) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))) .addContainerGap(466, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(268, Short.MAX_VALUE) .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1) .addGap(24, 24, 24)) ); pack(); }// </editor-fold> private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: ShowCards fahad = new ShowCards(); } /** * @param args the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) "> /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(MainClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(MainClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(MainClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(MainClass.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new MainClass().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JButton jButton1; private javax.swing.JLabel jLabel1; // End of variables declaration }
Sorry for very very long post. the problem is here the Player4 means Player[3] has first turn. when i run the programme all robots players Player[0] Player[1] and Player[2] start their funtions of Play before me. i only want that they will wait when i clicked on card then they will through their cards after me.
Thanks in advance