If I run this code. I can manipulate it so that X wins. When the third X button is pressed, there is no display saying that X has won and that the game is over. I have read over this code for days and still have not figured it out. Please don't give me the answer exactly. I really want to become a better programmer.
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TicTacToe extends JFrame implements ActionListener, WindowListener{ public JFrame window = new JFrame("window"); public JButton button1 = new JButton(""); public JButton button2 = new JButton(""); public JButton button3 = new JButton(""); public JButton button4 = new JButton(""); public JButton button5 = new JButton(""); public JButton button6 = new JButton(""); public JButton button7 = new JButton(""); public JButton button8 = new JButton(""); public JButton button9 = new JButton(""); public String letter = ""; public String lastLetter = ""; public int count = 0; public boolean win; public boolean isTieGame; //Buttons added, window made visible public TicTacToe() { window.setSize(300,300); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setLayout(new GridLayout(3,3)); window.setName("Tic Tac"); window.add(button1); window.add(button2); window.add(button3); window.add(button4); window.add(button5); window.add(button6); window.add(button7); window.add(button8); window.add(button9); button1.addActionListener(this); button2.addActionListener(this); button3.addActionListener(this); button4.addActionListener(this); button5.addActionListener(this); button6.addActionListener(this); button7.addActionListener(this); button8.addActionListener(this); button9.addActionListener(this); window.setVisible(true); } public static void main(String[] args) { new TicTacToe(); } public boolean isGameOver() { //Button 1 2 3 if(button1.getText() == button2.getText() && button2.getText() == button3.getText() && button1.getText() != "") { win = true; } else if(button4.getText() == button5.getText() && button5.getText() == button6.getText() && button4.getText() != "") { win = true; } else if(button7.getText() == button8.getText() && button8.getText() == button9.getText() && button7.getText() != "") { win = true; } else if(button1.getText() == button5.getText() && button5.getText() == button9.getText() && button1.getText() != "") { win = true; } else if(button3.getText() == button5.getText() && button5.getText() == button7.getText() && button3.getText() != "") { win = true; } else if(button1.getText() == button4.getText() && button4.getText() == button7.getText() && button1.getText() != "") { win = true; } else if(button2.getText() == button5.getText() && button5.getText() == button8.getText() && button2.getText() != "") { win = true; } else if(button3.getText() == button6.getText() && button6.getText() == button9.getText() && button3.getText() != "" ) { win = true; } else { win = false; } return win; } public void counter() { if(count == 1 || count == 3 || count == 5 || count == 7 || count == 9) { letter = "X"; } else { letter = "O"; } } public void endOrReset() { if(win == true) { JOptionPane.showMessageDialog(null, lastLetter() + " WINS!"); int playAgain = JOptionPane.showConfirmDialog(null, "Would you like to play again?", "Play Again.", JOptionPane.YES_NO_OPTION); if(playAgain == JOptionPane.YES_OPTION) { win = false; } else if(playAgain == JOptionPane.NO_OPTION) { JOptionPane.showMessageDialog(null, "Goodbye"); System.exit(0); } } else if(isTieGame == true) { JOptionPane.showMessageDialog(null, "Tie Game!"); int playAgain = JOptionPane.showConfirmDialog(null, "Would you like to play again?", "Play Again.", JOptionPane.YES_NO_OPTION); if(playAgain == JOptionPane.YES_OPTION) { } else if(playAgain == JOptionPane.NO_OPTION) { JOptionPane.showMessageDialog(null, "Goodbye"); System.exit(0); } } } public void actionPerformed(ActionEvent a) { count++; isGameOver(); endOrReset(); counter(); //method needs to return win or loss as count is updated if(a.getSource() == button1) { button1.setText(letter); button1.setEnabled(false); System.out.println( "" + count); System.out.println("" + win); } else if(a.getSource() == button2) { button2.setText(letter); button2.setEnabled(false); System.out.println( "" + count); System.out.println("" + win); } else if(a.getSource() == button3) { button3.setText(letter); button3.setEnabled(false); System.out.println( "" + count); System.out.println("" + win); } else if(a.getSource() == button4){ button4.setText(letter); button4.setEnabled(false); System.out.println( "" + count); System.out.println("" + win); } else if(a.getSource() == button5) { button5.setText(letter); button5.setEnabled(false); System.out.println( "" + count); System.out.println("" + win); } else if(a.getSource() == button6) { button6.setText(letter); button6.setEnabled(false); System.out.println( "" + count); System.out.println("" + win); } else if(a.getSource() == button7) { button7.setText(letter); button7.setEnabled(false); System.out.println( "" + count); System.out.println("" + win); } else if(a.getSource() == button8){ button8.setText(letter); button8.setEnabled(false); System.out.println( "" + count); System.out.println("" + win); } else if(a.getSource() == button9){ button9.setText(letter); button9.setEnabled(false); System.out.println( "" + count); System.out.println("" + win); } } @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } public String lastLetter() { String lastLetter; if(letter == "O") { lastLetter = "X"; } else { lastLetter = "O"; } return lastLetter; } public boolean isTieGame() { if(count >= 9 && win == false) { isTieGame = true; } return isTieGame(); } }