Write a program that displays a tic-tac-toe, board. A cell may be X, O or empty.
ID: 3777640 • Letter: W
Question
Write a program that displays a tic-tac-toe, board. A cell may be X, O or empty. What to display in each cell is randomly selected.
Use the GridPane to control your cells and show the grid lines. Also, use Labels, NOT images!
Once you have it working properly, submit your .java file for grading.
modified this code by USING the GridPane to control the cells and show the grid lines. Also, USING Labels insted of images!
HERE IS THE CODE :
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public class TicTacToe extends Application {
@Override
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
Image img1 = new Image("image/o.gif");
Image img2 = new Image("image/x.gif");
ImageView imgView1 = new ImageView(img1);
ImageView imgView2 = new ImageView(img2);
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
int random = (int)(Math.random() * 2);
if(random == 0) {
pane.add(imgView1, j, i);
} else {
pane.add(imgView2, j, i);
}
}
}
Scene scene = new Scene(pane);
primaryStage.setTitle("Tic-Tac-Toe Board");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
Explanation / Answer
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
public category TicTacToe extends Application
}
}
Scene scene = new Scene(pane);
primaryStage.setTitle("Tic-Tac-Toe Board");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args)
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.