java code please Add event handlers to the buttons in your TicTacToe GUI so that label of the
first button clicked changes to X, the second button clicked changes to O, the third button clicked
changes to X, etc. Clicking a button that already contains an X or O should have no effect. The
Game Status label at the bottom should start out saying \"Player 1\'s turn\", and then change to
\"Player 2\'s turn\" after the first click, then back to \"Player 1\'s turn\" and so on. You do not
need to recognize when the game has ended, but that might be a good challenge to take on if you
have some extra time.
Solution
A)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TicGUI extends JFrame
{
JFrame frame = new JFrame(\"TicTacToe\");
JButton[][] buttons = new JButton[3][3];
JButton start = new JButton(\"Start\");
JButton reset = new JButton(\"Reset\");
JOptionPane turn;
int moveCounter = 9;
boolean gameWon = false;
public TicGUI()
{
super();
frame.setSize(350, 450);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
private void checkWin(int row, int col)
{
if(buttons[row][0].getText()==buttons[row][1].getText()&&
buttons[row][1].getText()==buttons[row][2].getText())
{
gameWon = true;
System.out.println(buttons[row][0].getText()+ \" wins!!!\");
}
else if(buttons[0][col].getText()==buttons[1][col].getText()&&
buttons[1][col].getText()==buttons[2][col].getText())
{
gameWon = true;
System.out.println(buttons[row][0].getText()+ \" wins!!!\");
}
}
private void compTurn(int count)
{
int randomMove=count;
Random num = new Random();
randomMove = num.nextInt(randomMove)+1;
while(gameWon ==false)
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(buttons[i][j].isEnabled()==true)
{
randomMove;
if(randomMove==0 )
{
buttons[i][j].setText(\"O\");
buttons[i][j].setEnabled(false);
moveCounter--;
checkWin(i, j);
}
}
}
}
}
}
private void initialize()
{
JPanel mainPanel = new JPanel(new BorderLayout());
JPanel menu = new JPanel(new BorderLayout());
JPanel game = new JPanel(new GridLayout(3,3));
frame.add(mainPanel);
mainPanel.setPreferredSize(new Dimension(215,315));
menu.setPreferredSize(new Dimension(300,50));
game.setPreferredSize(new Dimension(300,300));
mainPanel.add(menu, BorderLayout.NORTH);
mainPanel.add(game, BorderLayout.SOUTH);
menu.add(start, BorderLayout.WEST);
menu.add(reset, BorderLayout.EAST);
start.addActionListener(new myActionListener());
reset.addActionListener(new myActionListener());
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
buttons[i][j] = new JButton();
buttons[i][j].setText(\"\");
buttons[i][j].setVisible(true);
game.add(buttons[i][j]);
buttons[i][j].addActionListener(new myActionListener());
}
}
}
private class myActionListener implements ActionListener
{ //Implementing action listener for buttons
public void actionPerformed(ActionEvent a)
{
if(a.getSource() == buttons[0][0])
{
buttons[0][0].setText(\"X\");
buttons[0][0].s.
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Â
java code please Add event handlers to the buttons in your TicTacToe.pdf
1. java code please Add event handlers to the buttons in your TicTacToe GUI so that label of the
first button clicked changes to X, the second button clicked changes to O, the third button clicked
changes to X, etc. Clicking a button that already contains an X or O should have no effect. The
Game Status label at the bottom should start out saying "Player 1's turn", and then change to
"Player 2's turn" after the first click, then back to "Player 1's turn" and so on. You do not
need to recognize when the game has ended, but that might be a good challenge to take on if you
have some extra time.
Solution
A)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TicGUI extends JFrame
{
JFrame frame = new JFrame("TicTacToe");
JButton[][] buttons = new JButton[3][3];
JButton start = new JButton("Start");
JButton reset = new JButton("Reset");
JOptionPane turn;
int moveCounter = 9;
boolean gameWon = false;
public TicGUI()
{
super();
frame.setSize(350, 450);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
}
private void checkWin(int row, int col)
{
if(buttons[row][0].getText()==buttons[row][1].getText()&&