I’m trying to make a tictactoe game in javafx and the conditions of victory doesnt work, the problem is that for an unknown reason a value is replaced in the array the result in the image works here the grid[0][0] = ‘X’, grid[0][1] = ‘X’, grid[0][2] = ‘X’, grid[1][0] = ‘O’, grid[1][1] = ‘O’ but it doesnt display that(https://i.sstatic.net/fzVXsTi6.png)
I tried everything and it didnt work here is my code Thanks:
public class Controller {
@FXML
public GridPane gridGame;
public Button[][] grid = new Button[3][3];
public String currentPlayer = "X";
public void start(){
innitGrid();
}
@FXML
public void onClick(ActionEvent e){
if(!winCondition()) {
Button b = (Button) e.getSource();
if(b.getText().isEmpty()) b.setText(currentPlayer);
if(b.getText().equals("X")) currentPlayer = "0";
else currentPlayer = "X";
}
}
public void innitGrid(){
int count = 0;
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
grid[i][j] = (Button) gridGame.getChildren().get((count));
count++;
}
}
}
public boolean winCondition(){
for(int i = 0; i < 3; i++){
// check is there is honrizontal win
if(!grid[0][i].getText().isEmpty()){
if(grid[0][i].getText().equals(grid[1][i].getText()) && grid[1][i].getText().equals(grid[2][i].getText()))
return true;
}
if(!grid[i][0].getText().isEmpty()){
if(grid[i][0].getText().equals(grid[i][1].getText()) && grid[i][1].getText().equals(grid[i][2].getText())) return true;
}
}
return false;
}
public void clickReset(){
for(int i = 0; i < 3; i++){
for(int j = 0; j < 3; j++){
grid[i][j].setText("");
}
}
innitGrid();
currentPlayer = "X";
}
}
New contributor
Ferhat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.