OOP-JAVA Practical 17

OOP-JAVA Practical 17
  • Aim: Write a program that displays a tic-tac-toe board. A cell may be X, O, or empty. What to display at each cell is randomly decided. The X and O are images in the files X.gif and O.gif.
  • Code:
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.layout.GridPane;
    import javafx.scene.image.Image;
    import javafx.scene.image.ImageView;
    import javafx.stage.Stage;
    public class practical17 extends Application {
        @Override
        public void start(Stage primaryStage) {
            GridPane pane = new GridPane();
            for (int i = 0; i<3; i++) {
                for (int j = 0; j<3; j++) {
                    int n = (int) (Math.random() * 3);
                    if (n == 0) {
                        ImageView x=new ImageView(new Image("path of image x"));
                        x.setFitWidth(100);
                        x.setPreserveRatio(true);
                        pane.add(x, 1, j);
                    } else if (n == 1) {
                        ImageView o=new ImageView(new Image("path of image o"));
                        o.setFitWidth(100);
                        o.setPreserveRatio(true);
                        pane.add(o, j, i);
                    } else {
                        continue;
                    }
                }
            }
            pane.setGridLinesVisible(true);
            Scene scene = new Scene(pane, 300, 300);
            primaryStage.setTitle("Practical 17");
            primaryStage.setScene(scene);
            primaryStage.show();
        }
        public static void main(String[] args) {
            Application.launch(args);
        }
    }
  • Output:
    Output of practical 17

Comments