Tic-Tac-Toe Simulator This is done on a form, not a console application. Create
ID: 3689679 • Letter: T
Question
Tic-Tac-Toe Simulator This is done on a form, not a console application. Create an application that simulates a game of tic-tac-toe. The form shown in the figure uses eight large Label controls to display the Xs and Os. The application should use a two-dimensional int array to simulate the game board in memory. When the user clicks the New Game button, the application should step through the array, storing a random number in the range of 0 through 1 in each element. The number 0 represents the letter O, and the number 1 represents the letter X. The form should then be updated to display the game board. The application should display a message indicating whether player X won, player Y won, or the game was a tie. Once again, This is done on a form, not a console application. Thanks!Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TicTacToeSimulator extends JFrame
{
//new frame declared
JFrame myForm = new JFrame("TicTacToeSimulator");
//buttons array and start reset buttons declared
JButton[][] gameButtons = new JButton[3][3];
JButton startGame = new JButton("start Game");
JButton resetGame = new JButton("Reset Game");
JOptionPane userTurn;
int counterMove = 9;
boolean isWon = false;
//consstructor declared
public TicTacToeSimulator()
{
super();
myForm.setSize(450, 550);
myForm.setDefaultCloseOperation(EXIT_ON_CLOSE);
myForm.setVisible(true);
myForm.setResizable(false);
}
//checking user wins or not
private void checkUserWin(int row, int col)
{
//checking all ways of winning chances
if(gameButtons[row][0].getText().equals(gameButtons[row][1].getText()) && gameButtons[row][1].getText().equals(gameButtons[row][2].getText()))
{
isWon = true;
System.out.println(gameButtons[row][0].getText()+ " won");
}
else if(gameButtons[0][col].getText().equals(gameButtons[1][col].getText())&& gameButtons[1][col].getText().equals(gameButtons[2][col].getText()))
{
isWon = true;
System.out.println(gameButtons[row][0].getText()+ " won");
}
}
//getting computer turn
private void computerTurn(int count)
{
int computerMove=count;
Random number = new Random();
computerMove = number.nextInt(computerMove)+1;
//creating grid on form .. wher we hve to play
while(isWon ==false)
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
if(gameButtons[i][j].isEnabled()==true)
{
computerMove--;
if(computerMove==0 )
{
gameButtons[i][j].setText("O");
gameButtons[i][j].setEnabled(false);
counterMove--;
checkUserWin(i, j); //after computer move .. checking wins or not
}
}
}
}
}
}
//initializing board
private void initialize()
{
//creating panel
JPanel tictacToePanel = new JPanel(new BorderLayout());
JPanel displayMenu = new JPanel(new BorderLayout());
JPanel Game = new JPanel(new GridLayout(3,3));
myForm.add(tictacToePanel);
//adding dimensions to all elements
tictacToePanel.setPreferredSize(new Dimension(325,425));
displayMenu.setPreferredSize(new Dimension(300,50));
Game.setPreferredSize(new Dimension(300,300));
tictacToePanel.add(displayMenu, BorderLayout.NORTH);
tictacToePanel.add(Game, BorderLayout.SOUTH);
//adding start, reset buttons to panel
displayMenu.add(startGame, BorderLayout.WEST);
displayMenu.add(resetGame, BorderLayout.EAST);
startGame.addActionListener(new myActionListener());
resetGame.addActionListener(new myActionListener());
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
gameButtons[i][j] = new JButton();
gameButtons[i][j].setText("");
gameButtons[i][j].setVisible(true);
Game.add(gameButtons[i][j]);
//adding action listerners to buttons
gameButtons[i][j].addActionListener(new myActionListener());
}
}
}
private class myActionListener implements ActionListener
{
public void actionPerformed(ActionEvent a)
{
//printing user choice symbol either 'X' or 'O'
if(a.getSource() == gameButtons[0][0]) //getting button pressed
{
gameButtons[0][0].setText("X");
gameButtons[0][0].setEnabled(false);
counterMove--;
computerTurn(counterMove);
checkUserWin(0,0);
}
else if(a.getSource() == gameButtons[0][1])
{
gameButtons[0][1].setText("X");
gameButtons[0][1].setEnabled(false);
counterMove--;
computerTurn(counterMove);
checkUserWin(0,1);
}
else if(a.getSource() == gameButtons[1][0])
{
gameButtons[1][0].setText("X");
gameButtons[1][0].setEnabled(false);
counterMove--;
computerTurn(counterMove);
checkUserWin(1,0);
}
else if(a.getSource() == gameButtons[1][1])
{
gameButtons[1][1].setText("X");
gameButtons[1][1].setEnabled(false);
counterMove--;
computerTurn(counterMove);
checkUserWin(1,1);
}
else if(a.getSource() == gameButtons[1][2])
{
gameButtons[1][2].setText("X");
gameButtons[1][2].setEnabled(false);
counterMove--;
computerTurn(counterMove);
checkUserWin(1,2);
}
else if(a.getSource() == gameButtons[2][2])
{
gameButtons[2][2].setText("X");
gameButtons[2][2].setEnabled(false);
counterMove--;
computerTurn(counterMove);
checkUserWin(2,2);
}
else if(a.getSource() == gameButtons[0][2])
{
gameButtons[0][2].setText("X");
gameButtons[0][2].setEnabled(false);
counterMove--;
computerTurn(counterMove);
checkUserWin(0,2);
}
else if(a.getSource() == gameButtons[2][1])
{
gameButtons[2][1].setText("X");
gameButtons[2][1].setEnabled(false);
counterMove--;
computerTurn(counterMove);
checkUserWin(2,1);
}
else if(a.getSource() == gameButtons[2][0])
{
gameButtons[2][0].setText("X");
gameButtons[2][0].setEnabled(false);
counterMove--;
computerTurn(counterMove);
checkUserWin(2,0);
}
else if(a.getSource() == startGame)
{
userTurn = new JOptionPane("Do you want to start? ",JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
startGame.setEnabled(false);
}
else if(a.getSource() == resetGame)
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
gameButtons[i][j].setText("");
gameButtons[i][j].setEnabled(true);
isWon = false;
}
}
}
}
}
public static void main(String[] args)
{
TicTacToeSimulator Game = new TicTacToeSimulator(); //initializr game and start game
Game.initialize();
}
}