Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA PRACTICE QUESTION: You will create a tic tac toe game. Here are the steps. Declare a class called TicTacToe Frame. Here is the list

JAVA PRACTICE QUESTION:

image text in transcribed

You will create a tic tac toe game. Here are the steps.

Declare a class called TicTacToe Frame.

Here is the list of imports required

import java.awt.GridBagConstraints;

import java.awt.GridBagLayout;

import java.awt.Insets;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JTextField;

3. Create a class called TicTacToeFrame

Instance variables:

private JTextField winner; // Label for yearly salary private JTextField x00; private JTextField x01; private JTextField x02; private JTextField x10; private JTextField x11; private JTextField x12; private JTextField x20; private JTextField x21; private JTextField x22; public static String[][] matrix = new String[3][3]; private JButton playButton; // Triggers salary calculation private JButton clearButton; // Triggers clearing the field so you can play again

4. Begin the TicTacToeFrame no argument constructor

Initialize the matrix so each cell has a different number. This is required because you are going to check the matrix each time the play button is pushed looking for matches in row/columns or diagonals and you need them to all be different except the fields entered(x,o)

Make a nested loop

And inside the inner loop

matrix[i][j]=Integer.toString(((j+1)*(i+1)));// set each cell to different number

b.set the JFrame title and the GridBagConstraints

c. the nine textfields should be setup like this

x00TextField

x01TextField

x02TextField

x10TextField

x11TextField

x12TextField

x20TextField

x21TextField

x22TextField

PlayButton

WinnerLabel

ClearButton

x00 = new JTextField(1);

x00.setEditable(true);

x00.setText(" ");

d. Set up the Buttons - one for Play and the other Clear

playButton = new JButton("Play");

e. Add action listener to button

playButton.addActionListener(this);

f. Add the items created

Example

// Use a GridBagLayout setLayout(new GridBagLayout()); positionConst = new GridBagConstraints(); // Specify component's grid location positionConst.gridx = 0; positionConst.gridy = 0; // 10 pixels of padding around component positionConst.insets = new Insets(10, 10, 10, 10); // Add component using the specified constraints add(x00, positionConst);

g. Add the 9 fields , 2 buttons and 1 winner field

h. Last step in construct - use pack command

this.pack();

4. Create public void actionPerformed(ActionEvent event) {

There are listeners attached to the two buttons. First you have to test to see what button was pushed.

if(event.getSource()==playButton){

5. If the play button was pushed you want to read from all 9 fields and update the matrix if the String read from the field is x or o. Trim takes off any leading or trailing blanks

field=x00.getText().trim();

if(field.equals("x")||field.equals("o")){

matrix[0][0] = field;

Do this for all 9 fields. Pay attention to what we did in class about the difference inthe way an array is indexed and the way the grid is indexed.

6.At the end of the steps you do if Play is pushed you pass the matrix to a method to check if its got Tic Tac Toe

boolean ans= checkTTT(matrix);

if(ans)

winner.setText("Winner");

7.Write a checkTTT method that checks all the rows, columns, diagonals of matrix to see if they are the same String. If they are equals returns true. Otherwise it returns false

If none of the rows, columns, diagonals match, return false.

8. Go back to the actionPerformed method and add steps to do if the event was the Clear button. You will also need to reset the matrix. Copy code from step 4a here also.

x00.setText(" ");

9. Write the main method

Create the frame

TicTacToeFrame myFrame = new TicTacToeFrame();

b. Then set the default close operation, pack and set Visible(see book)

Last steps:

1. Give the computer a turn to pick a spot. Ask the player are they X or O. Then the computer is the other The computer would pick an open spot . After the user places - the computer would update the matrix. Should check after each - after user and after computer to see if winner

2. Make the above computer choices - rather then random - strategic. Have the computer decide what the update should be - not just what is open but what would help the computer win. It should be able to block the user or try to win. Must be go

TicTacToe 0 Play Winner Clear TicTacToe 0 Play Winner Clear

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

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions