0

This is my first JavaFX project.

I have created a Tic Tac Toe game by using JavaFx. The game works for a player vs player mode and is perfect but the code I have used is too long.

Here are some apparent duplicates but they are completely different:

Tic-tac-toe code help improve

Code Golf: Tic Tac Toe

This is my code for button one which I have to repeat for eight more buttons:

one.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent actionEvent) {
        System.out.println(1);
        System.out.println("my state is " + state[0]);
        if (state[0] == 0){
            state[0]  = 1 ;
            for (int a: state ) {
            System.out.print(a);}
            if(i%2==0){
                one.setText("X"); i+= 1;
                System.out.println(i+"recorded");
                turn.setText("O turn");
                result[0] = 'x';
                win() ;
            }
            else{
                one.setText("O"); i+= 1 ;
                System.out.println(i+"recorded");
                turn.setText("X turn");
                result[0] = 'o';
                win() ;
            }
        }
    }
});

A screenshot:

enter image description here

Is there a choice to avoid repeating the code for 8 more buttons?

You can see in the screenshot that there are repeated for loops too.

This is my very first gui project but I do not intend to remain a beginner.

Edit

After I don't see any for-loop in the code. Perhaps you meant if-statements? – NiVeR

Yes, I meant if statements.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • i don't see any for-loop in the code. Perhaps you meant if-statements? – NiVeR Jun 21 '19 at 07:03
  • 1
    Just in case you aren't aware, if you have working code and are looking for ways to improve it, then [codereview.se] may (read their guidelines) be a better fit. – Slaw Jun 21 '19 at 13:52
  • @Slaw i have a code and thanks i will check that out –  Jun 21 '19 at 13:55

1 Answers1

2

Try doing this:

void makeMove(Button button, JLabel turn, int cell){
    System.out.println(cell);
    System.out.println("my state is " + state[cell-1]);
    if (state[cell-1] == 0){
        state[cell-1]  = 1 ;
        for (int a: state ) {
        System.out.print(a);}
        if(i%2==0){
            button.setText("X"); i+= 1;
            System.out.println(i+"recorded");
            turn.setText("O turn");
            result[cell-1] = 'x';
            win() ;
        }
        else{
            button.setText("O"); i+= 1 ;
            System.out.println(i+"recorded");
            turn.setText("X turn");
            result[cell-1] = 'o';
            win() ;
        }
    }
}

one.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent actionEvent) {
        makeMove(one,turn,1);
    }
);

and repeat that for other buttons by changing 1 to 2...9

This code can actually be still improved, but, since you said you're starting out, this is a good way to start thinking about modularising code.

I wasn't sure what turn was so I made it a Label in the definition of makeMove, change it to whatever it actually is.

fabian
  • 80,457
  • 12
  • 86
  • 114
isstiaung
  • 611
  • 5
  • 12
  • I was just looking through this, where does the value of i get set? – isstiaung Jun 21 '19 at 07:26
  • i have not added that code but i set it and add 1 each time so that when it is even it is turn of X and when it is odd it is turn of O, if you need that part of code reply to me –  Jun 21 '19 at 07:28
  • Is `i` a global variable? – isstiaung Jun 21 '19 at 07:34
  • i is declared inside public "class Controller implements Initializable{}" but outside "public void initialize(URL url, ResourceBundle resourceBundle) {}" –  Jun 21 '19 at 07:38