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

THATS MY PROGRAM BUT PLEASE RUN MY PROGRAM THEN YOU WILL SEE WHAT I NEED AND PLE

ID: 3634618 • Letter: T

Question

THATS MY PROGRAM BUT PLEASE RUN MY PROGRAM THEN YOU WILL SEE WHAT I NEED AND PLEASE HELP ME OUT .....


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

public class tictactoe extends JApplet{
private char whoseTurn = 'X';
private Cell[][] cells = new Cell[3][4];
private JLabel jlblStatus = new JLabel("X's turn to play.");

public tictactoe() {
JPanel p = new JPanel(new GridLayout(3,4,0,0));

for(int i = 0; i < 3; i++)
for(int j = 0; j < 4; j++)
{
if(i==0 && j==3)
{
//add button
p.add(new JButton());
}
else if(i==1 && j==3)
{
//add regular panel for number of games won
p.add(new JPanel());
}
else
{
p.add(cells[i][j] = new Cell());
}
}

p.setBorder(new LineBorder(Color.red, 1));
jlblStatus.setBorder(new LineBorder(Color.yellow, 1));

add(p, BorderLayout.CENTER);
add(jlblStatus, BorderLayout.SOUTH);
}

public boolean isFull() {
for(int i = 0; i < 3; i++)
for(int j = 0; j < 4; j++)
if(cells[i][j].getToken() == ' ')
return false;
return true;
}

public boolean isWon(char token) {
for(int i = 0; i < 3; i++) {
if((cells[i][0].getToken() == token) && (cells[i][1].getToken() == token) && (cells[i][2].getToken() == token))
return true;
}

for(int i = 0; i < 3; i++) {
if((cells[0][i].getToken() == token) && (cells[1][i].getToken() == token) && (cells[2][i].getToken() == token))
return true;
}

for( int i = 0; i < 3; i++) {
if((cells[i][1].getToken() == token) && (cells[i][2].getToken() ==token) && (cells[i][3].getToken()== token))
return true;
}

if((cells[0][0].getToken() == token) && (cells[1][1].getToken() == token) && (cells[2][2].getToken() == token))
return true;

if((cells[0][2].getToken() == token) && (cells[1][1].getToken() == token) && (cells[2][0].getToken() == token))
return true;
if((cells[0][1].getToken() == token) && (cells[1][2].getToken() == token) && (cells[2][3].getToken() == token))
return true;
if((cells[1][0].getToken() == token) && (cells[2][1].getToken() == token) && (cells[3][2].getToken() == token))
return true;


return false;
}

public class Cell extends JPanel {
private char token = ' ';

public Cell() {
setBorder(new LineBorder(Color.black, 1));
addMouseListener(new MyMouseListener());
}

public char getToken() {
return token;
}

public void setToken(char c) {
token = c;
repaint();
}

protected void paintComponent(Graphics g) {
super.paintComponent(g);

if(token == 'X') {
g.drawLine(10,10,getWidth() - 10, getHeight() - 10);
g.drawLine(getWidth() - 10, 10, 10, getHeight() - 10);
}
else if(token == 'O') {
g.drawOval(10, 10, getWidth() - 20, getHeight() - 20);
}
}

private class MyMouseListener extends MouseAdapter {
public void mouseClicked(MouseEvent e) {
if(token == ' ' && whoseTurn != ' ') {
setToken(whoseTurn);

if(isWon(whoseTurn)) {
jlblStatus.setText(whoseTurn + " won! The game is over.");
whoseTurn = ' ';
}
else if(isFull()) {
jlblStatus.setText("Draw! The game is over.");
whoseTurn = ' ';
}
else {
if (whoseTurn == 'X')
whoseTurn = 'O';
else if (whoseTurn == 'O')
whoseTurn = 'X';
jlblStatus.setText(whoseTurn + "'s turn.");
}
}
}
}
}
}

Explanation / Answer

C:my.html