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

java Please fix this tic tac toe coding error following is the error I\'ve found

ID: 3814120 • Letter: J

Question

java

Please fix this tic tac toe coding error

following is the error I've found difficult for me to fix.

1. Before I click the board, the grid doesn’t show up at all

2. It is not an automatic system.(if I click, it doesn’t move to next move)

3. if you click a button twice it shows o, x both

4. it is hard to close (you have to close the instruction)

5. instruction keep pops up (start->if you lose you lose button etc…)

import java.awt.*;

import java.awt.event.*;

import java.applet.Applet;

public class TicTacToe extends Applet implements MouseListener

{

Frame board;

int whoseTurn = 0;

int n, m, i=0;

char ch[] = new char[9];

public TicTacToe()

{

board = new Frame("Tic Tac Toe");

board.setLayout(null);

board.setVisible(true);

board.setSize(600, 600);

board.addMouseListener(this);

for(i=0;i<9;i++)

ch[i]='B';

}

  

  

  

public void mouseClicked(MouseEvent e)

{

Graphics g = board.getGraphics();

g.drawLine(200,0,200,600);

g.drawLine(400,0,400,600);

g.drawLine(0,200,600,200);

g.drawLine(0,400,600,400);

  

whoseTurn++;

  

int x= e.getX();

int y= e.getY();

  

if(whoseTurn==2)

{

if(x<200&&y<200){m=0;n=0;ch[0]='X';}

if((x>200&&x<400)&&(y<200)){m=200;n=0;ch[1]='X';}

if((x>400&&x<600)&&(y<200)){m=400;n=0;ch[2]='X';}

if(x<200&&(y>200&&y<400)){m=0;n=200;ch[3]='X';}

if((x>200&&x<400)&&(y>200&&y<400)){m=200;n=200;ch[4]='X';}

if((x>400&&x<600)&&(y>200&&y<400)){m=400;n=200;ch[5]='X';}

if(x<200&&(y>400&&y<600)){m=0;n=400;ch[6]='X';}

if((x>200&&x<400)&&(y>400&&y<600)){m=200;n=400;ch[7]='X';}

if((x>400&&x<600)&&(y>400&&y<600)){m=400;n=400;ch[8]='X';}

g.setColor(Color.red);

g.drawLine(m,n,m+199,n+199);

g.drawLine(m+199,n,m,n+199);

}

if(whoseTurn==3)

{

if(x<200&&y<200){m=0;n=20;ch[0]='O';}

if((x>200&&x<400)&&(y<200)){m=200;n=20;ch[1]='O';}

if((x>400&&x<600)&&(y<200)){m=400;n=20;ch[2]='O';}

if(x<200&&(y>200&&y<400)){m=0;n=200;ch[3]='O';}

if((x>200&&x<400)&&(y>200&&y<400)){m=200;n=200;ch[4]='O';}

if((x>400&&x<600)&&(y>200&&y<400)){m=400;n=200;ch[5]='O';}

if(x<200&&(y>400&&y<600)){m=0;n=400;ch[6]='O';}

if((x>200&&x<400)&&(y>400&&y<600)){m=200;n=400;ch[7]='O';}

if((x>400&&x<600)&&(y>400&&y<600)){m=400;n=400;ch[8]='O';}

g.setColor(Color.green);

g.drawOval(m+10,n+10,169,169);

// g.drawLine(m,n,m+189,n+189);

// g.drawLine(m+199,n,m,n+199);

whoseTurn=whoseTurn-2;

}

for(i=0;i<9;i++) // for draw

{

if(ch[i]!='B')

{

if(i==8)

draw();

}

else

break;

}

for(i=0;i<3;i++) //for vertical

{

// System.out.print(ch[i]);

if(ch[i]!='B')

{

if((ch[i+3]==ch[i])&&(ch[i+6]==ch[i]))

win();

}

}

for(i=0;i<7;i++) //for horizontal

{

if(ch[i]!='B')

{

if((ch[i]==ch[i+1])&&(ch[i]==ch[i+2]))

win();

i=i+2;

}

else

i=i+2;

}

if(ch[4]!='B') //for diagonals

{

if(((ch[0]==ch[4])&&(ch[4]==ch[8]))||((ch[2]==ch[4])&&(ch[4]==ch[6])))

win();

}

}

public Frame win()

{

Frame m=new Frame("Result");

Label l=new Label("Game Over. You Win");

m.setLayout(null);

m.add(l);

l.setBounds(20,20,300,60);

m.setVisible(true);

m.setSize(150,100);

return m;

}

public Frame draw()

{

Frame m=new Frame("Result");

Label l1=new Label("Tie");

m.setLayout(null);

m.add(l1);

l1.setBounds(20,20,60,60);

m.setVisible(true);

m.setSize(100,100);

return m;

}

public void mouseReleased(MouseEvent e)

{

System.out.print("");

}

public void mouseEntered(MouseEvent e)

{

System.out.print("");

}

public void mouseExited(MouseEvent e)

{

System.out.print("");

}

public void mousePressed(MouseEvent e)

{

System.out.print("");

}

public static void main(String args [])

{

new TicTacToe();

}

}

Explanation / Answer

import java.awt.*;   import java.awt.event.*;   import javax.swing.*;   class TTT1 extends JFrame implements ItemListener, ActionListener{   int i,j,ii,jj,x,y,yesnull;    int a[][]={{10,1,2,3,11},{10,1,4,7,11},{10,1,5,9,11},{10,2,5,8,11},                   {10,3,5,7,11},{10,3,6,9,11},{10,4,5,6,11},           {10,7,8,9,11} };   int a1[][]={{10,1,2,3,11},{10,1,4,7,11},{10,1,5,9,11},{10,2,5,8,11},                   {10,3,5,7,11},{10,3,6,9,11},{10,4,5,6,11},{10,7,8,9,11} };                      boolean state,type,set;      Icon ic1,ic2,icon,ic11,ic22;   Checkbox c1,c2;   JLabel l1,l2;   JButton b[]=new JButton[9];   JButton reset;      public void showButton(){      x=10; y=10;j=0;   for(i=0;i