Answered step by step
Verified Expert Solution
Link Copied!

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 (TicTacToeGUI.java). 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 main(String[] 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 1. 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 =='X')?'O' : 'X';
}
private final char[][] board = new char[][]{{'','',''},
{'','',''},
{'','',''}};
private void applyMove(int row, int col){
board[row][col]= XO;
}
private boolean someoneWon(){
if (''!= board[0][0] && board[0][0]== board[0][1] && board[0][1]== board[0][2]){
return true;
}
if (''!= board[1][0] && board[1][0]== board[1][1] && board[1][1]== board[1][2]){
return true;
}
if (''!= board[2][0] && board[2][0]== board[2][1] && board[2][1]== board[2][2]){
return true;
}
if (''!= board[0][0] && board[0][0]== board[1][0] && board[1][0]== board[2][0]){
return true;
}
if (''!= board[0][1] && board[0][1]== board[1][1] && board[1][1]== board[2][1]){
return true;
}
if (''!= board[0][2] && board[0][2]== board[1][2] && board[1][2]== board[2][2]){
return true;
}
if (''!= board[0][0] && board[0][0]== board[1][1] && board[1][1]== board[2][2]){
return true;
}
if (''!= board[0][2] && board[0][2]== board[1][1] && board[1][1]== board[2][0]){
return true;
}
return false;
}
private boolean boardFull(){
return board[0][0]!='' && board[0][1]!='' && board[0][2]!='' &&
board[1][0]!='' && board[1][1]!='' && board[1][2]!='' &&
board[2][0]!='' && board[2][1]!='' && board[2][2]!='';
}
}
// GUI is reponsible for the user interaction
// step 2. 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, e.g. setSize, setTitle, setDefaultCloseOperation and so on
// b. make use to Grid layout for JFrame/JPanel,3x3 grid
// c. need to add 9 custom buttons (Tile) in total for this grid, inital text part ""
// step 3. 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
// b1. implement our custom action listener, override the actionPerformed method
// b2. tell what is the source button of this event (getSource())
// b3. check if this button has been occupied by a previous move
// b4. if occupied, show up a dialog telling user about this
// b5. if not occupied, change the text part to be 'X' or 'O' by using setText() Method
// b6. call processValidMove method to hanld status
// step 4. 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 processValidMove(int row, int col){
}
}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 25th European Conference Adbis 2021 Tartu Estonia August 24 26 2021 Proceedings Lncs 12843

Authors: Ladjel Bellatreche ,Marlon Dumas ,Panagiotis Karras ,Raimundas Matulevicius

1st Edition

3030824713, 978-3030824716

More Books

Students also viewed these Databases questions

Question

What is meant by planning or define planning?

Answered: 1 week ago

Question

Define span of management or define span of control ?

Answered: 1 week ago

Question

What is meant by formal organisation ?

Answered: 1 week ago

Question

What is meant by staff authority ?

Answered: 1 week ago