Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: trying to use hashtable in tic tac toe game

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default trying to use hashtable in tic tac toe game

    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++;
     }
     
     
     
     
    	}
     
     
     
     }
     
     
    		 }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: trying to use hashtable in tic tac toe game

    using "player1.containsKey(1)" incorrectly
    Did you try writing a 5 line program that adds a key to a Hashtable and then calls containsKey() to see what happens?


    Also printing out the contents of the hashtables will help you see what the program is doing.
     System.out.println("p1="+player1);
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    leonne (January 12th, 2013)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    14
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: trying to use hashtable in tic tac toe game

    dude.....you are passing buttons as key and value....but searching for integer...
    also....i don't think while loop here can give you desired results...
    btw...why is panel p1 used??

  5. The Following User Says Thank You to rougeking For This Useful Post:

    leonne (January 12th, 2013)

  6. #4
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: trying to use hashtable in tic tac toe game

    o ok thank for the info, o , I don't think p1 is used, originally the code was something else and I just changed it.
    Also while loops seems fine lets me select the 9 button and wont let me pick the same one more then once and stops after all 9 are pressed

  7. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: trying to use hashtable in tic tac toe game

    Have you made any progress solving the problem and getting the program to work the way you want?
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    leonne (January 12th, 2013)

  9. #6
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: trying to use hashtable in tic tac toe game

    Hey, yea I think I have. What I did now was to change player1.put(btn[xxx], btn[xxx]); to player1.put(xxx, xxx); and seems to work. The message pops up when circle has selects the top 3 buttons
    thanks

  10. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: trying to use hashtable in tic tac toe game

    Why are you using a hashtable? You never use the saved value.
    For something as simple as tictactoe, an array of binary values would work.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #8
    Member
    Join Date
    Oct 2012
    Posts
    44
    Thanks
    9
    Thanked 2 Times in 1 Post

    Default Re: trying to use hashtable in tic tac toe game

    O ill try the other way out too, could not think of how to check who won and I remembered using hashtables in class to store stuff so I figured ill use that.
    thanks

Similar Threads

  1. Need help with tic tac toe game
    By ogpg2006 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 2nd, 2012, 07:34 AM
  2. (Beginner/Intermediate) Tic Tac Toe game issues
    By Sylentwolf8 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 17th, 2012, 08:03 AM
  3. tic tac toe game problem with X and O
    By woohooXX in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 20th, 2011, 07:34 AM
  4. TIC-TAC-TOE GAME
    By umerahmad in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 29th, 2011, 12:15 PM
  5. [NEED HELP] to clear result on my tic-tac-toe game
    By bontet in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 28th, 2010, 03:50 PM