Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please fill out the code in java ( TicTacToeGUI . java ) . Thank you! TicTacToeGUI.java ( private class State should not be altered! )
Please fill out the code in java TicTacToeGUIjava Thank you!
TicTacToeGUI.java private class State should not be altered!:
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public final class TicTacToeGUI
public static void mainString args
TicTacToeGUI t new TicTacToeGUI;
private State state;
private GUI gui;
private void startNewGame
if gui null
gui.dispose;
state new State;
gui new GUI;
step define our custom default constructor for TicTacToeGUI
we can simply call the startNewGame in this default constructor
so that data members will be initialzed
the state class manages the logic part of this game do not alter the code within this class
private class State
private char XO X;
private void prepareForNextMove
XO XO XO : X;
private final char board new char
;
private void applyMoveint row, int col
boardrowcol XO;
private boolean someoneWon
if board && board board && board board
return true;
if board && board board && board board
return true;
if board && board board && board board
return true;
if board && board board && board board
return true;
if board && board board && board board
return true;
if board && board board && board board
return true;
if board && board board && board board
return true;
if board && board board && board board
return true;
return false;
private boolean boardFull
return board && board && board &&
board && board && board &&
board && board && board;
GUI is reponsible for the user interaction
step provide a default constructor for class GUI
important parts
a GUI is a subclass of JFrame, in the default contructor
set the basic setting for JFrame, eg setSize, setTitle, setDefaultCloseOperation and so on
b make use to Grid layout for JFrameJPanelx grid
c need to add custom buttons Tile in total for this grid, inital text part
step implement our custom button Tile
a provide a row and col data member for Tile so that we can know which button we are clicking
b provide an action listener for it
b implement our custom action listener, override the actionPerformed method
b tell what is the source button of this event getSource
b check if this button has been occupied by a previous move
b if occupied, show up a dialog telling user about this
b if not occupied, change the text part to be X or O by using setText Method
b call processValidMove method to hanld status
step change the status as well, this part should be implemented in processValidMove
if it's a draw, show up a dialog, start a new game
if someone wins, also show up a dialog, start a new game
if the game is not finished, prepare for the next move
private class GUI extends JFrame
private class Tile extends JButton
private void processValidMoveint row, int col
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started