Hey, I'm trying to use a hashtable to determine which player has won, but seems like its not working.
My guess is that im using "player1.containsKey(1)" incorrectly. I figured that each button is linked to a number from 1 to 9 and so each time a button is clicked, its stored in that players hashtable, then it checks if that hash table contains button 1,2,3 and all the other combinations. Any advise? thanks.
class EnlargeListener implements ActionListener { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); int xxx = 1; while(xxx <=9){ if(source == btn[xxx] && turn < 10) { if ((turn % 2 == 0)){ btn[xxx].setIcon(new ImageIcon(imagee)); player1.put(btn[xxx], btn[xxx]); btn[xxx].setEnabled(false); if(player1.containsKey(1) && player1.containsKey(2) && player1.containsKey(3)){ JOptionPane.showMessageDialog(null, "Player one won!"); } if(player1.containsKey(1) && player1.containsKey(4) && player1.containsKey(7)){ JOptionPane.showMessageDialog(null, "Player one won!"); } if(player1.containsKey(1) && player1.containsKey(5) && player1.containsKey(9)){ JOptionPane.showMessageDialog(null, "Player one won!"); } } else { btn[xxx].setIcon(new ImageIcon(image)); player2.put(btn[xxx], btn[xxx]); btn[xxx].setEnabled(false); } turn++; } xxx++; } } }
full source code
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Hashtable; public class mouse extends JFrame { private ImageIcon imagei = new ImageIcon("C:/Users/chris/Desktop/x.JPG"); private ImageIcon imageii = new ImageIcon("C:/Users/chris/Desktop/circle.JPG"); Image image = imagei.getImage(); Image imagee = imageii.getImage(); JButton btn[] = new JButton[10]; boolean btnEmptyClicked = false; int turn = 1; Hashtable player1= new Hashtable(); Hashtable player2= new Hashtable(); public mouse() { JPanel p1 = new JPanel(); // Use the panel to group buttons p1.setLayout(new GridLayout(3,3)); // JPanel p2 = new JPanel(); // p2.add(new JTextField("test"),BorderLayout.SOUTH); // p2.add(p1,BorderLayout.CENTER); add(p2); p2.setLayout(new GridLayout(3, 3)); p2.setBackground(Color.black); for(int i=1; i<=9; i++) { btn[i] = new JButton(); btn[i].setBackground(Color.BLACK); btn[i].addActionListener(new EnlargeListener()); p2.add(btn[i],BorderLayout.CENTER); } } /** Main method */ public static void main(String[] args) { JFrame frame = new mouse(); frame.setTitle("ControlCircle2"); frame.setLocationRelativeTo(null); // Center the frame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setVisible(true); } class EnlargeListener implements ActionListener { public void actionPerformed(ActionEvent e) { Object source = e.getSource(); int xxx = 1; while(xxx <=9){ if(source == btn[xxx] && turn < 10) { if ((turn % 2 == 0)){ btn[xxx].setIcon(new ImageIcon(imagee)); player1.put(btn[xxx], btn[xxx]); btn[xxx].setEnabled(false); if(player1.containsKey(1) && player1.containsKey(2) && player1.containsKey(3)){ JOptionPane.showMessageDialog(null, "Player one won!"); } if(player1.containsKey(1) && player1.containsKey(4) && player1.containsKey(7)){ JOptionPane.showMessageDialog(null, "Player one won!"); } if(player1.containsKey(1) && player1.containsKey(5) && player1.containsKey(9)){ JOptionPane.showMessageDialog(null, "Player one won!"); } } else { btn[xxx].setIcon(new ImageIcon(image)); player2.put(btn[xxx], btn[xxx]); btn[xxx].setEnabled(false); } turn++; } xxx++; } } } }