Results 1 to 8 of 8
Like Tree2Likes
  • 1 Post By DarrylBurke
  • 1 Post By CustomException

Thread: I can't get my tic tac toe program to call the winner or draw

  1. #1
    imckinn40 is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default I can't get my tic tac toe program to call the winner or draw

    I have everything working but it only will call a winner but I can't get to call a draw. The text box should be blank until someone win or if there's a draw. This is what I have:

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Random;
    
    public class TicTacToe extends JPanel implements ActionListener {
    
    	JButton btnButton1, btnButton2, btnButton3, btnButton4, btnButton5, btnButton6, btnButton7,btnButton8, btnButton9;
    	JPanel panel1, panel2, panel3;
    	JTextField txtMove=new JTextField(10);
    	int turn =1; // 1 is O 2 X
    	int cell1, cell2, cell3, cell4, cell5, cell6, cell7, cell8, cell9;
    	int drawcount;
    
    
    	public TicTacToe() {
    
    		setLayout(new GridLayout(3,3));
    		panel1= new JPanel();
    		panel2= new JPanel();
    		panel3= new JPanel();
    
    
    		panel1.setLayout(new GridLayout(3,3));
    
    		cell1=cell2=cell3=cell4=cell5=cell6=cell7=cell8=cell9=0;
    
    		btnButton1=new JButton(" ");
    		btnButton2=new JButton(" ");
    		btnButton3=new JButton(" ");
    		btnButton4=new JButton(" ");
    		btnButton5=new JButton(" ");
    		btnButton6=new JButton(" ");
    		btnButton7=new JButton(" ");
    		btnButton8=new JButton(" ");
    		btnButton9=new JButton(" ");
    		btnButton1.setActionCommand("cell1");
    		btnButton2.setActionCommand("cell2");
    		btnButton3.setActionCommand("cell3");
    		btnButton4.setActionCommand("cell4");
    		btnButton5.setActionCommand("cell5");
    		btnButton6.setActionCommand("cell6");
    		btnButton7.setActionCommand("cell7");
    		btnButton8.setActionCommand("cell8");
    		btnButton9.setActionCommand("cell9");
    
    		btnButton1.addActionListener(this);
    		btnButton2.addActionListener(this);
    		btnButton3.addActionListener(this);
    		btnButton4.addActionListener(this);
    		btnButton5.addActionListener(this);
    		btnButton6.addActionListener(this);
    		btnButton7.addActionListener(this);
    		btnButton8.addActionListener(this);
    		btnButton9.addActionListener(this);
    		panel1.add(btnButton1);
    		panel1.add(btnButton2);
    		panel1.add(btnButton3);
    		panel1.add(btnButton4);
    		panel1.add(btnButton5);
    		panel1.add(btnButton6);
    		panel1.add(btnButton7);
    		panel1.add(btnButton8);
    		panel1.add(btnButton9);
    		panel3.add(txtMove);
    		add(panel1);
    		add(panel2);
    		add(panel3);
    		setPreferredSize(new Dimension(300,300));
    	}
    
    
    	public void actionPerformed(ActionEvent event) {
    		System.out.println(event.getActionCommand());
    		String s=event.getActionCommand();
    		String txtCaption=" ";
    		if(turn==1) txtCaption="O";
    		else
    		if(turn==2) txtCaption="X";
    
    
    		if(s=="cell1" && cell1==0) {
    			btnButton1.setText(txtCaption);
    			cell1=turn;
    		}
    		if(s=="cell2" && cell2==0) {
    			btnButton2.setText(txtCaption);
    			cell2=turn;
    		}
    		if(s=="cell3" && cell3==0) {
    			btnButton3.setText(txtCaption);
    			cell3=turn;
    		}
    		if(s=="cell4" && cell4==0) {
    			btnButton4.setText(txtCaption);
    			cell4=turn;
    		}
    		if(s=="cell5" && cell5==0) {
    			btnButton5.setText(txtCaption);
    			cell5=turn;
    		}
    		if(s=="cell6" && cell6==0) {
    			btnButton6.setText(txtCaption);
    			cell6=turn;
    		}
    		if(s=="cell7" && cell7==0) {
    			btnButton7.setText(txtCaption);
    			cell7=turn;
    		}
    		if(s=="cell8" && cell8==0) {
    			btnButton8.setText(txtCaption);
    			cell8=turn;
    		}
    		if(s=="cell9" && cell9==0) {
    			btnButton9.setText(txtCaption);
    			cell9=turn;
    		}
    
    		if(turn==1) turn=2;
    		else
    		if(turn==2) turn=1;
    
    		if(cell1==1 && cell2==1 && cell3==1) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell4==1 && cell5==1 && cell6==1) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell7==1 && cell8==1 && cell9==1) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell1==1 && cell4==1 && cell7==1) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell2==1 && cell5==1 && cell8==1) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell3==1 && cell6==1 && cell9==1) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell1==1 && cell5==1 && cell9==1) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell3==1 && cell5==1 && cell7==1) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell1==2 && cell2==2 && cell3==2) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell4==2 && cell5==2 && cell6==2) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell7==2 && cell8==2 && cell9==2) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell1==2 && cell4==2 && cell7==2) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell2==2 && cell5==2 && cell8==2) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell3==2 && cell6==2 && cell9==2) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell1==2 && cell5==2 && cell9==2) {
    			System.out.println("Winner");
    			txtMove.setText("Winner");
    		}
    		if(cell3==2 && cell5==2 && cell7==2) {
    			System.out.println("Winner");
    			txtMove.setText("winner");
    		}
    		else
    		System.out.println("Draw");
    
    
    
    
    
    
    
    	}
    }
    Last edited by imckinn40; 05-05-2012 at 12:59 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I can't get my tic tac toe program to call the winner or draw

    Please check the forum FAQ to see how to edit your original post and add [code] [/code] tags around your code so that it's readable. Also consider using an array or a 2-dimensional array to allow you to shrink your code greatly, and make it easier for you and us to understand and debug your code. And finally, have you added any println statements or other tricks to try to debug your problem, to test the state of your program while it's running to see why nothing is being drawn when you feel that it should be?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: I can't get my tic tac toe program to call the winner or draw

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: I can't get my tic tac toe program to call the winner or draw

    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: I can't get my tic tac toe program to call the winner or draw

    Next we have to fix your use of == to compare Strings, but I need to make sure you're reading and understanding our responses before moving further here. Please respond.

    Edit, I just saw the cross-post information. Never mind. You'll probably get your answer in a cross-post.

  6. #6
    imckinn40 is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: I can't get my tic tac toe program to call the winner or draw

    I'm not sure how to put it in code form and can I change it. The program works. When I run the program, I can input the Xs and Os, it will call the "Winner", but it will not call a "Draw". My teacher wants me to do it this way, I'm not getting why but I'm stuck with this one.

  7. #7
    imckinn40 is offline Member
    Join Date
    May 2012
    Posts
    3
    Rep Power
    0

    Default Re: I can't get my tic tac toe program to call the winner or draw

    The cross post was me also I didn't get the help I was looking for.

  8. #8
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: I can't get my tic tac toe program to call the winner or draw

    Then at least have the courtesy to inform us. It's not fun to spend time trying to help someone with a problem only to find out they've already solved it elsewhere.
    Fubarable likes this.

Similar Threads

  1. Simple draw line program not working
    By forms in forum New To Java
    Replies: 7
    Last Post: 01-24-2012, 04:51 PM
  2. Replies: 7
    Last Post: 01-20-2012, 11:29 AM
  3. Lopping the game for two wins is the winner
    By 54byler in forum Advanced Java
    Replies: 1
    Last Post: 04-22-2009, 02:26 AM
  4. Replies: 2
    Last Post: 04-03-2009, 05:04 AM
  5. I need to call and run another program
    By coco in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2007, 05:47 AM

Posting Permissions

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