Results 1 to 5 of 5
  1. #1
    jimmycool44 is offline Member
    Join Date
    Sep 2013
    Posts
    9
    Rep Power
    0

    Default a bit of help please D:

    so,i tought i would make a tic-tac-toe game,i did it pretty much...
    the problem is,the if statement that checks if somebody wins doesnt have any functions,so even if you win/lose/tie nothing happens,i would want it to be a popup window or just the whole thing changing color and having some text and a play again button,here is my code so far...

    Java Code:
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    import javax.swing.JTextField;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.Color;
    import java.awt.GridLayout;
    public class MiniGameClass extends JFrame{
    	private boolean xwin,owin,tie;
    	private int whosgo = 0;
    	private JButton button,button1,button2,button3,button4,button5,button6,button7,button8;
    	public MiniGameClass(){
    		setLayout(new GridLayout(3,3));
    		
    		button = new JButton("");
    		add(button);
    		
    		button1 = new JButton("");
    		add(button1);
    		
    		button2 = new JButton("");
    		add(button2);
    		
    		button3 = new JButton("");
    		add(button3);
    		
    		button4 = new JButton("");
    		add(button4);
    		
    		button5 = new JButton("");
    		add(button5);
    		
    		button6 = new JButton("");
    		add(button6);
    		
    		button7 = new JButton("");
    		add(button7);
    		
    		button8 = new JButton("");
    		add(button8);
    		
    		
    		event e = new event();
    		button.addActionListener(e);
    		
    		event1 e1 = new event1();
    		button1.addActionListener(e1);
    		
    		event2 e2 = new event2();
    		button2.addActionListener(e2);
    		
    		event3 e3 = new event3();
    		button3.addActionListener(e3);
    		
    		event4 e4 = new event4();
    		button4.addActionListener(e4);
    		
    		event5 e5 = new event5();
    		button5.addActionListener(e5);
    		
    		event6 e6 = new event6();
    		button6.addActionListener(e6);
    		
    		event7 e7 = new event7();
    		button7.addActionListener(e7);
    		
    		event8 e8 = new event8();
    		button8.addActionListener(e8);
    		
    	}
    	
    	public class event implements ActionListener{
    		public void actionPerformed(ActionEvent e){
    			switch (whosgo){
    				case 0:
    					button.setText("X");
    					button.setEnabled(false);
    					whosgo++;
    					break;
    				case 1:
    					button.setText("O");
    					button.setEnabled(false);
    					whosgo--;
    					break;
    			}
    		}
    	}
    	
    	public class event1 implements ActionListener{
    		public void actionPerformed(ActionEvent e1){
    			switch(whosgo){
    				case 0:
    					button1.setText("X");
    					button1.setEnabled(false);
    					whosgo++;
    					break;
    				case 1:
    					button1.setText("O");
    					button1.setEnabled(false);
    					whosgo--;
    					break;
    			}
    		}
    	}
    	
    	public class event2 implements ActionListener{
    		public void actionPerformed(ActionEvent e2){
    			switch(whosgo){
    				case 0:
    					button2.setText("X");
    					button2.setEnabled(false);
    					whosgo++;
    					break;
    				case 1:
    					button2.setText("O");
    					button2.setEnabled(false);
    					whosgo--;
    					break;
    				
    			}
    		}
    	}
    	
    	public class event3 implements ActionListener{
    		public void actionPerformed(ActionEvent e3){
    			switch(whosgo){
    				case 0:
    					button3.setText("X");
    					button3.setEnabled(false);
    					whosgo++;
    					break;
    				case 1:
    					button3.setText("O");
    					button3.setEnabled(false);
    					whosgo--;
    					break;
    				
    			}
    		}
    	}
    	
    	public class event4 implements ActionListener{
    		public void actionPerformed(ActionEvent e4){
    			switch(whosgo){
    				case 0:
    					button4.setText("X");
    					button4.setEnabled(false);
    					whosgo++;
    					break;
    				case 1:
    					button4.setText("O");
    					button4.setEnabled(false);
    					whosgo--;
    					break;
    				
    			}
    		}
    	}
    	
    	public class event5 implements ActionListener{
    		public void actionPerformed(ActionEvent e5){
    			switch(whosgo){
    				case 0:
    					button5.setText("X");
    					button5.setEnabled(false);
    					whosgo++;
    					break;
    				case 1:
    					button5.setText("O");
    					button5.setEnabled(false);
    					whosgo--;
    					break;
    				
    			}
    		}
    	}
    	
    	public class event6 implements ActionListener{
    		public void actionPerformed(ActionEvent e6){
    			switch(whosgo){
    				case 0:
    					button6.setText("X");
    					button6.setEnabled(false);
    					whosgo++;
    					break;
    				case 1:
    					button6.setText("O");
    					button6.setEnabled(false);
    					whosgo--;
    					break;
    				
    			}
    		}
    	}
    	
    	public class event7 implements ActionListener{
    		public void actionPerformed(ActionEvent e7){
    			switch(whosgo){
    				case 0:
    					button7.setText("X");
    					button7.setEnabled(false);
    					whosgo++;
    					break;
    				case 1:
    					button7.setText("O");
    					button7.setEnabled(false);
    					whosgo--;
    					break;
    				
    			}
    		}
    	}
    	
    	public class event8 implements ActionListener{
    		public void actionPerformed(ActionEvent e8){
    			switch(whosgo){
    				case 0:
    					button8.setText("X");
    					button8.setEnabled(false);
    					whosgo++;
    					break;
    				case 1:
    					button8.setText("O");
    					button8.setEnabled(false);
    					whosgo--;
    					break;
    				
    			}
    		}
    	}
    	
    	public void wincheck(){
    		if(button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() == "X"){
    			
    		}else if(button.getText() == button2.getText() && button1.getText() == button2.getText() && button.getText() == "O"){
    			
    		}else if (button3.getText() == button4.getText() && button5.getText() == button3.getText() && button3.getText() == "X"){
    			
    		}else if (button3.getText() == button4.getText() && button5.getText() == button3.getText() && button3.getText() == "Y"){
    			
    		}else if (button6.getText() == button7.getText() && button8.getText() == button6.getText() && button6.getText() == "X"){
    			
    		}else if(button6.getText() == button7.getText() && button8.getText() == button6.getText() && button6.getText() == "Y"){
    			
    		}else if(button.getText() == button3.getText() && button6.getText() == button.getText() && button.getText() == "X"){
    			
    		}else if(button.getText() == button3.getText() && button6.getText() == button.getText() && button.getText() == "Y"){
    			
    		}else if(button1.getText() == button4.getText() && button7.getText() == button1.getText() && button1.getText() == "X"){
    			
    		}else if(button1.getText() == button4.getText() && button7.getText() == button1.getText() && button1.getText() == "Y"){
    			
    		}else if(button2.getText() == button5.getText() && button8.getText() == button2.getText() && button2.getText() == "X"){
    			
    		}else if(button2.getText() == button5.getText() && button8.getText() == button2.getText() && button2.getText() == "Y"){
    			
    		}else if(button.getText() == button4.getText() && button8.getText() == button.getText() && button.getText() == "X"){
    			
    		}else if(button.getText() == button4.getText() && button8.getText() == button.getText() && button.getText() == "Y"){
    			
    		}else if(button2.getText() == button4.getText() && button6.getText() == button2.getText() && button2.getText() == "X"){
    			
    		}else if(button2.getText() == button4.getText() && button6.getText() == button2.getText() && button2.getText() == "Y"){
    			
    		}else{
    			
    		}
    	}
    	
    	
    	public static void main (String [] args){
    		MiniGameClass gui = new MiniGameClass();
    		gui.setVisible(true);
    		gui.setSize(300,300);
    		gui.setTitle("Tic-Tac-Toe");
    		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	}
    
    }
    help is much appreciated

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: a bit of help please D:

    Moved to the main Eclipse forum from a staff only section.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,368
    Rep Power
    6

    Default Re: a bit of help please D:

    (but why is this an Eclipse question? The Swing forum would be more appropriate wouldn't it?)

    button1.getText() == "X"
    This won't work (reliably). You use the String equals() method to compare it to another String.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: a bit of help please D:

    Sorry, didn't actually read the question...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,039
    Rep Power
    6

    Default Re: a bit of help please D:

    There is nothing wrong with the way you did your actionListeners but in the future you may want to consider the following:

    Java Code:
    public class event implements ActionListener{
       public void actionPerformed(ActionEvent e){
         JButton button = (JButton)e.getSource();
         switch(whosgo) {
               case 0:
                     button.setText("X");
                     button.setEnabled(false);
                     whosgo++;
                     break;
               case 1:
                     button.setText("O");
                     button.setEnabled(false);
                     whosgo--;
                     break;
          }
       }
    }
    Now you can use the same instance of the event listener for all of your buttons since the source of the event is retrieved from
    the ActionEvent object itself instead of having separate event handlers for each button. It basically makes your program a little smaller and requires less typing.

    Regards,
    Jim
    Last edited by jim829; 10-28-2013 at 09:17 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •