Program has to have a user playing against a computer. Create a Tic Tac Toe game
ID: 3858181 • Letter: P
Question
Program has to have a user playing against a computer. Create a Tic Tac Toe game with a JPanel that uses an array of nine JButtons to represent the Tic Tac Toe grid. When the user clicks a button that has not already been taken, place an X on the button and then allow the computer to place an O on a different button. Announce the winner when either the computer or the player achieves three marks in sequence, or announce that the game was a tie. Save the game as JTicTacToe.java Add a graphic that displays a large letter representing the winning player of the JTicTacToe game. Draw a large X,O, or in case of a tie, an overlapping X and O in different colors. Save the game as JTicTacToe2.java
Explanation / Answer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JOptionPane;
class TicTacToe extends JFrame
{
JButton[][] buttons = new JButton[3][3];
JFrame frame = new JFrame("TicTacToe");
JButton reset = new JButton("Reset");
JOptionPane turn;
int moveCounter = 9;
boolean gameWon = false;
int WhoseTurn = 1;
public TicTacToe()
{
super();
frame.setSize(390, 400);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setResizable(false);
JButton[][] buttons = new JButton[3][3];
JFrame frame = new JFrame("TicTacToe");
JButton reset = new JButton("Reset");
JOptionPane turn;
}
private void check_win(int row, int col)
{
try {
if (buttons[0][2].getText()==buttons[1][2].getText()&& buttons[1][2].getText()==buttons[2][2].getText()&& buttons[2][2].getText()==buttons[0][2].getText()&& buttons[1][2].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[1][2].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[1][2].getText()+ " wins!!!");
}
if (buttons[0][1].getText()==buttons[1][1].getText()&& buttons[1][1].getText()==buttons[2][1].getText()&& buttons[2][1].getText()==buttons[0][1].getText()&& buttons[1][1].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[1][1].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[1][1].getText()+ " wins!!!");
}
if (buttons[0][0].getText()==buttons[1][0].getText()&& buttons[1][0].getText()==buttons[2][0].getText()&& buttons[2][0].getText()==buttons[0][0].getText()&& buttons[1][0].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[1][0].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[1][0].getText()+ " wins!!!");
}
if (buttons[2][0].getText()==buttons[2][1].getText()&& buttons[2][1].getText()==buttons[2][2].getText()&& buttons[2][2].getText()==buttons[2][0].getText()&& buttons[2][1].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[2][1].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[2][1].getText()+ " wins!!!");
}
if (buttons[1][0].getText()==buttons[1][1].getText()&& buttons[1][1].getText()==buttons[1][2].getText()&& buttons[1][2].getText()==buttons[1][0].getText()&& buttons[1][1].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[1][1].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[1][1].getText()+ " wins!!!");
}
if (buttons[0][0].getText()==buttons[0][1].getText()&& buttons[0][1].getText()==buttons[0][2].getText()&& buttons[0][2].getText()==buttons[0][0].getText()&& buttons[0][1].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[0][1].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[0][1].getText()+ " wins!!!");
}
if (buttons[0][0].getText()==buttons[1][1].getText()&& buttons[1][1].getText()==buttons[2][2].getText()&& buttons[2][2].getText()==buttons[0][0].getText()&& buttons[1][1].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[1][1].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[1][1].getText()+ " wins!!!");
}
if (buttons[0][2].getText()==buttons[1][1].getText()&& buttons[1][1].getText()==buttons[2][0].getText()&& buttons[2][0].getText()==buttons[0][2].getText()&& buttons[1][1].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[1][1].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[1][1].getText()+ " wins!!!");
}
}catch(Exception e) {
gameWon = true;
WhoseTurn = 0;
System.out.println("Stalemate");
JOptionPane.showMessageDialog(frame, "Stalemate");
}
}
private void computer_turn(int count)
{
int randomMove=count;
Random num = new Random();
randomMove = num.nextInt(randomMove)+1;
while(gameWon ==false & WhoseTurn ==2)
{
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--;
check_win(i, j);
WhoseTurn = 1;
}
}
}
}
}
}
private void game_intialise()
{
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(325,425));
menu.setPreferredSize(new Dimension(300,50));
game.setPreferredSize(new Dimension(300,300));
mainPanel.add(menu, BorderLayout.NORTH);
mainPanel.add(game, BorderLayout.SOUTH);
menu.add(reset, BorderLayout.NORTH);
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
{
public void actionPerformed(ActionEvent a)
{
if(gameWon == false)
{
if(a.getSource() == buttons[0][0])
{
buttons[0][0].setText("X");
buttons[0][0].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
computer_turn(moveCounter);
check_win(0,0);
}
else if(a.getSource() == buttons[0][1])
{
buttons[0][1].setText("X");
buttons[0][1].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
computer_turn(moveCounter);
check_win(0,1);
}
else if(a.getSource() == buttons[1][0])
{
buttons[1][0].setText("X");
buttons[1][0].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
computer_turn(moveCounter);
check_win(1,0);
}
else if(a.getSource() == buttons[1][1])
{
buttons[1][1].setText("X");
buttons[1][1].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
computer_turn(moveCounter);
check_win(1,1);
}
else if(a.getSource() == buttons[1][2])
{
buttons[1][2].setText("X");
buttons[1][2].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
computer_turn(moveCounter);
check_win(1,2);
}
else if(a.getSource() == buttons[2][2])
{
buttons[2][2].setText("X");
buttons[2][2].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
computer_turn(moveCounter);
check_win(2,2);
}
else if(a.getSource() == buttons[0][2])
{
buttons[0][2].setText("X");
buttons[0][2].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
computer_turn(moveCounter);
check_win(0,2);
}
else if(a.getSource() == buttons[2][1])
{
buttons[2][1].setText("X");
buttons[2][1].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
computer_turn(moveCounter);
check_win(2,1);
}
else if(a.getSource() == buttons[2][0])
{
buttons[2][0].setText("X");
buttons[2][0].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
computer_turn(moveCounter);
check_win(2,0);
}
}
if(a.getSource() == reset)
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
gameWon = false;
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
moveCounter = 9;
WhoseTurn = 1;
}
}
}
}
}
public static void main(String[] args)
{
TicTacToe game = new TicTacToe();
game.game_intialise();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.