import java.awt.*; import java.awt.event.*; import java.net.URL; import java.util.Random; import javax.swing.*; public class Poker extends JApplet implements ActionListener { JLabel blah; JLabel nextLabel; Card[] cards; JButton[] cardButtons; int cardsLeft; int nextCard; Card[] deck; JTextArea[] rowHands; JTextArea[] colHands; int score; public void init() { shuffle(); cards = new Card[25]; cardButtons = new JButton[25]; rowHands = new JTextArea[5]; colHands = new JTextArea[5]; cardsLeft = 25; nextCard = 0; score = 0; blah = new JLabel("Next card: "); nextLabel = new JLabel(); nextLabel.setHorizontalTextPosition(SwingConstants.LEADING); printCard(nextLabel, deck[nextCard]); JPanel nextPanel = new JPanel(); nextPanel.add(blah); nextPanel.add(nextLabel); JPanel cardPanel = new JPanel(new GridLayout(6, 6)); for (int i=0; i<5; i++) { for (int j=0; j<5; j++) { cardButtons[5*i+j] = new JButton(); cardButtons[5*i+j].setHorizontalTextPosition(SwingConstants.LEADING); cardButtons[5*i+j].setMargin(new Insets(0,0,0,0)); cardButtons[5*i+j].addActionListener(this); cardPanel.add(cardButtons[5*i+j]); } rowHands[i] = new JTextArea(); rowHands[i].setEditable(false); rowHands[i].setBackground(blah.getBackground()); cardPanel.add(rowHands[i]); } for (int i=0; i<5; i++) { colHands[i] = new JTextArea(); colHands[i].setEditable(false); colHands[i].setBackground(blah.getBackground()); cardPanel.add(colHands[i]); } Container pane = getContentPane(); pane.add(cardPanel); pane.add(nextPanel, BorderLayout.NORTH); } public void actionPerformed(ActionEvent e) { for (int i=0; i<25; i++) { if (e.getSource() == cardButtons[i]) { if (cards[i] == null) { printCard(cardButtons[i], deck[nextCard]); cards[i] = deck[nextCard]; nextCard++; cardsLeft--; if (cardsLeft == 0) { nextLabel.setText(""); score(); blah.setText("Score: "); nextLabel.setForeground(Color.blue); nextLabel.setText(String.valueOf(score)); nextLabel.setIcon(null); } else printCard(nextLabel, deck[nextCard]); } } } } void score() { Card[] curcards = new Card[5]; // rows for (int i=0; i<5; i++) { for (int j=0; j<5; j++) { curcards[j] = cards[5*i+j]; } score(curcards, rowHands[i]); } // cols for (int i=0; i<5; i++) { for (int j=0; j<5; j++) { curcards[j] = cards[5*j+i]; } score(curcards, colHands[i]); } } void score(Card[] cards, JTextArea label) { // flush boolean flush = true; String suit = cards[0].suit; for (int i=1; i<5; i++) { if (!cards[i].suit.equals(suit)) { flush = false; break; } } int[] nums = new int[5]; for (int i=0; i<5; i++) nums[i] = cards[i].num % 13; // selection sort for (int i=0; i<4; i++) { int min = i; for (int j=i; j<5; j++) { if (nums[j] < nums[min]) min = j; } int temp = nums[i]; nums[i] = nums[min]; nums[min] = temp; } // four boolean fourKind = false; for (int i=0; i<2; i++) { int match = nums[i]; fourKind = true; for (int j=i; j