Results 1 to 8 of 8
- 05-05-2012, 12:00 AM #1
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
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.
-
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?
- 05-05-2012, 12:15 AM #3
Re: I can't get my tic tac toe program to call the winner or draw
Why do they call it rush hour when nothing moves? - Robin Williams
- 05-05-2012, 12:20 AM #4
Re: I can't get my tic tac toe program to call the winner or draw
Why do they call it rush hour when nothing moves? - Robin Williams
-
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.
- 05-05-2012, 12:39 AM #6
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
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.
- 05-05-2012, 01:19 AM #7
Member
- Join Date
- May 2012
- Posts
- 3
- Rep Power
- 0
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.
- 05-06-2012, 10:16 AM #8
Member
- Join Date
- May 2012
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Simple draw line program not working
By forms in forum New To JavaReplies: 7Last Post: 01-24-2012, 04:51 PM -
How to call methods in a for loop-- trying to draw a string to count each buton click
By jeff30 in forum New To JavaReplies: 7Last Post: 01-20-2012, 11:29 AM -
Lopping the game for two wins is the winner
By 54byler in forum Advanced JavaReplies: 1Last Post: 04-22-2009, 02:26 AM -
Best way to draw to screen & call game update method?
By dwfait in forum Java AppletsReplies: 2Last Post: 04-03-2009, 05:04 AM -
I need to call and run another program
By coco in forum Advanced JavaReplies: 1Last Post: 07-31-2007, 05:47 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks