Results 1 to 9 of 9
Thread: Tic Tac Toe Issue
- 08-12-2011, 06:01 AM #1
Member
- Join Date
- Aug 2011
- Posts
- 1
- Rep Power
- 0
Tic Tac Toe Issue
Hey all,
I'm currently in the process of making Tic Tac Toe, and most of it is working. The way I wrote it (Warning: It's written in a god-awful manage) is with
9 buttons, with the a String called letter alternating between X and O, based on a boolean (switching every ActionPerformed). In the ActionPerformed
section, there's a huge load of if statements accounting for every winning possibility (I know, not the best way to do it).
I also have a boolean which makes sure that the button values are not null (So that it only checks the buttons when they have values). There is also
another boolean called 'Win', initially set to false, but when the winning condition is met, it is said to true. When 'Win' is true, then a dialogue box pops up saying 'lettervalue' + "Wins!".
The problem is that, although most of the winning combinations work, some of them do not, and the 'letterValue' stays as a blank string, so it just
comes up as ' Wins!'. After a few hours of debugging, a friend of mine and I managed to figure out that the string was being set before the
Action Performed, so it's reading the blank button.
The combinations that still do not work (which are the last two sets of if statements):
Row1Column1 + Row1Column2 + Row1Column3
Row1Column3 + Row2Column3 + Row3Column3
I've tried to fix it, but no luck, so I need some advice...
XML Code://Credit to: http://forum.codecall.net for inspiration import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.applet.*; public class TicTacToeMain extends Applet implements ActionListener { boolean IfNull = true; private JButton Row1Column1 = new JButton(""); //Column 1 private JButton Row1Column2 = new JButton(""); private JButton Row1Column3 = new JButton(""); private JButton Row2Column1 = new JButton(""); //Column 2 private JButton Row2Column2 = new JButton(""); private JButton Row2Column3 = new JButton(""); private JButton Row3Column1 = new JButton(""); //Column 3 private JButton Row3Column2 = new JButton(""); private JButton Row3Column3 = new JButton(""); private JFrame MyWindow = new JFrame("Tic-Tac-Toe"); private boolean XTurn; private boolean Win = false; String WinnerString = ""; private boolean Tie = false; private String letterValue = ""; public void init() { Color white = new Color(255,255,255); MyWindow.setBackground(white); MyWindow.setLayout(new GridLayout(4,3)); MyWindow.setSize(500,500); MyWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MyWindow.add(Row1Column1); Row1Column1.addActionListener(this); MyWindow.add(Row1Column2); Row1Column2.addActionListener(this); MyWindow.add(Row1Column3); Row1Column3.addActionListener(this); MyWindow.add(Row2Column1); Row2Column1.addActionListener(this); MyWindow.add(Row2Column2); Row2Column2.addActionListener(this); MyWindow.add(Row2Column3); Row2Column3.addActionListener(this); MyWindow.add(Row3Column1); Row3Column1.addActionListener(this); MyWindow.add(Row3Column2); Row3Column2.addActionListener(this); MyWindow.add(Row3Column3); Row3Column3.addActionListener(this); MyWindow.setVisible(true); XTurn = true; } public void actionPerformed(ActionEvent e) { System.out.println(Row2Column3.getText() + "."); if(XTurn == true) { letterValue = "X"; } else if(XTurn == false) { letterValue = "O"; } if(e.getSource() == Row1Column1) { Row1Column1.setText(letterValue); Row1Column1.setEnabled(false); if(XTurn == true) { XTurn = false; } else if (XTurn == false) { XTurn = true; } } else if(e.getSource() == Row1Column2) { Row1Column2.setText(letterValue); Row1Column2.setEnabled(false); if(XTurn == true) { XTurn = false; } else if (XTurn == false) { XTurn = true; } } else if(e.getSource() == Row1Column3) { Row1Column3.setText(letterValue); Row1Column3.setEnabled(false); if(XTurn == true) { XTurn = false; } else if (XTurn == false) { XTurn = true; } } else if(e.getSource() == Row2Column1) { Row2Column1.setText(letterValue); Row2Column1.setEnabled(false); if(XTurn == true) { XTurn = false; } else if (XTurn == false) { XTurn = true; } } else if(e.getSource() == Row2Column2) { Row2Column2.setText(letterValue); Row2Column2.setEnabled(false); if(XTurn == true) { XTurn = false; } else if (XTurn == false) { XTurn = true; } } else if(e.getSource() == Row2Column3) { Row2Column3.setText(letterValue); Row2Column3.setEnabled(false); if(XTurn == true) { XTurn = false; } else if (XTurn == false) { XTurn = true; } } else if(e.getSource() == Row3Column1) { Row3Column1.setText(letterValue); Row3Column1.setEnabled(false); if(XTurn == true) { XTurn = false; } else if (XTurn == false) { XTurn = true; } } else if(e.getSource() == Row3Column2) { Row3Column2.setText(letterValue); Row3Column2.setEnabled(false); if(XTurn == true) { XTurn = false; } else if (XTurn == false) { XTurn = true; } } else if(e.getSource() == Row3Column3) { Row3Column3.setText(letterValue); Row3Column3.setEnabled(false); if(XTurn == true) { XTurn = false; } else if (XTurn == false) { XTurn = true; } } //Win Possibilities if(Row1Column1.getText() != "" && Row1Column2.getText() != "" && Row1Column3.getText() != "" || Row2Column1.getText() != "" && Row2Column2.getText() != "" && Row2Column3.getText() != "" || Row3Column1.getText() != "" && Row3Column2.getText() != "" && Row3Column3.getText() != "" || Row1Column1.getText() != "" && Row2Column2.getText() != "" && Row3Column3.getText() != "" || Row1Column3.getText() != "" && Row2Column3.getText() != "" && Row3Column3.getText() != "" || Row1Column2.getText() != "" && Row2Column2.getText() != "" && Row3Column2.getText() != "" || Row1Column3.getText() != "" && Row2Column2.getText() != "" && Row3Column1.getText() != "" || Row1Column1.getText() != "" && Row2Column1.getText() != "" && Row3Column1.getText() != "" || Row1Column3.getText() != "" && Row2Column2.getText() != "" && Row3Column1.getText() != "" || Row1Column3.getText() != "" && Row2Column3.getText() != "" && Row3Column3.getText() != "" || Row3Column1.getText() != "" && Row3Column2.getText() != "" && Row3Column3.getText() != "") { IfNull = false; } CheckForWin(); CheckForTrue(); CheckForTie(); } public void CheckForTrue() { if(Win == true) { if(WinnerString == "") { JOptionPane.showMessageDialog(this, WinnerString + " Wins!" ); } else { PopUpWin(WinnerString); } } } //} public void CheckForTie() { if(Tie == true) { JOptionPane.showMessageDialog(this, "It's a tie!"); } else { } } public void CheckForWin() { if(Row1Column1.getText() == Row1Column2.getText() && Row1Column2.getText() == Row1Column3.getText() && IfNull == false) { WinnerString = Row1Column1.getText(); Win = true; } // else if(Row2Column1.getText() == Row2Column2.getText() && Row2Column2.getText() == Row2Column3.getText() && IfNull == false) { WinnerString = Row2Column1.getText(); Win = true; } // else if (Row3Column1.getText() == Row3Column2.getText() && Row3Column2.getText() == Row3Column3.getText() && IfNull == false) { WinnerString = Row3Column1.getText(); Win = true; } // else if(Row1Column1.getText() == Row2Column1.getText() && Row2Column1.getText() == Row3Column1.getText() && IfNull == false) { WinnerString = Row1Column1.getText(); Win = true; } // else if(Row1Column1.getText() == Row2Column2.getText() && Row2Column2.getText() == Row3Column3.getText() && IfNull == false) { WinnerString = Row1Column1.getText(); Win = true; } // else if(Row1Column2.getText() == Row2Column2.getText() && Row2Column2.getText() == Row3Column2.getText() && IfNull == false) { WinnerString = Row1Column2.getText(); Win = true; } else if(Row1Column3.getText() == Row2Column2.getText() && Row2Column2.getText() == Row3Column1.getText() && IfNull == false) { WinnerString = Row1Column3.getText(); Win = true; } else if(Row1Column3.getText() == Row2Column3.getText() && Row2Column3.getText() == Row3Column3.getText() && IfNull == false) { WinnerString = Row2Column3.getText(); Win = true; } else if (Row3Column1.getText() == Row3Column2.getText() && Row3Column2.getText() == Row3Column3.getText() && IfNull == false) { WinnerString = Row3Column1.getText(); Win = true; } } public void PopUpWin(String i) { JOptionPane.showMessageDialog(this, i + " Wins!"); } }Last edited by Jakesta42; 08-12-2011 at 06:06 AM.
- 08-12-2011, 07:17 AM #2
Get rid of those silly increasing indents.
Code conventions for the Java programming language
db
- 08-12-2011, 02:07 PM #3
Cross posted at Tic Tac Toe ActionPerformed Issue
- 08-12-2011, 03:10 PM #4
Thanks Norm.
db
- 08-16-2011, 11:28 PM #5
I really don't feel like reading through this, but if you wanna take a look at how I did mine and model yours after it feel free to do that.
Also the end() thing is a method I made that just resets everything.Java Code:if(b1.getIcon() == X && b2.getIcon() == X && b3.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end(); }else if(b1.getIcon() == X && b4.getIcon() == X && b7.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end(); }else if(b9.getIcon() == X && b3.getIcon() == X && b6.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end(); }else if(b7.getIcon() == X && b8.getIcon() == X && b9.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end(); }else if(b4.getIcon() == X && b5.getIcon() == X && b6.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end(); }else if(b1.getIcon() == X && b5.getIcon() == X && b9.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end(); }else if(b2.getIcon() == X && b5.getIcon() == X && b8.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end(); }else if(b5.getIcon() == X && b3.getIcon() == X && b7.getIcon() == X){JOptionPane.showMessageDialog(null, "X Wins!"); end(); }else if(b1.getIcon() == O && b2.getIcon() == O && b3.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end(); }else if(b1.getIcon() == O && b4.getIcon() == O && b7.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end(); }else if(b9.getIcon() == O && b3.getIcon() == O && b6.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end(); }else if(b7.getIcon() == O && b8.getIcon() == O && b9.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end(); }else if(b4.getIcon() == O && b5.getIcon() == O && b6.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end(); }else if(b1.getIcon() == O && b5.getIcon() == O && b9.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end(); }else if(b2.getIcon() == O && b5.getIcon() == O && b8.getIcon() == O){JOptionPane.showMessageDialog(null, "O Wins!"); end(); }
- 08-16-2011, 11:33 PM #6
Looks like a ugly mess of code that should be reduced to a loop with an array.
- 08-16-2011, 11:36 PM #7
- 08-16-2011, 11:42 PM #8
Define an array for the squares and another 2D array of indexes for the winning rows. (Whoops need 2 arrays)
Loop thru the array of the winning rows using their values to index into the array of squares. If the squares on a row are not empty and they match, you have a winner.
I wrote a JS version a while back: http://normsstuff.zxq.net/Games/TicTacToe.html
- 08-17-2011, 06:26 AM #9
Senior Member
- Join Date
- Aug 2011
- Posts
- 248
- Rep Power
- 2
I just looked around in your code, he is really inefficient and there is alot of unnecessary code
Just a small example:
Your else if is unnecessary.Java Code:if(XTurn == true) { letterValue = "X"; } else if(XTurn == false) { letterValue = "O"; }
The meaning of this else means that the XTurn is false, so why do you ask again the same question?
That what happens when people rush and not learn properly.
The code should be:
I'm not a pro in java at all, those are very basic things.Java Code:if(XTurn) letterValue = "X"; else letterValue = "O";
Now I have a question, why the buttons has to be private?Last edited by tnrh1; 08-17-2011 at 06:45 AM.
Similar Threads
-
another tic tac toe issue
By hoangx87 in forum New To JavaReplies: 3Last Post: 04-18-2011, 10:26 AM -
JSP issue
By RamaLakshmi in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-21-2011, 01:55 PM -
nio issue
By mawandiadeepak in forum NetworkingReplies: 2Last Post: 03-17-2010, 05:23 AM -
JSF issue
By premjo in forum New To JavaReplies: 0Last Post: 02-14-2010, 02:19 PM -
Issue
By FlashNinja in forum New To JavaReplies: 20Last Post: 11-28-2009, 09:44 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks