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

GUI based user interface Tic tac toe game program with main class you will imple

ID: 3695014 • Letter: G

Question

GUI based user interface Tic tac toe game program with main class

you will implement a simple Tic Tac Toe game using the Java Swing class.

The GUI will consist of a 3x3 panel of buttons representing the board as well as a status text field at the bottom that displays which player’s turn it is and whether a player has won.

Your game should support play between two players and does not need to implement any sort of artificial opponent.

Your program should contain logic that keeps track of which boxes (buttons) are currently empty or occupied with an X or an O. Once a box is occupied with an X or an O, then neither opp onent should be able to select that box. Once a player wins the game, then the status bar should display the winner.

Recall that a game is won when a player selects three boxes in a row horizontally, vertically, or diagonally. Some games end in a draw, i.e., no player wins.

As far as presentation is concerned, the X’s and O’s should be rendered in a non-default font (i.e., you must specify a font… I used Arial Bold Size 24) and must use interesting (i.e., non-default black text on gray background) colors.

Explanation / Answer

CODE:

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class tictocfrme extends JFrame
{

JButton [][] btns= new JButton[3][3];
JTextField stsbar;
Gamepnl pnl;
Integer trn;
Gamelstnr lstnr=new Gamelstnr();
Integer count;

public tictocfrme()
{
setLayout(new BorderLayout());

pnl=new Gamepnl();
add(pnl,BorderLayout.CENTER);

stsbar=new JTextField("Player 1 choice");
stsbar.setEditable(false);
add(stsbar,BorderLayout.SOUTH);

setTitle("Tic Tac Toe!");
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(400,400,300,300);
}

class Gamepnl extends Jpanel
{

public Gamepnl()
{
setLayout(new GridLayout(3,3));
trn =1;
count=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++) {
btns[i][j]=new JButton();
btns[i][j].putClientProperty("INDEX", new Integer[]{i,j});
btns[i][j].putClientProperty("OWNER", null);
btns[i][j].addActionlstnr(lstnr);
add(btns[i][j]);
}
}
}

class Gamelstnr implements Actionlistener
{
public void actionPerformed(ActionEvent e)
{
count++;
JButton b=(JButton)e.getSource();
Integer[]index=(Integer[]) b.getClientProperty("INDEX");

//System.out.println(trn); //trn // //System.out.println("["+index[0]+"]"+"["+index[1]+"]"); //
b.putClientProperty("OWNER", trn);
Icon ico=new ImageIcon(trn.toString()+".gif");
b.setIcon(ico);
b.setEnabled(false);
boolean result=chckvictory(index);
if(result)
{
JOptionPane.showMessageDialog(null, "Player "+trn.toString()+" Wins");
initComponents();
}
else
{
if(trn==1)
{
trn=2;
stsbar.setText("Player2's trn");
}
else
{
trn=1;
stsbar.setText("Player1's trn");
}
}
if(count==9)
{
JOptionPane.showMessageDialog(null, "Match is a draw!");
initComponents();

}

}

Integer getonr(JButton b)
{
retrn (Integer)b.getClientProperty("OWNER");
}

//prntbttnmap for Diagnostics
void prntbttnmap(Integer [][]bMap)
{
for(int i=0;i for(int j=0;j System.out.print(bMap[i][j]+" ");
System.out.println("");
}
}

boolean chckvictory(Integer [] index)
{
/*Integer[][]buttonMap=new Integer[][] {
{ getonr(btns[0][0]),getonr(btns[0][1]),getonr(btns[0][2])},
{ getonr(btns[1][0]),getonr(btns[1][1]),getonr(btns[1][2])},
{ getonr(btns[2][0]),getonr(btns[2][1]),getonr(btns[2][2])}
};

prntbttnmap(buttonMap); */

Integer a=index[0];
Integer b=index[1];
int i;

//check row
for(i=0;i<3;i++) {
if(getonr(btns[a][i])!=getonr(btns[a][b]))
break;
}
if(i==3)
retrn true;

//check column
for(i=0;i<3;i++) {
if(getonr(btns[i][b])!=getonr(btns[a][b]))
break;
}
if(i==3)
retrn true;

//check diagonal
if((a==2&&b==2)||(a==0&&b==0)||(a==1&&b==1)||(a==0&&b==2)||(a==2&&b==0))
{
//left diagonal
for(i=0;i if(getonr(btns[i][i])!=getonr(btns[a][b]))
break;
if(i==3)
retrn true;

//right diagonal
if((getonr(btns[0][2])==getonr(btns[a][b]))&&(getonr(btns[1][1])==getonr(btns[a][b]))&&(getonr(btns[2][0])==getonr(btns[a][b])))
retrn true;

}

retrn false;

}
}

void initComponents()
{
for(int i=0;i<3;i++)   
for(int j=0;j<3;j++) {
btns[i][j].putClientProperty("INDEX", new Integer[]{i,j});
btns[i][j].putClientProperty("OWNER",null);
btns[i][j].setIcon(null);
btns[i][j].setEnabled(true);
trn=1;
count=0;
stsbar.setText("Player1's trn");

}
}

}

class tictactoe {

public static void main(String[] args) {
EventQueue.invokeLater(new Runnable(){
public void run()
{
tictocfrme frame=new tictocfrme();
}
});

}

}