Note that there are some explanatory texts on larger screens.

plurals
  1. POPlaying Logic about Card Game Bridge
    text
    copied!<p>i have have design a class whos constructor produce 52 Cards object each orject has its own color, value, isTrump(boolean) and imageIcon which store image of each card. now there is another class called Que who stores all the objects of cards into a que and shuffle them. there is a method which deals the cards object among 4 players into array of object(Cards). now i used LayeredPane and Jlabels to show them and a mouse-listener also attached with each.</p> <p>Now i only want a tip how can i start each trick. means i place one card then three other robots placed there cards automatically after analysing my card on the bases of some predefined rules. please any suggestions</p> <pre><code>public class Cards { public Cards(int a, int b){ this.cardvalue = a; this.color = b; this.isTrump = 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 isTrump(){ 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; } 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 ImageIcon backSide = new ImageIcon(getClass().getResource("/testing/Cards/Backside.jpg")); } </code></pre> <p>Class which deal the card is </p> <pre><code>public class CardsInHand { public CardsInHand(){ totalCards = new Que(52); int a =1; int b=2; for (int j = 0;j&lt;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(){ card = new CardsInHand(); setPlayers(); } private static void setPlayers(){ for(int i =0;i&lt;13;i++){ Hands[0][i] = totalCards.get(); Hands[1][i] = totalCards.get(); Hands[2][i] = totalCards.get(); Hands[3][i] = totalCards.get(); } sortingarr(); } private static void Sorting(Cards arr[]){ //here some Sorting algorithm i used } private static void sortingarr(){ Sorting(Hands[0]); Sorting(Hands[1]); Sorting(Hands[2]); Sorting(Hands[3]); NumberSorting(Hands[0]); NumberSorting(Hands[1]); NumberSorting(Hands[2]); NumberSorting(Hands[3]); } private static void NumberSorting(Cards arr[]){ //some algorith i used for number sorting } public static Cards[] getPlayer1(){ return Hands[0]; } public static Cards[] getPlayer2(){ return Hands[1]; } public static Cards[] getPlayer3(){ return Hands[2]; } public static Cards[] getPlayer4(){ return Hands[3]; } public static Cards[][] getAllHands(){ return Hands; } private final static Cards[] Player1 = new Cards[13]; private final static Cards[] Player2 = new Cards[13]; private final static Cards[] Player3 = new Cards[13]; private final static Cards[] Player4 = new Cards[13]; private final static Cards[][] Hands = {Player1,Player2,Player3,Player4}; private static Que totalCards; private static CardsInHand card; } </code></pre> <p>the class which is showing the cards after dealing is </p> <pre><code>public class ShowCards { private JFrame frame = new JFrame(); private static JLayeredPane lpane = new JLayeredPane(); private static JLabel[] Player1 = new JLabel[13]; private static JLabel[] Player2 = new JLabel[13]; private static JLabel[] Player3 = new JLabel[13]; private static JLabel[] Player4 = new JLabel[13]; private static JButton button = new JButton("Deal Again"); public ShowCards() { CardsInHand.Deal(); frame.setPreferredSize(new Dimension(800, 640)); 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(200, 250, 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); cardDeal(); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } static void cardDeal(){ loadJLabels1(); loadJLabels2(); loadJLabels3(); loadJLabels4(); } static void loadJLabels1(){ int k = 30; for(int i=0;i&lt;13;i++){ Player1[i] = new JLabel(); Player1[i].setIcon(CardsInHand.getPlayer1()[i].getCardImage()); Player1[i].setBounds(k, 20, 170, 100); Player1[i].setOpaque(true); Player1[i].addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { int count = evt.getClickCount(); if (count == 1){ int Index = getIndex(Player1, (JLabel)evt.getSource()); Player1[Index].setIcon(CardsInHand.getPlayer1()[Index].getBackSide()); } } }); lpane.add(Player1[i], new Integer(i), 0); k = k+30; } } static void loadJLabels2(){ int k = 140; for(int i=0;i&lt;13;i++){ Player2[i] = new JLabel(); Player2[i].setIcon(CardsInHand.getPlayer2()[i].getCardImage()); Player2[i].setBounds(30, k, 170, 100); Player2[i].setOpaque(true); Player2[i].addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { int count = evt.getClickCount(); if (count == 1){ int Index = getIndex(Player2, (JLabel)evt.getSource()); Player2[Index].setIcon(CardsInHand.getPlayer2()[Index].getBackSide()); } } }); lpane.add(Player2[i], new Integer(i), 0); k = k+20; } } static void loadJLabels3(){ int k = 140; for(int i=0;i&lt;13;i++){ Player3[i] = new JLabel(); Player3[i].setIcon(CardsInHand.getPlayer3()[i].getCardImage()); Player3[i].setBounds(400, k, 170, 100); Player3[i].setOpaque(true); Player3[i].addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { int count = evt.getClickCount(); if (count == 1){ int Index = getIndex(Player3, (JLabel)evt.getSource()); Player3[Index].setIcon(CardsInHand.getPlayer2()[Index].getBackSide()); } } }); lpane.add(Player3[i], new Integer(i), 0); k = k+20; } } static void loadJLabels4(){ int k = 30; for(int i=0;i&lt;13;i++){ Player4[i] = new JLabel(); Player4[i].setIcon(CardsInHand.getPlayer4()[i].getCardImage()); Player4[i].setBounds(k, 500, 170, 100); Player4[i].setOpaque(true); Player4[i].addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent evt) { int count = evt.getClickCount(); if (count == 1){ int Index = getIndex(Player4, (JLabel)evt.getSource()); Player4[Index].setIcon(CardsInHand.getPlayer2()[Index].getBackSide()); } } }); lpane.add(Player4[i], new Integer(i), 0); k = k+30; } } private void buttonActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: CardsInHand.Deal(); cardDeal(); } static int getIndex(JLabel[] arr, JLabel obj){ int index=0; for(JLabel cmp: arr){ if(cmp == obj) break; index++; } return index; } } </code></pre> <p>Now i also creat a player class which creat three Bots to play and one will be the human. the class is as</p> <pre><code>public class Players { public Players(Cards[] arr){ 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); for(int i=0;i&lt;13;i++){ this.remainingSpade[i] = true; this.remainingHeart[i] = true; this.remainingClub[i] = true; this.remainingDiamond[i] = true; } for(int i=0;i&lt;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; this.trickStarter = false; } public static void createRobot(){ CardsInHand.Deal(); for(int i=0; i&lt;3;i++){ Robots[i] = new Players(CardsInHand.getAllHands()[i+1]); } } private int getScoreInHand(Cards[] arr){ 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&lt;4;i++){ if(max&lt;temp[i]){ max=temp[i]; strong = i; } } return strong+1; } private int getWeakestSuit(Cards[] arr){ 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&lt;4;i++){ if(min&gt;temp[i]){ min=temp[i]; weak = i; } } return weak+1; } private int getLongestSuit(Cards[] arr){ int Longest = 0; int[] temp = new int[4]; getCardOfEachSuit(arr, temp); int max = temp[0]; for(int i=0;i&lt;4;i++){ if(max&lt;temp[i]){ max=temp[i]; Longest = i; } } return Longest+1; } private int getSmallestSuit(Cards[] arr){ int Smallest = 0; int[] temp = new int[4]; getCardOfEachSuit(arr, temp); int max = temp[0]; for(int i=0;i&lt;4;i++){ if(max&gt;temp[i]){ max=temp[i]; Smallest = i; } } return Smallest+1; } private boolean getIsBalanceHand(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&lt;=4&amp;&amp;S&gt;=3)&amp;&amp;(H&lt;=4&amp;&amp;H&gt;=3)&amp;&amp;(D&lt;=4&amp;&amp;D&gt;=3)&amp;&amp;(C&lt;=4&amp;&amp;C&gt;=3)) return true; else return false; } private void getCardOfEachSuit(Cards[] arr, int[] array){ 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){ int temp = 0; for(Cards x: arr){ if(x.getisTrump() == true) temp = temp+1; } return temp; } public void setTrickStarter(boolean a){ this.trickStarter = a; } public boolean getTrickStarter(){ return this.trickStarter; } private static Players[] Robots = new Players[3]; // 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 boolean trickStarter; } </code></pre> <p>Now please guide me little forward what is can do to start playing. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload