Question

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

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();

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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   !!!!! \n    \nto start the game \n";
      
        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 ! \n    \nYou win \n";
      
        JOptionPane pane = new JOptionPane(msg);
        JDialog dialog = pane.createDialog(new JFrame(), "Dilaog");
        dialog.show();
    }
  
    public void draw()
    {
        String msg = " the result is !! \n    \nSTALEMATE \n";
      
        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)
    {
    }
}

Add a comment
Know the answer?
Add Answer to:
java ****Please fix this tic tac toe coding error *****following is the error I've found difficult...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • JAVA TIC TAC TOE - please help me correct my code Write a program that will...

    JAVA TIC TAC TOE - please help me correct my code Write a program that will allow two players to play a game of TIC TAC TOE. When the program starts, it will display a tic tac toe board as below |    1       |   2        |   3 |    4       |   5        |   6                 |    7      |   8        |   9 The program will assign X to Player 1, and O to Player    The program will ask Player 1, to...

  • "Polygon Drawer Write an applet that lets the user click on six points. After the sixth point is clicked, the applet should draw a polygon with a vertex at each point the user clicked." import...

    "Polygon Drawer Write an applet that lets the user click on six points. After the sixth point is clicked, the applet should draw a polygon with a vertex at each point the user clicked." import java.awt.*; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.applet.*; public class PolygonDrawer extends Applet implements MouseListener{ //Declares variables    private int[] X, Y;    private int ptCT;    private final static Color polygonColor = Color.BLUE; //Color stuff    public void init() {        setBackground(Color.BLACK);        addMouseListener(this);        X = new int [450];        Y =...

  • TIC TAC TOE in openGL/ C++

    I just cannot figure out how to get the game to alternate turns. it is designed to be a 2 player game. the problem is once either an x or an o is placed it will onlycontinue to place the x or the o.please show me how to do the rest here is my code://#include <windows.h> // This header file will be needed for some windows compilers//#include <GL/gl.h> // gl.h and glu.h also maybe needed for some compilers//#include <GL/glu.h>#include <GL/glut.h>...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

  • JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to...

    JAVA problem: Upgrade and extend the previous programs Draw.java and DrawCanvas.java used in the class to maintain a list of shapes drawn as follows: Replace and upgrade all the AWT components used in the programs to the corresponding Swing components, including Frame, Button, Label, Choice, and Panel. Add a JList to the left of the canvas to record and display the list of shapes that have been drawn on the canvas. Each entry in the list should contain the name...

  • This is an advanced version of the game tic tac toe, where the player has to...

    This is an advanced version of the game tic tac toe, where the player has to get five in a row to win. The board is a 30 x 20. Add three more functions that will check for a win vertically, diagonally, and backwards diagonally. Add an AI that will play against the player, with random moves. Bound check each move to make sure it is in the array. Use this starter code to complete the assignment in C++. Write...

  • This is a Clock program in Java. Can you fix my Java program error, and add function to display c...

    This is a Clock program in Java. Can you fix my Java program error, and add function to display current time currently? import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.util.Date; import javax.swing.JPanel; import javax.swing.WindowConstants; public class Clock extends javax.swing.JFrame {    public int hour;    public int min;    public int sec;    ClockDial cd;    public Clock() {        setSize(510, 530);        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);        cd = new ClockDial(this);        getContentPane().add(cd);        Date curr = new Date();        String time = curr.toString();        hour = Integer.parseInt(time.substring(11, 13));        min = Integer.parseInt(time.substring(14, 16));        sec...

  • please evaluate the following code. this is JAVA a. class Car { public int i =...

    please evaluate the following code. this is JAVA a. class Car { public int i = 3; public Car(int i) { this.i = i; } } ... Car x = new Car(7), y = new Car(5); x = y; y.i = 9; System.out.println(x.i); b. class Driver { public static void main(String[] args) { int[] x = {5, 2, 3, 6, 5}; int n = x.length; for (int j = n-2; j > 0; j--) x[j] = x[j-1]; for (int j...

  • In a game of Tic Tac Toe, two players take turns making an available cell in...

    In a game of Tic Tac Toe, two players take turns making an available cell in a 3 x 3 grid with their respective tokens (either X or O). When one player has placed three tokens in a horizontal, vertical, or diagonal row on the grid, the game is over and that player has won. A stalemate occurs when all the cells on the grid have been filled with tokens and neither player has achieved a win. Write a program...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT