Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please, I am in need of assistance with this problem. Could you help me. In Sect

ID: 3773513 • Letter: P

Question

Please, I am in need of assistance with this problem. Could you help me.

In Section 16.12, Case Study: Developing a Tic-Tac-Toe Game, you developed a program
for a tic-tac-toe game that enables two players to play the game on the same machine. In this
section, you will learn how to develop a distributed tic-tac-toe game using multithreads and
networking with socket streams. A distributed tic-tac-toe game enables users to play on different
machines from anywhere on the Internet.
You need to develop a server for multiple clients. The server creates a server socket and
accepts connections from every two players to form a session. Each session is a thread that
communicates with the two players and determines the status of the game. The server can
establish any number of sessions, as shown in Figure 31.13.
For each session, the first client connecting to the server is identified as player 1 with token
X, and the second client connecting is identified as player 2 with token O. The server notifies
the players of their respective tokens. Once two clients are connected to it, the server starts a
thread to facilitate the game between the two players by performing the steps repeatedly, as
shown in Figure 31.13.

The server does not have to be a graphical component, but creating it in a GUI in which
game information can be viewed is user-friendly. You can create a scroll pane to hold a text
area in the GUI and display game information in the text area. The server creates a thread to
handle a game session when two players are connected to the server.
The client is responsible for interacting with the players. It creates a user interface with
nine cells and displays the game title and status to the players in the labels. The client class is
very similar to the TicTacToe class presented in the case study in Listing 16.13. However,
the client in this example does not determine the game status (win or draw); it simply passes
the moves to the server and receives the game status from the server.
Based on the foregoing analysis, you can create the following classes:
TicTacToeServer serves all the clients in Listing 31.9.
HandleASession facilitates the game for two players. This class is defined in
TicTacToeServer.java.
TicTacToeClient models a player in Listing 31.10.
Cell models a cell in the game. It is an inner class in TicTacToeClient.
TicTacToeConstants is an interface that defines the constants shared by all the
classes in the example in Listing 31.8.

The server can serve any number of sessions simultaneously. Each session takes care of two
players. The client can be deployed to run as a Java applet. To run a client as a Java applet
from a Web browser, the server must run from a Web server.

The TicTacToeConstants interface defines the constants shared by all the classes in the
project. Each class that uses the constants needs to implement the interface. Centrally defining
constants in an interface is a common practice in Java.
Once a session is established, the server receives moves from the players in alternation.
Upon receiving a move from a player, the server determines the status of the game. If
the game is not finished, the server sends the status (CONTINUE) and the player’s move to
the other player. If the game is won or a draw, the server sends the status (PLAYER1_WON,
PLAYER2_WON, or DRAW) to both players.

Modify the code so that the user is not locked into a single choice of 'X' or 'O', BUT can decide which character to use when it's their turn. This is the 'wild tic-tac-toe' version of the game.

Also, have an option when the game is over whether the two players want to play again or not. If not, print display the stats to both the players.

// TicTacToeConstants.java

public interface TicTacToeConstants {
public static int PLAYER1 = 1; // Indicate player 1
public static int PLAYER2 = 2; // Indicate player 2
public static int PLAYER1_WON = 1; // Indicate player 1 won
public static int PLAYER2_WON = 2; // Indicate player 2 won
public static int DRAW = 3; // Indicate a draw
public static int CONTINUE = 4; // Indicate to continue
}

// TicTacToeServer.java

import java.io.*;
import java.net.*;
import java.util.Date;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

public class TicTacToeServer extends Application implements TicTacToeConstants {
private int sessionNo = 1; // Number a session
  
@Override // Override the start method in the Application class
public void start(Stage primaryStage) {
TextArea taLog = new TextArea();
  
// Create a scene and place it in the stage
Scene scene = new Scene(new ScrollPane(taLog), 450, 200);
primaryStage.setTitle("TicTacToeServer"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
  
new Thread( () -> {
try {
// Create a server socket
ServerSocket serverSocket = new ServerSocket(8000);
Platform.runLater(() -> taLog.appendText(new Date() +
": Server started at socket 8000 "));
  
// Ready to create a session for every two players
while (true) {
Platform.runLater(() -> taLog.appendText(new Date() +
": Wait for players to join session " + sessionNo + ' '));
  
// Connect to player 1
Socket player1 = serverSocket.accept();
  
Platform.runLater(() -> {
taLog.appendText(new Date() + ": Player 1 joined session "
+ sessionNo + ' ');
taLog.appendText("Player 1's IP address" +
player1.getInetAddress().getHostAddress() + ' ');
});
  
// Notify that the player is Player 1
new DataOutputStream(player1.getOutputStream()).writeInt(PLAYER1);
  
// Connect to player 2
Socket player2 = serverSocket.accept();
  
Platform.runLater(() -> {
taLog.appendText(new Date() +
": Player 2 joined session " + sessionNo + ' ');
taLog.appendText("Player 2's IP address" +
player2.getInetAddress().getHostAddress() + ' ');
});
  
// Notify that the player is Player 2
new DataOutputStream(player2.getOutputStream()).writeInt(PLAYER2);
  
// Display this session and increment session number
Platform.runLater(() ->
taLog.appendText(new Date() +
": Start a thread for session " + sessionNo++ + ' '));
  
// Launch a new thread for this session of two players
new Thread(new HandleASession(player1, player2)).start();
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}).start();
}
  
// Define the thread class for handling a new session for two players
class HandleASession implements Runnable, TicTacToeConstants {
private Socket player1;
private Socket player2;
  
// Create and initialize cells
private char[][] cell = new char[3][3];
  
private DataInputStream fromPlayer1;
private DataOutputStream toPlayer1;
private DataInputStream fromPlayer2;
private DataOutputStream toPlayer2;
  
// Continue to play
private boolean continueToPlay = true;
  
/** Construct a thread */
public HandleASession(Socket player1, Socket player2) {
this.player1 = player1;
this.player2 = player2;
  
// Initialize cells
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
cell[i][j] = ' ';
}
  
/** Implement the run() method for the thread */
public void run() {
try {
// Create data input and output streams
DataInputStream fromPlayer1 = new DataInputStream(player1.getInputStream());
DataOutputStream toPlayer1 = new DataOutputStream(player1.getOutputStream());
DataInputStream fromPlayer2 = new DataInputStream(player2.getInputStream());
DataOutputStream toPlayer2 = new DataOutputStream(player2.getOutputStream());
  
// Write anything to notify player 1 to start
// This is just to let player 1 know to start
toPlayer1.writeInt(1);
  
// Continuously serve the players and determine and report
// the game status to the players
while (true) {
// Receive a move from player 1
int row = fromPlayer1.readInt();
int column = fromPlayer1.readInt();
cell[row][column] = 'X';
  
// Check if Player 1 wins
if (isWon('X')) {
toPlayer1.writeInt(PLAYER1_WON);
toPlayer2.writeInt(PLAYER1_WON);
sendMove(toPlayer2, row, column);
break; // Break the loop
}
else if (isFull()) { // Check if all cells are filled
toPlayer1.writeInt(DRAW);
toPlayer2.writeInt(DRAW);
sendMove(toPlayer2, row, column);
break;
}
else {
// Notify player 2 to take the turn
toPlayer2.writeInt(CONTINUE);
  
// Send player 1's selected row and column to player 2
sendMove(toPlayer2, row, column);
}
  
// Receive a move from Player 2
row = fromPlayer2.readInt();
column = fromPlayer2.readInt();
cell[row][column] = 'O';
  
// Check if Player 2 wins
if (isWon('O')) {
toPlayer1.writeInt(PLAYER2_WON);
toPlayer2.writeInt(PLAYER2_WON);
sendMove(toPlayer1, row, column);
break;
}
else {
// Notify player 1 to take the turn
toPlayer1.writeInt(CONTINUE);
  
// Send player 2's selected row and column to player 1
sendMove(toPlayer1, row, column);
}
}
}
catch(IOException ex) {
ex.printStackTrace();
}
}
  
/** Send the move to other player */
private void sendMove(DataOutputStream out, int row, int column) throws IOException {
out.writeInt(row); // Send row index
out.writeInt(column); // Send column index
}
  
/** Determine if the cells are all occupied */
private boolean isFull() {
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (cell[i][j] == ' ')
return false; // At least one cell is not filled
  
// All cells are filled
return true;
}
  
/** Determine if the player with the specified token wins */
private boolean isWon(char token) {
// Check all rows
for (int i = 0; i < 3; i++)
if ((cell[i][0] == token) && (cell[i][1] == token) && (cell[i][2] == token)) {
return true;
}
  
/** Check all columns */
for (int j = 0; j < 3; j++)
if ((cell[0][j] == token) && (cell[1][j] == token) && (cell[2][j] == token)) {
return true;
}
  
/** Check major diagonal */
if ((cell[0][0] == token) && (cell[1][1] == token) && (cell[2][2] == token)) {
return true;
}
  
/** Check subdiagonal */
if ((cell[0][2] == token) && (cell[1][1] == token) && (cell[2][0] == token)) {
return true;
}
  
/** All checked, but no winner */
return false;
}
}
}

// TicTacToeClient.java

import java.io.*;
import java.net.*;
import java.util.Date;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.scene.shape.Line;
import javafx.stage.Stage;

public class TicTacToeClient extends Application implements TicTacToeConstants {
// Indicate whether the player has the turn
private boolean myTurn = false;
  
// Indicate the token for the player
private char myToken = ' ';
  
// Indicate the token for the other player
private char otherToken = ' ';
  
// Create and initialize cells
private Cell[][] cell = new Cell[3][3];
  
// Create and initialize a title label
private Label lblTitle = new Label();
  
// Create and initialize a status label
private Label lblStatus = new Label();
  
// Indicate selected row and column by the current move
private int rowSelected;
private int columnSelected;
  
// Input and output streams from/to server
private DataInputStream fromServer;
private DataOutputStream toServer;
  
// Continue to play?
private boolean continueToPlay = true;
  
// Wait for the player to mark a cell
private boolean waiting = true;
  
// Host name or ip
private String host = "localhost";
  
@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 < 3; i++)
for (int j = 0; j < 3; j++)
pane.add(cell[i][j] = new Cell(i, j), j, i);
  
BorderPane borderPane = new BorderPane();
borderPane.setTop(lblTitle);
borderPane.setCenter(pane);
borderPane.setBottom(lblStatus);
  
// Create a scene and place it in the stage
Scene scene = new Scene(borderPane, 320, 350);
primaryStage.setTitle("TicTacToeClient"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
  
// Connect to the server
connectToServer();
}
  
private void connectToServer() {
try {
// Create a socket to connect to the server
Socket socket = new Socket(host, 8000);
  
// Create an input stream to receive data from the server
fromServer = new DataInputStream(socket.getInputStream());
  
// Create an output stream to send data to the server
toServer = new DataOutputStream(socket.getOutputStream());
}
catch (Exception ex) {
ex.printStackTrace();
}
  
// Control the game on a separate thread
new Thread(() -> {
try {
// Get notification from the server
int player = fromServer.readInt();
  
// Am I player 1 or 2?
if (player == PLAYER1) {
myToken = 'X';
otherToken = 'O';
Platform.runLater(() -> {
lblTitle.setText("Player 1 with token 'X'");
lblStatus.setText("Waiting for player 2 to join");
});
  
// Receive startup notification from the server
fromServer.readInt(); // Whatever read is ignored
  
// The other player has joined
Platform.runLater(() ->
lblStatus.setText("Player 2 has joined. I start first"));
  
// It is my turn
myTurn = true;
}
else if (player == PLAYER2) {
myToken = 'O';
otherToken = 'X';
Platform.runLater(() -> {
lblTitle.setText("Player 2 with token 'O'");
lblStatus.setText("Waiting for player 1 to move");
});
}
  
// Continue to play
while (continueToPlay) {
if (player == PLAYER1) {
waitForPlayerAction(); // Wait for player 1 to move
sendMove(); // Send the move to the server
receiveInfoFromServer(); // Receive info from the server
}
else if (player == PLAYER2) {
receiveInfoFromServer(); // Receive info from the server
waitForPlayerAction(); // Wait for player 2 to move
sendMove(); // Send player 2's move to the server
}
}
  
}
catch (Exception ex) {
ex.printStackTrace();
}
}).start();
}

/** Wait for the player to mark a cell */
private void waitForPlayerAction() throws InterruptedException {
while (waiting) {
Thread.sleep(100);
}
  
waiting = true;
}
  
/** Send this player's move to the server */
private void sendMove() throws IOException {
toServer.writeInt(rowSelected); // Send the selected row
toServer.writeInt(columnSelected); // Send the selected column
}
  
/** Receive info from the server */
private void receiveInfoFromServer() throws IOException {
// Receive game status
int status = fromServer.readInt();
  
if (status == PLAYER1_WON) {
// Player 1 won, stop playing
continueToPlay = false;
if (myToken == 'X') {
Platform.runLater(() -> lblStatus.setText("I won! (X)"));
}
else if (myToken == 'O') {
Platform.runLater(() ->
lblStatus.setText("Player 1 (X) has won!"));
receiveMove();
}
}
else if (status == PLAYER2_WON) {
// Player 2 won, stop playing
continueToPlay = false;
if (myToken == 'O') {
Platform.runLater(() -> lblStatus.setText("I won! (O)"));
}
else if (myToken == 'X') {
Platform.runLater(() ->
lblStatus.setText("Player 2 (O) has won!"));
receiveMove();
}
}
else if (status == DRAW) {
// No winner, game is over
continueToPlay = false;
Platform.runLater(() ->
lblStatus.setText("Game is over, no winner!"));
  
if (myToken == 'O') {
receiveMove();
}
}
else {
receiveMove();
Platform.runLater(() -> lblStatus.setText("My turn"));
myTurn = true; // It is my turn
}
}
  
private void receiveMove() throws IOException {
// Get the other player's move
int row = fromServer.readInt();
int column = fromServer.readInt();
Platform.runLater(() -> cell[row][column].setToken(otherToken));
}
  
// An inner class for a cell
public class Cell extends Pane {
// Indicate the row and column of this cell in the board
private int row;
private int column;
  
// Token used for this cell
private char token = ' ';
  
public Cell(int row, int column) {
this.row = row;
this.column = column;
this.setPrefSize(2000, 2000); // What happens without this?
setStyle("-fx-border-color: black"); // Set cell's border
this.setOnMouseClicked(e -> handleMouseClick());
}
  
/** Return token */
public char getToken() {
return token;
}
  
/** Set a new token */
public void setToken(char c) {
token = c;
repaint();
}
  
protected void repaint() {
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 not occupied and the player has the turn
if (token == ' ' && myTurn) {
setToken(myToken); // Set the player's token in the cell
myTurn = false;
rowSelected = row;
columnSelected = column;
lblStatus.setText("Waiting for the other player to move");
waiting = false; // Just completed a successful move
}
}
}
}

Explanation / Answer

import java.util.logging.Level; import java.util.logging.Logger; public class TicTacToe extends javax.swing.JFrame { static int winComb[][] = {{1,2,3},{4,5,6},{7,8,9},{1,4,7},{2,5,8},{3,6,9},{1,5,9},{3,5,7}}; public static int[][] state = {{0,0,0},{0,0,0},{0,0,0}}; Player pl1 = new Human(); Player pl2 = new Computer("mind\layer"); public static int butClicked = 0; int w1=0 , w2 = 0 , dr = 0; public TicTacToe() { initComponents(); } public void start(){ if(w1==500)System.exit(0); int current = 1 , turn = 1; int w=0; while((w=checkWin(turn,state))==0) { if(current == 1) { pl1.playTurn(1,turn); refreshGrid(); current = 2; } else if(current == 2 ) { pl2.playTurn(2,turn); refreshGrid(); current = 1; } turn++; try {Thread.sleep(0);} catch (InterruptedException ex) {Logger.getLogger(TicTacToe.class.getName()).log(Level.SEVERE, null, ex);} } if(w==1) { pl1.notifyWin(1); pl2.notifyLose(2); print("Player 1 Won The Game !"); w1++; } else if(w==2) { pl2.notifyWin(1); pl1.notifyLose(2); print("Player 2 Won The Game !"); w2++; } else if(w==-1) { print("Game DRAW !"); dr++; } try {Thread.sleep(0);} catch (InterruptedException ex) {Logger.getLogger(TicTacToe.class.getName()).log(Level.SEVERE, null, ex);} } public void refreshGrid(){ b11.setText(state[0][0]==1?"X":(state[0][0]==2?"O":"")); b12.setText(state[0][1]==1?"X":(state[0][1]==2?"O":"")); b13.setText(state[0][2]==1?"X":(state[0][2]==2?"O":"")); b21.setText(state[1][0]==1?"X":(state[1][0]==2?"O":"")); b22.setText(state[1][1]==1?"X":(state[1][1]==2?"O":"")); b23.setText(state[1][2]==1?"X":(state[1][2]==2?"O":"")); b31.setText(state[2][0]==1?"X":(state[2][0]==2?"O":"")); b32.setText(state[2][1]==1?"X":(state[2][1]==2?"O":"")); b33.setText(state[2][2]==1?"X":(state[2][2]==2?"O":"")); jLabel1.setText(" X wins : "+w1); jLabel2.setText(" O wins : "+w2); jLabel3.setText(" Draws : "+dr); } public static int checkWin(int turn,int[][] st){ int ret = 0; String x =""; String o =""; int i=0 , j=0 , c=0 , p , q; for(p=0;p-1) { ret = 1; break; } if(o.indexOf((char)winComb[p][0]+'0')>-1 && o.indexOf((char)winComb[p][1]+'0')>-1 && o.indexOf((char)winComb[p][2]+'0')>-1) { ret = 2; break; } } return ret; } public void print(String s){ Notification.setText(" "+s); } public void gameInit(){ state[0][0] = 0; state[0][1] = 0; state[0][2] = 0; state[1][0] = 0; state[1][1] = 0; state[1][2] = 0; state[2][0] = 0; state[2][1] = 0; state[2][2] = 0; refreshGrid(); } @SuppressWarnings("unchecked") // //GEN-BEGIN:initComponents private void initComponents() { try { jPanel1 =(javax.swing.JPanel)java.beans.Beans.instantiate(getClass().getClassLoader(), "TicTacToe_jPanel1"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (java.io.IOException e) { e.printStackTrace(); } b21 = new javax.swing.JButton(); b11 = new javax.swing.JButton(); b22 = new javax.swing.JButton(); b12 = new javax.swing.JButton(); b13 = new javax.swing.JButton(); b23 = new javax.swing.JButton(); b31 = new javax.swing.JButton(); b32 = new javax.swing.JButton(); b33 = new javax.swing.JButton(); Notification = new javax.swing.JLabel(); jPanel2 = new javax.swing.JPanel(); jLabel1 = new javax.swing.JLabel(); jLabel2 = new javax.swing.JLabel(); jLabel3 = new javax.swing.JLabel(); jLabel4 = new javax.swing.JLabel(); jLabel5 = new javax.swing.JLabel(); jLabel6 = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); setAlwaysOnTop(true); setPreferredSize(new java.awt.Dimension(600, 400)); b21.setBackground(new java.awt.Color(255, 255, 255)); b21.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N b21.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); b21.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b21ActionPerformed(evt); } }); b11.setBackground(new java.awt.Color(255, 255, 255)); b11.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N b11.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); b11.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b11ActionPerformed(evt); } }); b22.setBackground(new java.awt.Color(255, 255, 255)); b22.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N b22.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); b22.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b22ActionPerformed(evt); } }); b12.setBackground(new java.awt.Color(255, 255, 255)); b12.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N b12.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); b12.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b12ActionPerformed(evt); } }); b13.setBackground(new java.awt.Color(255, 255, 255)); b13.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N b13.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); b13.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b13ActionPerformed(evt); } }); b23.setBackground(new java.awt.Color(255, 255, 255)); b23.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N b23.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); b23.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b23ActionPerformed(evt); } }); b31.setBackground(new java.awt.Color(255, 255, 255)); b31.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N b31.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); b31.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b31ActionPerformed(evt); } }); b32.setBackground(new java.awt.Color(255, 255, 255)); b32.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N b32.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); b32.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b32ActionPerformed(evt); } }); b33.setBackground(new java.awt.Color(255, 255, 255)); b33.setFont(new java.awt.Font("Arial", 1, 48)); // NOI18N b33.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR)); b33.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { b33ActionPerformed(evt); } }); Notification.setBackground(new java.awt.Color(255, 255, 0)); Notification.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N Notification.setForeground(new java.awt.Color(0, 0, 102)); Notification.setText("Tic - Tac - Toe"); Notification.setBorder(new javax.swing.border.MatteBorder(null)); jLabel1.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N jLabel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel2.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N jLabel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel3.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N jLabel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel4.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N jLabel4.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel5.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N jLabel5.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); jLabel6.setFont(new java.awt.Font("Arial", 0, 18)); // NOI18N jLabel6.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))); javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2); jPanel2.setLayout(jPanel2Layout); jPanel2Layout.setHorizontalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE) .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addContainerGap()) ); jPanel2Layout.setVerticalGroup( jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel2Layout.createSequentialGroup() .addContainerGap() .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) ); jLabel1.getAccessibleContext().setAccessibleName("l1"); jLabel1.getAccessibleContext().setAccessibleDescription(""); javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1); jPanel1.setLayout(jPanel1Layout); jPanel1Layout.setHorizontalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(Notification, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(b21, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(b11, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(b22, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(b23, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(b12, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(b13, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))) .addGroup(jPanel1Layout.createSequentialGroup() .addComponent(b31, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(b32, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(b33, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))) .addContainerGap()) ); jPanel1Layout.setVerticalGroup( jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(jPanel1Layout.createSequentialGroup() .addContainerGap() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addGroup(jPanel1Layout.createSequentialGroup() .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(b12, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(b13, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(b11, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(b22, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(b21, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(b23, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(b32, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(b31, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(b33, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE))) .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(Notification, javax.swing.GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE) .addContainerGap()) ); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 561, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE)) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 385, Short.MAX_VALUE) ); pack(); }// //GEN-END:initComponents private void b33ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b33ActionPerformed if(state[2][2]==0) butClicked = 9; }//GEN-LAST:event_b33ActionPerformed private void b32ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b32ActionPerformed if(state[2][1]==0) butClicked = 8; }//GEN-LAST:event_b32ActionPerformed private void b31ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b31ActionPerformed if(state[2][0]==0) butClicked = 7; }//GEN-LAST:event_b31ActionPerformed private void b23ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b23ActionPerformed if(state[1][2]==0) butClicked = 6; }//GEN-LAST:event_b23ActionPerformed private void b13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b13ActionPerformed if(state[0][2]==0) butClicked = 3; }//GEN-LAST:event_b13ActionPerformed private void b12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b12ActionPerformed if(state[0][1]==0) butClicked = 2; }//GEN-LAST:event_b12ActionPerformed private void b22ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b22ActionPerformed if(state[1][1]==0) butClicked = 5; }//GEN-LAST:event_b22ActionPerformed private void b11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b11ActionPerformed if(state[0][0]==0) butClicked = 1; }//GEN-LAST:event_b11ActionPerformed private void b21ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b21ActionPerformed if(state[1][0]==0) butClicked = 4; }//GEN-LAST:event_b21ActionPerformed public static void main(String args[]) { /* Set the Nimbus look and feel */ // /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(TicTacToe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(TicTacToe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(TicTacToe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(TicTacToe.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // TicTacToe t = new TicTacToe(); t.setVisible(true); while(true) { t.start(); t.gameInit(); } } // Variables declaration - do not modify//GEN-BEGIN:variables private javax.swing.JLabel Notification; public static javax.swing.JButton b11; public static javax.swing.JButton b12; public static javax.swing.JButton b13; public static javax.swing.JButton b21; public static javax.swing.JButton b22; public static javax.swing.JButton b23; public static javax.swing.JButton b31; public static javax.swing.JButton b32; public static javax.swing.JButton b33; private javax.swing.JLabel jLabel1; private javax.swing.JLabel jLabel2; private javax.swing.JLabel jLabel3; private javax.swing.JLabel jLabel4; private javax.swing.JLabel jLabel5; private javax.swing.JLabel jLabel6; private javax.swing.JPanel jPanel1; private javax.swing.JPanel jPanel2; // End of variables declaration//GEN-END:variables }