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:
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:
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.