Question: I'm trying to modify a java Tic Tac Toe game that is currently human vs human and make it human vs computer. This should be

I'm trying to modify a java Tic Tac Toe game that is currently human vs human and make it human vs computer. This should be presented in GUI rather than console. See expected output below and my current code that needs to be modified.

I'm trying to modify a java Tic Tac Toe game that is

import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.scene.shape.Ellipse;

public class TicTacToe extends Application { // Indicate which player has a turn, initially it is the X player private char whoseTurn = 'X';

// Create and initialize cell private Cell[][] cell = new Cell[3][3];

// Create and initialize a status label private Label lblStatus = new Label("X's turn to play");

@Override // Override the start method in the Application class public void start(Stage primaryStage) { // Pane to hold cell GridPane pane = new GridPane(); for (int i = 0; i

BorderPane borderPane = new BorderPane(); borderPane.setCenter(pane); borderPane.setBottom(lblStatus); // Create a scene and place it in the stage Scene scene = new Scene(borderPane, 450, 170); primaryStage.setTitle("TicTacToe"); // Set the stage title primaryStage.setScene(scene); // Place the scene in the stage primaryStage.show(); // Display the stage }

/** Determine if the cell are all occupied */ public boolean isFull() { for (int i = 0; i

return true; }

/** Determine if the player with the specified token wins */ public boolean isWon(char token) { for (int i = 0; i

for (int j = 0; j

if (cell[0][0].getToken() == token && cell[1][1].getToken() == token && cell[2][2].getToken() == token) { cell[0][0].setStyle("-fx-background-color: red;" + "-fx-border-color: black;"); cell[1][1].setStyle("-fx-background-color: red;" + "-fx-border-color: black;"); cell[2][2].setStyle("-fx-background-color: red;" + "-fx-border-color: black;"); return true; }

if (cell[0][2].getToken() == token && cell[1][1].getToken() == token && cell[2][0].getToken() == token) { cell[0][2].setStyle("-fx-background-color: red;" + "-fx-border-color: black;"); cell[1][1].setStyle("-fx-background-color: red;" + "-fx-border-color: black;"); cell[2][0].setStyle("-fx-background-color: red;" + "-fx-border-color: black;"); return true; }

return false; }

// An inner class for a cell public class Cell extends Pane { // Token used for this cell private char token = ' ';

public Cell() { setStyle("-fx-border-color: black"); this.setPrefSize(2000, 2000); this.setOnMouseClicked(e -> handleMouseClick()); }

/** Return token */ public char getToken() { return token; }

/** Set a new token */ public void setToken(char c) { token = c; if (token == 'X') { Line line1 = new Line(10, 10, this.getWidth() - 10, this.getHeight() - 10); line1.endXProperty().bind(this.widthProperty().subtract(10)); line1.endYProperty().bind(this.heightProperty().subtract(10)); Line line2 = new Line(10, this.getHeight() - 10, this.getWidth() - 10, 10); line2.startYProperty().bind( this.heightProperty().subtract(10)); line2.endXProperty().bind(this.widthProperty().subtract(10)); // Add the lines to the pane this.getChildren().addAll(line1, line2); } else if (token == 'O') { Ellipse ellipse = new Ellipse(this.getWidth() / 2, this.getHeight() / 2, this.getWidth() / 2 - 10, this.getHeight() / 2 - 10); ellipse.centerXProperty().bind( this.widthProperty().divide(2)); ellipse.centerYProperty().bind( this.heightProperty().divide(2)); ellipse.radiusXProperty().bind( this.widthProperty().divide(2).subtract(10)); ellipse.radiusYProperty().bind( this.heightProperty().divide(2).subtract(10)); ellipse.setStroke(Color.BLACK); ellipse.setFill(Color.WHITE); getChildren().add(ellipse); // Add the ellipse to the pane } }

/* Handle a mouse click event */ private void handleMouseClick() { // If cell is empty and game is not over if (token == ' ' && whoseTurn != ' ') { setToken(whoseTurn); // Set token in the cell

// Check game status if (isWon(whoseTurn)) { lblStatus.setText(whoseTurn + " won! The game is over"); whoseTurn = ' '; // Game is over } else if (isFull()) { lblStatus.setText("Draw! The game is over"); whoseTurn = ' '; // Game is over } else { // Change the turn whoseTurn = (whoseTurn == 'X') ? 'O' : 'X'; // Display whose turn lblStatus.setText(whoseTurn + "'s turn"); } } } } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }

Use the TicTacToe program discussed in Module 8 as a baseline and make it a real TicTacToe game so that you are now playing with the computer. Tic Tac Toe OK XO X won! The game is over

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!