Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package Lab 0 4 ; import javax.swing.JFrame; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel;

package Lab04;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
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.JPanel;
public class TicTacToeFrame extends JFrame implements ActionListener
{
//Define the game components
private JLabel lblStatus; //The label to show the game status
private JPanel gamePanel; //The panel that displays the button grid
private JButton[][] btnGrid; //A 2D array of buttons on the grid
private boolean xTurn; //Stores the player turn(true => X, false =>0)
private boolean bFinished; //Stores whether the game is finished or not
public static void main(String[] args)
{
TicTacToeFrame frame = new TicTacToeFrame();
frame.setVisible(true);
}
//Constructor method
public TicTacToeFrame()
{
//Add code for this constructor method
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(500,500); //Set form size width =500 and height =500
this.setLocationRelativeTo(null); //Make the frame appearing in the center of the screen
this.setTitle("Tic Tac Toe");
this.setLayout(new BorderLayout());
xTurn = true; //Player X starts first
bFinished = false; //The game is not over at first
lblStatus = new JLabel("Player X turn");
lblStatus.setFont(new Font("Arial", Font.BOLD, 40));
this.add(lblStatus, BorderLayout.NORTH);
gamePanel = new JPanel(new GridLayout(3,3)); //Game panel is a grid size 3x3
this.add(gamePanel, BorderLayout.CENTER);
btnGrid = new JButton[3][3]; //Initialize the button grid
for(int i =0; i <3; i++)
for(int j =0; j <3; j++)
{
btnGrid[i][j]= new JButton();
btnGrid[i][j].setFont(new Font("Arial", Font.BOLD, 80));
btnGrid[i][j].setBackground(new Color(115,214,185));
btnGrid[i][j].setOpaque(true);
btnGrid[i][j].addActionListener(this); //register this as the event handler
gamePanel.add(btnGrid[i][j]);
}
}
//This method handles the button click event
@Override
public void actionPerformed(ActionEvent e)
{
if(bFinished)
{
return;
}
for(int i =0; i <3; i++)
for(int j =0; j <3; j++)
if(e.getSource()== btnGrid[i][j])//Event source is button at (i, j)
if(btnGrid[i][j].getText().isEmpty())//Button (i, j) is not clicked before
{
btnGrid[i][j].setForeground(xTurn ? Color.red : Color.yellow);
btnGrid[i][j].setText(xTurn ?"X" : "O");
xTurn =!xTurn; //Invert xTurn to change the player turn
lblStatus.setText(String.format("Player %s turn", xTurn ?"X" : "O"));
checkWin(); //check if the game is finished or not
}
}
//This method checks if the game is finished or not
public void checkWin()
{
//Check rows
for(int i =0; i <3; i++)
{
if(!btnGrid[i][0].getText().isEmpty()//The player has put symbol here already
&& btnGrid[i][0].getText().equals(btnGrid[i][1].getText())
&& btnGrid[i][1].getText().equals(btnGrid[i][2].getText()))
{
lblStatus.setText(String.format("Player %s wins", btnGrid[i][0].getText()));
bFinished = true;
}
}
//Check columns
for(int i =0; i <3; i++)
{
if(!btnGrid[0][i].getText().isEmpty()//The player has put symbol here already
&& btnGrid[0][i].getText().equals(btnGrid[1][i].getText())
&& btnGrid[1][i].getText().equals(btnGrid[2][i].getText()))
{
lblStatus.setText(String.format("Player %s wins", btnGrid[0][i].getText()));
bFinished = true;
}
}
//check diagonal 1
for(int i =0; i <3; i++)
{
if(!btnGrid[0][0].getText().isEmpty()//The player has put symbol here already
&& btnGrid[0][0].getText().equals(btnGrid[1][1].getText())
&& btnGrid[1][1].getText().equals(btnGrid[2][2].getText()))
{
lblStatus.setText(String.format("Player %s wins", btnGrid[0][0].getText()));
bFinished = true;
}
}
//check diagonal 2
for(int i =0; i <3; i++)
{
if(!btnGrid[0][2].getText().isEmpty()//The player has put symbol here already
&& btnGrid[0][2].getText().equals(btnGrid[1][1].getText())
&& btnGrid[1][1].getText().equals(btnGrid[2][0].getText()))
{
lblStatus.setText(String.format("Player %s wins", btnGrid[0][2].getText()));
bFinished = true;
}
}
//Lab assignment
//Check if the game is a tie
}
}

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

Students also viewed these Databases questions