1. Engineering
  2. Computer Science
  3. i need to implement game logic for tic tac toe...

Question: i need to implement game logic for tic tac toe...

Question details
I need to implement game logic for tic tac toe in this code. Can someone help me. It just needs to be a a player vs player again and ask if you want to play again. 





import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class TicTacToe {
    public static void main(String[] args)
    {
        JFrame frame = new JFrame(" TicTacToe");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(290, 300);
        Board board = new Board();
        frame.add(board);
        frame.setResizable(false);
        frame.setVisible(true);

    }
}

class Board extends JComponent {
    private int w = 265, h = 265;
    char player = 'X';

    public Board(){
        addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent me) {
                int x = me.getX();
                int y = me.getY();

                int sideWidth = w / 3;
                int sideHeight = h / 3;

                int a = x / sideWidth;
                int b = y / sideHeight;

                int xStart = sideWidth * a + 10;
                int yStart = sideHeight * b + + sideHeight - 10;

                Graphics g = getGraphics();
                g.setFont(new Font("monospaced", Font.PLAIN, 110));

                g.drawString(player + "", xStart, yStart);
                if (player == 'X') {
                    player = 'O';
                } else {
                    player = 'X';
                }

                if 
            }

        });

    }


    public void paint(Graphics g) {
        g.drawLine(90, 0, 90, 300);
        g.drawLine(185, 0, 185, 300);
        g.drawLine(0, 85, 300, 85);
        g.drawLine(0, 175, 300, 175);

    }
}



Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution