0

I am trying to make Tic Tak Toe Game but I got problem that I cannot handle.

When i make choice it set Text it as X and also Not Editable. Then i generate random number from 1-9 and by switch i go to another method checkButton() where I take text of button and if text is X or O , It should do random number again.

But actually whats work is that It change empty char into O but if Its X or O then It doesnt generate new number but only change those X or O to O. (Sometimes it makes my choice as O)

I dont know where I should change something.It may be problem outside those two methods so I am adding a pastebin link to see : https://pastebin.com/n80x9xSH

public void randomCPUShot(){

  choose=(int)((Math.random()*9)+1);
    System.out.println(choose);

      switch (choose){
          case 1: checkButton(button1); break;
          case 2: checkButton(button2); break;
          case 3: checkButton(button3); break;
          case 4: checkButton(button4); break;
          case 5: checkButton(button5); break;
          case 6: checkButton(button6); break;
          case 7: checkButton(button7); break;
          case 8: checkButton(button8); break;
          case 9: checkButton(button9); break;
          default: break;


      } 
}

public void checkButton(JButton button)
{
     String text = button.getText();

            if(text=="X"){ randomCPUShot();
            // mam blad taki ze oki losuje miejsce dla O ale jak jest to X no to zamienia X na 0
             }
            else if(text=="O"){
                randomCPUShot();
            }else
                {
                   symbolChart='O';
                    switch (choose){
                        case 1: button1.setText(""+symbolChart); button1.setEnabled(false); break;
                        case 2: button2.setText(""+symbolChart);button2.setEnabled(false);break;
                        case 3: button3.setText(""+symbolChart);button3.setEnabled(false);break;
                        case 4: button4.setText(""+symbolChart);button4.setEnabled(false);break;
                        case 5: button5.setText(""+symbolChart);button5.setEnabled(false);break;
                        case 6: button6.setText(""+symbolChart);button6.setEnabled(false);break;
                        case 7: button7.setText(""+symbolChart);button7.setEnabled(false);break;
                        case 8: button8.setText(""+symbolChart);button8.setEnabled(false);break;
                        case 9: button9.setText(""+symbolChart);button9.setEnabled(false);break;
                        default:break;
                    }
            }

}



@Override
public void actionPerformed(ActionEvent e) {
    String command = e.getActionCommand();
    switch (command){
        case "b1": button1.setEnabled(false); setSymbol(); button1.setText(""+symbolChart); randomCPUShot(); break;
        case "b2": button2.setEnabled(false); setSymbol(); button2.setText(""+symbolChart); randomCPUShot(); break;
        case "b3": button3.setEnabled(false); setSymbol(); button3.setText(""+symbolChart); randomCPUShot(); break;
        case "b4": button4.setEnabled(false); setSymbol(); button4.setText(""+symbolChart); randomCPUShot(); break;
        case "b5": button5.setEnabled(false); setSymbol(); button5.setText(""+symbolChart); randomCPUShot(); break;
        case "b6": button6.setEnabled(false); setSymbol(); button6.setText(""+symbolChart); randomCPUShot(); break;
        case "b7": button7.setEnabled(false); setSymbol(); button7.setText(""+symbolChart); randomCPUShot(); break;
        case "b8": button8.setEnabled(false); setSymbol(); button8.setText(""+symbolChart); randomCPUShot(); break;
        case "b9": button9.setEnabled(false); setSymbol(); button9.setText(""+symbolChart); randomCPUShot(); break;

        default :break;
    }
repaint();
}


public void setSymbol() {
    symbolChart='X';
}

1 Answers1

1

You are comparing strings using ==. Never do that. Use equals(). Also, when comparing an instance of String to a constant string value, call equals() on that constant value. In your case, you should be doing things like "X".equals(text) and so on.

Mario Codes
  • 689
  • 8
  • 15
JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24
  • Thanks very much. I forgot about that one. Could u tell me how can i tell program to stop if there isnt any avaibles buttons left? – Paweł Łukasz Aug 27 '20 at 13:33
  • You can check if the buttons are enabled or disabled, because you disable them once you use them for either "X" or "O". If all of them are disabled you can exit the method/quit the program/show a "the game has ended" message/whatever you want to do to signify that the game is over. Another way would be checking their text values, if they are all "X" or "O" the buttons are all occupied and there are no more possible moves. BTW, if my previous answer solved your problem, please accept it so others with the same problem that come across your question can see the solution more easily. Thank you. – JustAnotherDeveloper Aug 27 '20 at 13:39
  • I added this all in if loop (button1.isEnabled())||(button2...... And it works smoothly .Thanks for helping :) – Paweł Łukasz Aug 27 '20 at 13:54