Hi,

I'm in URGENT need of anyone who can help me with the following TicTacToe code. When you run it, it lets you click a box. When you click a box, X appears. The, when it's O's player's turn, it will not print an O.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class TicTacToe extends JFrame implements ActionListener
{
  public String play = "X's turn.";
  public String one1 = "  ";
  public String two2 = "  ";
  public String three3 = "  ";
  public String four4 = "  ";
  public String five5 = "  ";
  public String six6 = "  ";
  public String seven7 = "  ";
  public String eight8 = "  ";
  public String nine9 = "  ";
  public boolean xTurn = true;


  JButton one = new JButton(one1);
  JButton two = new JButton(two2);
  JButton three = new JButton(three3);
  JButton four = new JButton(four4);
  JButton five = new JButton(five5);
  JButton six = new JButton(six6);
  JButton seven = new JButton(seven7);
  JButton eight = new JButton(eight8);
  JButton nine = new JButton(nine9);
  JLabel status = new JLabel(play);



  public TicTacToe()
  {
    super("Tic-Tac-Toe");
    setSize(200,200);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    Container contentArea = getContentPane();
    GridBagLayout flowManager = new GridBagLayout();
    GridBagConstraints pos = new GridBagConstraints();
    contentArea.setLayout(flowManager);

    one.addActionListener(this);
    two.addActionListener(this);
    three.addActionListener(this);
    four.addActionListener(this);
    five.addActionListener(this);
    six.addActionListener(this);
    seven.addActionListener(this);
    eight.addActionListener(this);
    nine.addActionListener(this);


    pos.gridx=0; pos.gridy=0;
    contentArea.add(one,pos);

    pos.gridx=1; pos.gridy=0;
    contentArea.add(two,pos);

    pos.gridx=2; pos.gridy=0;
    contentArea.add(three,pos);

    pos.gridx=0; pos.gridy=1;
    contentArea.add(four,pos);

    pos.gridx=1; pos.gridy=1;
    contentArea.add(five,pos);

    pos.gridx=2; pos.gridy=1;
    contentArea.add(six,pos);

    pos.gridx=0; pos.gridy=2;
    contentArea.add(seven,pos);

    pos.gridx=1; pos.gridy=2;
    contentArea.add(eight,pos);

    pos.gridx=2; pos.gridy=2;
    contentArea.add(nine,pos);

    pos.gridx=1; pos.gridy=3;
    contentArea.add(status,pos);

    setContentPane(contentArea);
  }


  public void actionPerformed(ActionEvent event)
   {
 //O's Turn:

      if(!(xTurn) && play=="O's turn." && event.getSource()==one){
            one.setText("O");
            xTurn=true;
            status.setText("X's turn.");
          }


          else if(!(xTurn) && play=="O's turn." && event.getSource()==two){
       two.setText("O");
       xTurn=true;
       status.setText("X's turn.");
     }



        else if(!(xTurn) && play=="O's turn." && event.getSource()==three){
          three.setText("O");
          xTurn=true;
            status.setText("X's turn.");
            }



            else if(!(xTurn) && play=="O's turn." && event.getSource()==four){
         four.setText("O");
         xTurn=true;
         status.setText("X's turn.");
       }



       else if(!(xTurn) && play=="O's turn." && event.getSource()==five){
     five.setText("O");
     xTurn=true;
     status.setText("X's turn.");
   }



          else if(!(xTurn) && play=="O's turn." && event.getSource()==six){
             six.setText("O");
             xTurn=true;
      status.setText("X's turn.");
 }




    else if(!(xTurn) && play=="O's turn." && event.getSource()==seven){
 seven.setText("O");
 xTurn=true;
 status.setText("X's turn.");
 }



  else if(!(xTurn) && play=="O's turn." && event.getSource()==eight){
 eight.setText("O");
 xTurn=true;
 status.setText("X's turn.");
 }


       else if(!(xTurn) && play=="O's turn." && event.getSource()==nine){
    nine.setText("O");
    xTurn=true;
    status.setText("X's turn.");
  }




//X's turn
     if(xTurn && play=="X's turn." && event.getSource()==one){
       one.setText("X");
       xTurn=false;
       status.setText("O's turn.");
     }
     else if(xTurn && play=="X's turn." && event.getSource()==two){
       two.setText("X");
       xTurn=false;
       status.setText("O's turn.");
     }
     else if(xTurn && play=="X's turn." && event.getSource()==three){
       three.setText("X");
       xTurn=false;
       status.setText("O's turn.");
     }
     else if(xTurn && play=="X's turn." && event.getSource()==four){
       four.setText("X");
       xTurn=false;
       status.setText("O's turn.");
     }
     else if(xTurn && play=="X's turn." && event.getSource()==five){
       five.setText("X");
       xTurn=false;
       status.setText("O's turn.");
     }
     else if(xTurn && play=="X's turn." && event.getSource()==six){
       six.setText("X");
       xTurn=false;
       status.setText("O's turn.");
     }
     else if(xTurn && play=="X's turn." && event.getSource()==seven){
       seven.setText("X");
       xTurn=false;
       status.setText("O's turn.");
     }
     else if(xTurn && play=="X's turn." && event.getSource()==eight){
       eight.setText("X");
       xTurn=false;
       status.setText("O's turn.");
     }
     else if(xTurn && play=="X's turn." && event.getSource()==nine){
       nine.setText("X");
       xTurn=false;
       status.setText("O's turn.");
     }
   }


  public static void main(String[] args)
  { TicTacToe eg = new TicTacToe(); }
}

Thanks! :)

C++

Dani AI

Generated

As hinted, the immediate bug is that the String variable play is never updated after a turn. That plus using == to compare Strings makes the O-turn checks unreliable. The posted code only updates the JLabel status but not the play variable, and == compares object identity, not content. Fix either by keeping play synchronized with the label and using equals(...), or better, stop basing logic on the label text at all and use the xTurn boolean only.

Minimal change (least invasive): whenever you change the label also update play and use .equals(...) for comparisons. Example pattern to add where you currently call status.setText(...):

xTurn = true;                 // or false, depending on the branch
play = "X's turn.";           // update the String you compare elsewhere
status.setText(play);

A cleaner, safer approach is to simplify action handling to one short block and avoid comparing strings entirely. Replace the long per-button if/else chain with this logic inside actionPerformed:

JButton b = (JButton) event.getSource();
if (!b.getText().trim().isEmpty()) return;   // prevent overwriting

b.setText(xTurn ? "X" : "O");
xTurn = !xTurn;
String next = xTurn ? "X's turn." : "O's turn.";
play = next;               // optional if you keep 'play' at all
status.setText(next);

Other important fixes and best practices: build the GUI on the Event Dispatch Thread (use SwingUtilities.invokeLater), call pack() and then setVisible(true) after adding components (do not call setVisible(true) before building the UI), and consider disabling a button after it has been played or maintain a board state array. After these fixes, add win/draw detection and refactor the button setup into an array to avoid repetitive code.

Recommended Answers

All 2 Replies

your screaming you demand urgent help is enough reason to let your request sit for a few months before looking at it.

One hint I will give you.

Where do you set the value of 'play' after each turn??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.