Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'ve been looking at a lot of complex ones online, but that is far beyond what

ID: 3643386 • Letter: I

Question

I've been looking at a lot of complex ones online, but that is far beyond what we've learned and I can't even use it for help. Really stuck, any help is appreciated!


Design and code a swing GUI for a two-player tic-tac-toe game on a 3x3 board. The JFrame should use a BorderLayout with a JLabel in the NORTH region to display messages and a JPanel in the CENTER region to display the board. For the board in JPanel use a grid layout manager with a 3x3 layout of JButton's in each cell to display the game board. The button label should initially be blank. When a player clicks on an empty button an appropriate X or O should be placed in the label field of the button. If there's a winner then the program should display the winner in the JLabel at the top of the window. If all 9 cells have been filled without a winner the program should indicate there was a tie.

Explanation / Answer

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class tictactoe implements ActionListener{

int x = 0; //counter for x turn
int o = 0; //counter for o turn
int t = 0; //counter keeping track of what turn it is
int boardArray[][] = new int[3][3]; //the board

JFrame frame = new JFrame("Tick-tack-toe");

JPanel panel = new JPanel();

ImageIcon whitesquare = new ImageIcon("/home/dean/Java images/whitesquare.gif");
ImageIcon bluesquare = new ImageIcon("/home/dean/Java images/lightbluesquare.gif");
ImageIcon redx = new ImageIcon("/home/dean/Java images/redx.gif");
ImageIcon bluecircle = new ImageIcon("/home/dean/Java images/bluecircle.gif");

JButton b00 = new JButton(whitesquare);
JButton b01 = new JButton(whitesquare);
JButton b02 = new JButton(whitesquare);
JButton b10 = new JButton(whitesquare);
JButton b11 = new JButton(whitesquare);
JButton b12 = new JButton(whitesquare);
JButton b20 = new JButton(whitesquare);
JButton b21 = new JButton(whitesquare);
JButton b22 = new JButton(whitesquare);

GridBagConstraints b00c = new GridBagConstraints();
GridBagConstraints b01c = new GridBagConstraints();
GridBagConstraints b02c = new GridBagConstraints();
GridBagConstraints b10c = new GridBagConstraints();
GridBagConstraints b11c = new GridBagConstraints();
GridBagConstraints b12c = new GridBagConstraints();
GridBagConstraints b20c = new GridBagConstraints();
GridBagConstraints b21c = new GridBagConstraints();
GridBagConstraints b22c = new GridBagConstraints();

public void constraints()
{
b00c.gridx = 0;
b00c.gridy = 0;
b00c.weightx = 1;
b00c.weighty = 1;

b01c.gridx = 1;
b01c.gridy = 0;
b01c.weightx = 1;
b01c.weighty = 1;

b02c.gridx = 2;
b02c.gridy = 0;
b02c.weightx = 1;
b02c.weighty = 1;

b10c.gridx = 0;
b10c.gridy = 1;
b10c.weightx = 1;
b10c.weighty = 1;

b11c.gridx = 1;
b11c.gridy = 1;
b11c.weightx = 1;
b11c.weighty = 1;

b12c.gridx = 2;
b12c.gridy = 1;
b12c.weightx = 1;
b12c.weighty = 1;

b20c.gridx = 0;
b20c.gridy = 2;
b20c.weightx = 1;
b20c.weighty = 1;

b21c.gridx = 1;
b21c.gridy = 2;
b21c.weightx = 1;
b21c.weighty = 1;

b22c.gridx = 2;
b22c.gridy = 2;
b22c.weightx = 1;
b22c.weighty = 1;


makeFrame();
}

public void makeFrame()
{
panel.setLayout(new GridBagLayout());

frame.setSize(300, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

panel.add(b00, b00c);
panel.add(b01, b01c);
panel.add(b02, b02c);
panel.add(b10, b10c);
panel.add(b11, b11c);
panel.add(b12, b12c);
panel.add(b20, b20c);
panel.add(b21, b21c);
panel.add(b22, b22c);

frame.add(panel);

xturn();
}

public void xturn()
{
t++;
x = 1;
listenForClick();
}

public void oturn()
{
t++;
o = 1;
listenForClick();
}

public void listenForClick()
{
b00.addActionListener(this);
b01.addActionListener(this);
b02.addActionListener(this);
b10.addActionListener(this);
b11.addActionListener(this);
b12.addActionListener(this);
b20.addActionListener(this);
b21.addActionListener(this);
b22.addActionListener(this);
}

public void check()
{
if(t < 5)
{
if(x == 1){x = 0; oturn();return;}
else if(o == 1){o = 0; xturn();return;}
}
else if(t >= 5)
{
//this is where rows of 3 will be checked for
if(x == 1){x = 0; oturn(); return;}
else if(o == 1){o = 0; xturn(); return;}
}
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource() == b00)
{
if(x == 1){boardArray[0][0] = 1; b00.setIcon(redx); check();return;}
else if(o == 1){boardArray[0][0] = 0; b00.setIcon(bluecircle); check();return;}
}

if(e.getSource() == b01)
{
if(x == 1){boardArray[1][0] = 1; b01.setIcon(redx); check();return;}
else if(o == 1){boardArray[1][0] = 0; b01.setIcon(bluecircle); check();return;}
}

if(e.getSource() == b02)
{
if(x == 1){boardArray[2][0] = 1; b02.setIcon(redx); check();return;}
else if(o == 1){boardArray[2][0] = 0; b02.setIcon(bluecircle); check();return;}
}

if(e.getSource() == b10)
{
if(x == 1){boardArray[0][1] = 1; b10.setIcon(redx); check();return;}
else if(o == 1){boardArray[0][1] = 0; b10.setIcon(bluecircle); check();return;}
}

if(e.getSource() == b11)
{
if(x == 1){boardArray[1][1] = 1; b11.setIcon(redx); check();return;}
else if(o == 1){boardArray[1][1] = 0; b11.setIcon(bluecircle); check();return;}
}

if(e.getSource() == b12)
{
if(x == 1){boardArray[2][1] = 1; b12.setIcon(redx); check();return;}
else if(o == 1){boardArray[2][1] = 0; b12.setIcon(bluecircle); check();return;}
}

if(e.getSource() == b20)
{
if(x == 1){boardArray[0][2] = 1; b20.setIcon(redx); check();return;}
else if(o == 1){boardArray[0][2] = 0; b20.setIcon(bluecircle); check();return;}
}

if(e.getSource() == b21)
{
if(x == 1){boardArray[1][2] = 1; b21.setIcon(redx); check();return;}
else if(o == 1){boardArray[1][2] = 0; b21.setIcon(bluecircle); check();return;}
}

if(e.getSource() == b22)
{
if(x == 1){boardArray[2][2] = 1; b22.setIcon(redx); check();return;}
else if(o == 1){boardArray[2][2] = 0; b22.setIcon(bluecircle); check();return;}
}
}

public static void main(String[] args)
{
tictactoe qw = new tictactoe();
qw.constraints();
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote