For this project, you will be implementing a working two-player game of Tic-Tac-Toe, using the attached (partial) implementation as a starting point. The classes used in this implementation uses the Model-View-Controller design pattern we have discussed in class; the Model encapsulates all application data, the View includes all GUI and presentation code, and the Controller receives input from the View and coordinates all communication between the Model and the View.

Inside the class files, you will find comments which indicate the portions of the code which are missing, as well as temporary code which will need to be removed later. To complete this assignment, make the corresponding changes and test your completed game with a variety of board sizes.

Your program should accept a command-line argument which represents the size (the width and height) of the board. For example, here is a way to start the game at the command prompt from the root of your NetBeans project folder, with an argument of 5 for a 5 x 5 board (assuming that your compiled JAR is named "TicTacToe.jar"):

java -jar dist\\TicTacToe.jar 5

If no argument is specified, the game should default to a traditional 3 x 3 board.

Player 1 ("X") makes the first move, then Player 2 ("O"). As the two players take turns, making their moves by clicking on the corresponding squares, the program should place the player's marks into the squares by setting the text of the buttons, and should check after each move to see if either Player 1 or Player 2 is the winner, or if the game is a tie. Your program should be able to do this for game boards of any size!

The GUI

Your program should use the Swing toolkit in Java to implement a point-and-click graphical user interface (GUI), as illustrated in the sample screenshots below. Most of the GUI-related code has already been supplied for you in the partial implementation! The remainder of this section describes the GUI-related code in more detail.

As discussed in class, your program should extend the JPanel class and implement the ActionListener interface. In your JPanel, the squares of the board should be implemented as a two-dimensional array of JButton objects with a preferred size of 64 x 64 pixels, and whose names are set to "SquareXY" (replacing X with the row and Y with the column):

squares = new JButton[width][width];
// loop through every row and col
squares[row][col] = new JButton();
squares[row][col].addActionListener(this);
squares[row][col].setName("Square" + row + col);
squares[row][col].setPreferredSize(new Dimension(64,64));
// add button to JPanel

In addition, to display the winning player at the end of the game, you should add a JLabel called "ResultLabel" to the bottom of the panel:

resultLabel = new JLabel();
resultLabel.setName("ResultLabel");

(The reason these names are so important is that, to be able to identify the X and Y coordinates of the buttons being clicked, it is necessary to "embed" these coordinates into the element names.)

To get the correct button layout, I suggest creating a separate JPanel as a "container" for the buttons, using the GridLayout layout manager. Create the new JPanel in the constructor for your GUI class ...

JPanel squaresPanel = new JPanel(new GridLayout(width,width));

... and add the buttons to this JPanel as you create them. Finally, add the panel to the GUI class (which is itself a JPanel):

add(squaresPanel);

When the players click on the buttons, the button text should be set to the players' marks, "X" or "O"; after a winner is determined, the buttons should be disabled so that no further moves can be entered. Display output messages (such as the result at the end of the game) in ResultLabel, as shown below. In your main class, when you create a new JFrame to display your main JPanel (called "view" in this example), its name should be set to "Tic-Tac-Toe":

JFrame win = new JFrame("Tic-Tac-Toe");
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.add(view);
win.pack();
win.setVisible(true);

Examples

Shown below are some sample games, with the final outcome displayed in the label. If you reproduce the moves shown in these samples, your program should arrive at the same outcomes!

X Wins (Diagonal): see image.

O Wins (Horizontal): see image.

A Tie (all squares filled with no winner): see image.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.