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\

ID: 3814178 • 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…)

****I want help for fixing this code, not a new one.

------------------------------------------------------------------------------------

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

TicTacToe.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package mymain;

/**
*
* @author user
*/


import java.applet.Applet;
import java.awt.*;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowListener;
import java.io.PrintStream;
import javax.swing.JButton;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import java.awt.event.WindowEvent;
import javax.swing.JDialog;

public class TicTacToe extends Applet
implements MouseListener, ActionListener, WindowListener
{
    JFrame f;
    int flg = 2;
    int n;
    int m;
    int i = 0;
    static int bg = 0;
    char[] ch = new char[9];
    JButton fst;
    JButton scnd;
    String s1 = "";
  
    public TicTacToe()
    {
        this.f = new JFrame("Tic Tac Toe");
        this.fst = new JButton("CLEAR");
        this.scnd = new JButton("EXIT");
        this.f.add(this.fst);
        this.f.add(this.scnd);
        this.f.addWindowListener(this);
        this.f.getContentPane().setBackground(Color.ORANGE);
        this.f.setLayout(null);
        this.f.setVisible(true);
        this.f.setSize(800, 600);
        this.fst.setBounds(650, 50, 90, 60);
        this.scnd.setBounds(650, 250, 90, 60);
      
        this.f.addMouseListener(this);
        for (this.i = 0; this.i < 9; this.i += 1)
        this.ch[this.i] = 'B';
        this.fst.addActionListener(this);
        this.scnd.addActionListener(this);
      
        String msg = "Please click on the frame   !!!!!     to start the game ";
      
        JOptionPane pane = new JOptionPane(msg);
        JDialog dialog = pane.createDialog(new JFrame(), "Dilaog");
        dialog.show();
        Graphics g = this.f.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);
        g.drawLine(600, 0, 600, 600);
    }
  
    public void keyPressed(KeyEvent k)
    {
        System.out.print("");
    }
  
    public void keyTyped(KeyEvent k) {
        this.s1 += k.getKeyChar();
    }
  
    public void keyReleased(KeyEvent k) {
        System.out.print("");
    }
  
    public void actionPerformed(ActionEvent ae)
    {
        if (ae.getSource() == this.fst)
        {
            this.f.setVisible(false);
            bg = 0;
            new TicTacToe();
        }
        if (ae.getSource() == this.scnd)
        {
            System.exit(0);
        }
    }
  
    public void windowClosing(WindowEvent we)
    {
    System.exit(0); }
  
     public void windowOpened(WindowEvent de) { }
  
    public void windowClosed(WindowEvent de) { }
  
    public void windowActivated(WindowEvent de) { }
  
    public void windowDeactivated(WindowEvent de) { }
  
    public void windowIconified(WindowEvent de) { }
  
    public void windowDeiconified(WindowEvent de) { }
   
    public void mouseClicked(MouseEvent e) { Graphics2D g2;
        Graphics g = this.f.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);
        g.drawLine(600, 0, 600, 600);
        this.flg -= 1;
        int x = e.getX();
        int y = e.getY();
        if (this.flg == 1)
        {
            if ((x < 200) && (y < 200)) { this.m = 0; this.n = 0; this.ch[0] = 'R'; }
            if ((x > 200) && (x < 400) && (y < 200)) { this.m = 200; this.n = 0; this.ch[1] = 'R'; }
            if ((x > 400) && (x < 600) && (y < 200)) { this.m = 400; this.n = 0; this.ch[2] = 'R'; }
            if ((x < 200) && (y > 200) && (y < 400)) { this.m = 0; this.n = 200; this.ch[3] = 'R'; }
            if ((x > 200) && (x < 400) && (y > 200) && (y < 400)) { this.m = 200; this.n = 200; this.ch[4] = 'R'; }
            if ((x > 400) && (x < 600) && (y > 200) && (y < 400)) { this.m = 400; this.n = 200; this.ch[5] = 'R'; }
            if ((x < 200) && (y > 400) && (y < 600)) { this.m = 0; this.n = 400; this.ch[6] = 'R'; }
            if ((x > 200) && (x < 400) && (y > 400) && (y < 600)) { this.m = 200; this.n = 400; this.ch[7] = 'R'; }
            if ((x > 400) && (x < 600) && (y > 400) && (y < 600)) { this.m = 400; this.n = 400; this.ch[8] = 'R'; }
            g.setColor(new Color(77, 176, 230));
            g2 = (Graphics2D)g;
            g2.setStroke(new BasicStroke(10.0F));
            g.drawOval(this.m + 10, this.n + 10, 159, 159);
        }
      
        if (this.flg == 0)
        {
            if ((x < 200) && (y < 200)) { this.m = 0; this.n = 20; this.ch[0] = 'P'; }
            if ((x > 200) && (x < 400) && (y < 200)) { this.m = 200; this.n = 20; this.ch[1] = 'P'; }
            if ((x > 400) && (x < 600) && (y < 200)) { this.m = 400; this.n = 20; this.ch[2] = 'P'; }
            if ((x < 200) && (y > 200) && (y < 400)) { this.m = 0; this.n = 200; this.ch[3] = 'P'; }
            if ((x > 200) && (x < 400) && (y > 200) && (y < 400)) { this.m = 200; this.n = 200; this.ch[4] = 'P'; }
            if ((x > 400) && (x < 600) && (y > 200) && (y < 400)) { this.m = 400; this.n = 200; this.ch[5] = 'P'; }
            if ((x < 200) && (y > 400) && (y < 600)) { this.m = 0; this.n = 400; this.ch[6] = 'P'; }
            if ((x > 200) && (x < 400) && (y > 400) && (y < 600)) { this.m = 200; this.n = 400; this.ch[7] = 'P'; }
            if ((x > 400) && (x < 600) && (y > 400) && (y < 600)) { this.m = 400; this.n = 400; this.ch[8] = 'P'; }
            g2 = (Graphics2D)g;
            g2.setStroke(new BasicStroke(10.0F));
            g.setColor(new Color(77, 176, 230));
            g.drawLine(this.m + 10, this.n + 13, this.m + 169, this.n + 164);
            g.drawLine(this.m + 169, this.n + 10, this.m + 10, this.n + 169);
            this.flg += 2;
        }
      
        for (this.i = 0; this.i < 3; this.i += 1)
        {
            if ((this.ch[this.i] != 'B') &&
            (this.ch[(this.i + 3)] == this.ch[this.i]) && (this.ch[(this.i + 6)] == this.ch[this.i]))
            {
                new PlayBoard().win();
                bg = 1;
            }
        }
      
        for (this.i = 0; this.i < 7; this.i += 1)
        {
            if (this.ch[this.i] != 'B')
            {
                if ((this.ch[this.i] == this.ch[(this.i + 1)]) && (this.ch[this.i] == this.ch[(this.i + 2)]))
                {
                    new PlayBoard().win();
                    bg = 1;
                }
                this.i += 2;
            }
            else {
                this.i += 2;
            }
        }
        if ((this.ch[4] != 'B') && ((
        ((this.ch[0] == this.ch[4]) && (this.ch[4] == this.ch[8])) || ((this.ch[2] == this.ch[4]) && (this.ch[4] == this.ch[6])))))
        {
            new PlayBoard().win();
            bg = 1;
        }
      
        for (this.i = 0; (this.i < 9) &&
        (this.ch[this.i] != 'B'); this.i += 1)
        {
            if (this.i == 8)
            {
                if (bg == 0)
                new PlayBoard().draw();
                bg = 0;
            }
        }
    }
  
    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();
    }
}

PlayBoard.java

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package mymain;

/**
*
* @author user
*/
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

class PlayBoard implements WindowListener
{
    public void win()
    {
        String msg = "Congratulations !     You win ";
      
        JOptionPane pane = new JOptionPane(msg);
        JDialog dialog = pane.createDialog(new JFrame(), "Dilaog");
        dialog.show();
    }
  
    public void draw()
    {
        String msg = " the result is !!     STALEMATE ";
      
        JOptionPane pane = new JOptionPane(msg);
        JDialog dialog = pane.createDialog(new JFrame(), "Dilaog");
        dialog.show();
    }
  
    public void windowClosing(WindowEvent we)
    {
        System.exit(0);
    }
  
    public void windowOpened(WindowEvent we)
    {
    }
  
    public void windowClosed(WindowEvent we)
    {
    }
  
    public void windowActivated(WindowEvent we)
    {
    }
  
    public void windowDeactivated(WindowEvent we)
    {
    }
  
    public void windowIconified(WindowEvent we)
    {
    }
  
    public void windowDeiconified(WindowEvent we)
    {
    }
}