Question: Can someone help me create a UML of this Program? import java.util.Random; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import
Can someone help me create a UML of this Program?
import java.util.Random; import javafx.application.Application; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.Scene; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.layout.GridPane; import javafx.scene.layout.VBox; import javafx.stage.Stage;
public class TicTacToe extends Application { Random rand = new Random(); char playFirst;
public void start(Stage primaryStage) throws Exception { VBox root = new VBox(); //Initializations Label ques = new Label("Who will play first?"); Button user = new Button("User"); Button comp = new Button("Computer"); GridPane grid = new GridPane(); Button btn[][] = new Button[3][3]; Label winner = new Label(); Label result = new Label(); user.setOnAction(e -> //Button to let user play first { playFirst = 'X'; root.getChildren().addAll(grid, result, winner); root.getChildren().removeAll(ques, user, comp); }); comp.setOnAction(e -> //Button to let computer play first { playFirst = 'O'; btn[rand.nextInt(3)][rand.nextInt(3)].setText("O"); //Fills the first cell by the computer root.getChildren().addAll(grid, result, winner); root.getChildren().removeAll(ques, user, comp); }); int i, j; for(i=0; i<3; i++) for(j=0; j<3; j++) { int x = i, y = j; btn[i][j] = new Button(""); grid.add(btn[i][j], j, i); //Adds the buttons to the grid btn[i][j].setPrefWidth(70); btn[i][j].setPrefHeight(70); btn[i][j].setOnAction(e -> { int xCount = 0, oCount = 0; String win = ""; if(result.getText().equals("")) //Runs only until someone wins { if(playFirst == 'X') //Runs only when X takes first turn { xCount = 0; for (int a = 0; a < 3; a++) //Counts the number of X for (int b = 0; b < 3; b++) if (btn[a][b].getText().equals("X")) xCount++; if (btn[x][y].getText().equals("")) //Changes the button to X { btn[x][y].setText("X"); xCount++; win = check(btn); //Tells if someone won if (!win.equals("")) //If won, change the winner and result label { winner.setText("Winner"); result.setText(win); } else if(xCount == 5) //When number of X becomes 5, game is draw winner.setText("DRAW"); if(winner.getText().equals("")) fillO(btn); //Computer takes the turn win = check(btn); //Checks after computer's turn if (!win.equals("")) { winner.setText("Winner"); result.setText(win); } } } else //Runs only when O takes first turn { oCount = 0; for (int a = 0; a < 3; a++) //Counts the number of O for (int b = 0; b < 3; b++) if (btn[a][b].getText().equals("O")) oCount++; if (btn[x][y].getText().equals("")) //Changes the button to O btn[x][y].setText("X"); win = check(btn); //Tells if someone won if (!win.equals("")) //If won, change the winner and result label { winner.setText("Winner"); result.setText(win); } if(winner.getText().equals("")) //Computer takes the turn { fillO(btn); oCount++; } win = check(btn); if (!win.equals("")) //Checks after computer's turn { winner.setText("Winner"); result.setText(win); } else if(oCount == 5) //When number of O becomes 5, game is draw winner.setText("DRAW"); } } }); }
ques.setAlignment(Pos.CENTER); ques.setPrefWidth(150); VBox.setMargin(ques, new Insets(20, 0, 0, 50)); user.setAlignment(Pos.CENTER); user.setPrefWidth(70); VBox.setMargin(user, new Insets(30, 0, 0, 90)); comp.setAlignment(Pos.CENTER); comp.setPrefWidth(100); VBox.setMargin(comp, new Insets(10, 0, 0, 75)); VBox.setMargin(grid, new Insets(20, 20, 20, 20)); winner.setAlignment(Pos.CENTER); winner.setPrefWidth(70); VBox.setMargin(winner, new Insets(0, 0, 0, 90)); result.setPrefWidth(70); result.setPrefHeight(70); result.setAlignment(Pos.CENTER); VBox.setMargin(result, new Insets(0, 0, 0, 90)); root.getChildren().addAll(ques, user, comp); primaryStage.setTitle("Tic Tac Toe"); Scene scene = new Scene(root, 250, 350); primaryStage.setScene(scene); primaryStage.show(); }
String check(Button[][] a) //To check if a player won { int i; for(i=0;i<3;i++) { if(a[i][0].getText().equals(a[i][1].getText()) && a[i][1].getText().equals(a[i][2].getText()) && !a[i][2].getText().equals("")) //Checks the rows return(a[i][0].getText()); else if(a[0][i].getText().equals(a[1][i].getText()) && a[1][i].getText().equals(a[2][i].getText()) && !a[2][i].getText().equals("")) //Checks the columns return(a[0][i].getText()); } if((a[0][0].getText().equals(a[1][1].getText()) && a[1][1].getText().equals(a[2][2].getText())) || (a[0][2].getText().equals(a[1][1].getText()) && a[1][1].getText().equals(a[2][0].getText())) && !a[1][1].getText().equals("")) //Checks the diagonals return(a[1][1].getText()); return(""); } void fillO(Button btn[][]) //Fills the O { int a, b, xc = 0, oc = 0, oCount = 0; for(a = 0; a < 3; a++) //Counts number of O in the grid for(b = 0; b < 3; b++) if(btn[a][b].getText().equals("O")) oCount++; if((oCount < 4 && playFirst == 'X') || (oCount < 5 && playFirst == 'O')) { //To find row, column or diagonal with two O and zero X for(a = 0; a < 3; a++) //Counts number of O and X in rows { xc = oc = 0; for(b = 0; b < 3; b++) if(btn[a][b].getText().equals("O")) oc++; else if(btn[a][b].getText().equals("X")) xc++; if(oc==2) break; } if(oc==2 && xc==0) //Fills the cell found above { for(b = 0; b <3; b++) if(btn[a][b].getText().equals("")) { btn[a][b].setText("O"); } } else { for(a = 0; a < 3; a++) //Counts number of O and X in rows { xc = oc = 0; for(b = 0; b < 3; b++) if(btn[b][a].getText().equals("O")) oc++; else if(btn[b][a].getText().equals("X")) xc++; if(oc==2) break; } if(oc==2 && xc==0) //Fills the cell found above { for(b = 0; b <3; b++) if(btn[b][a].getText().equals("")) { btn[b][a].setText("O"); } } else { xc = oc = 0; for(a = 0; a < 3; a++) //Counts number of O and X in primary diagonal if(btn[a][a].getText().equals("O")) oc++; else if(btn[a][a].getText().equals("X")) xc++; if(oc==2 && xc==0) //Fills the cell found above { for(a = 0; a < 3; a++) if(btn[a][a].getText().equals("")) { btn[a][a].setText("O"); } } else { xc = oc = 0; for(a = 0; a < 3; a++) //Counts number of O and X in secondary diagonal if(btn[a][2-a].getText().equals("O")) oc++; else if(btn[a][2-a].getText().equals("X")) xc++; if(oc==2 && xc==0) //Fills the cell found above { for(a = 0; a < 3; a++) if(btn[a][2-a].getText().equals("")) { btn[a][2-a].setText("O"); } } else //Similarly to find row, column or diagonal with two X and zero O { for(a = 0; a < 3; a++) //Row { xc = oc = 0; for(b = 0; b < 3; b++) if(btn[a][b].getText().equals("O")) oc++; else if(btn[a][b].getText().equals("X")) xc++; if(xc==2) break; } if(xc==2 && oc==0) { for(b = 0; b <3; b++) if(btn[a][b].getText().equals("")) { btn[a][b].setText("O"); } } else { for(a = 0; a < 3; a++) //Column { xc = oc = 0; for(b = 0; b < 3; b++) if(btn[b][a].getText().equals("O")) oc++; else if(btn[b][a].getText().equals("X")) xc++; if(xc==2) break; } if(xc==2 && oc==0) { for(b = 0; b <3; b++) if(btn[b][a].getText().equals("")) { btn[b][a].setText("O"); } } else { xc = oc = 0; for(a = 0; a < 3; a++) //Primary diagonal if(btn[a][a].getText().equals("O")) oc++; else if(btn[a][a].getText().equals("X")) xc++; if(xc==2 && oc==0) { for(a = 0; a < 3; a++) if(btn[a][a].getText().equals("")) { btn[a][a].setText("O"); } } else { xc = oc = 0; for(a = 0; a < 3; a++) //Secondary diagonal if(btn[a][2-a].getText().equals("O")) oc++; else if(btn[a][2-a].getText().equals("X")) xc++; if(xc==2 && oc==0) { for(a = 0; a < 3; a++) if(btn[a][2-a].getText().equals("")) { btn[a][2-a].setText("O"); } } else //If no case is satisfied, then fills O randomly { do { a = rand.nextInt(3); b = rand.nextInt(3); }while(btn[a][b].getText().equals("X") || btn[a][b].getText().equals("O")); btn[a][b].setText("O"); } } } } } } } } } } public static void TicTacToe(String[] args) //Main function { launch(args); //Run the GUI } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
