Tic Tac Toe mouseListener

时间:2019-01-28 17:32:55

标签: java

我试图在此代码中实现井字游戏逻辑。我什么都不要。它只需要成为玩家对玩家,就必须展示获胜者并询问您是否要再次比赛。我已经尝试使这项工作一段时间了,但没有成功。

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

说谁赢了,问您是否想再玩一次。

1 个答案:

答案 0 :(得分:0)

显然,您可以编写游戏逻辑,但是您可以通过几个循环轻松绘制棋盘。

我会尝试获取您单击的单元格索引并将播放器数据存储到矩阵中。这样,您可以在需要绘制时简单地调用值。这也增加了关于谁拥有哪个单元的验证层。

char[][] cells = new char[3][3];

@Override
void mousePressed(MouseEvent e) {
    int tileWidth = (int) Math.floor(BOARD_WIDTH / cells.length);
    int tileHeight = (int) Math.floor(BOARD_HEIGHT / cells[0].length);
    int col = (int) Math.floor(e.getX() / tileWidth);
    int row = (int) Math.floor(e.getY() / tileHeight);

    setPlayer(row, col);
}

void setPlayer(int row, int col) {
    if (col < cells.length && row < cells[0].length) {
        if (cells[row][col] == 0) {
            cells[row][col] = player;
            player = player == 'X' ? 'O' : 'X';
            repaint();
        }
    }
}

此外,您应该使用paintComponent(g)而不是paint(g)

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

public class TicTacToe implements Runnable {
    public static final String TITLE = "TicTacToe";

    @Override
    public void run() {
        JFrame frame = new JFrame(TITLE);
        Board board = new Board();

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(board);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new TicTacToe());
    }

    private class Board extends JComponent implements MouseListener {
        private static final long serialVersionUID = 5427550843122167512L;
        public static final int BOARD_WIDTH = 256;
        public static final int BOARD_HEIGHT = 256;
        private char[][] cells = new char[3][3];
        private char player = 'X';

        private int fontHeight = 110;
        private Font font = new Font("monospaced", Font.PLAIN, fontHeight);

        public Board() {
            this.setPreferredSize(new Dimension(BOARD_WIDTH, BOARD_HEIGHT));
            this.addMouseListener(this);
        }

        @Override
        protected void paintComponent(Graphics g) {
            g.setFont(font);
            drawBoard(g);
            drawValues(g);
        }

        private void drawBoard(Graphics g) {
            int tileWidth = BOARD_WIDTH / cells.length;
            int tileHeight = BOARD_HEIGHT / cells[0].length;
            g.drawRect(0, 0, BOARD_WIDTH, BOARD_HEIGHT);
            for (int row = 0; row < cells.length; row++) {
                g.drawLine(0, row * tileHeight, BOARD_WIDTH, row * tileHeight);
            }
            for (int col = 0; col < cells[0].length; col++) {
                g.drawLine(col * tileWidth, 0, col * tileWidth, BOARD_HEIGHT);
            }
        }

        private void drawValues(Graphics g) {
            int tileWidth = BOARD_WIDTH / cells.length;
            int tileHeight = BOARD_HEIGHT / cells[0].length;


            for (int row = 0; row < cells.length; row++) {
                for (int col = 0; col < cells[row].length; col++) {
                    String text = cells[row][col] + "";
                    int charWidth = g.getFontMetrics().stringWidth(text);
                    int xOffset = (int) Math.floor((tileWidth - charWidth) / 2);
                    int yOffset = (int) Math.floor((tileHeight - fontHeight) * 1.5);
                    int x = col * tileWidth + xOffset;
                    int y = row * tileHeight + fontHeight + yOffset;
                    System.out.println(yOffset);
                    g.drawString(text, x, y);
                }
            }
        }

        private void setPlayer(int row, int col) {
            if (col < cells.length && row < cells[0].length) {
                if (cells[row][col] == 0) {
                    cells[row][col] = player;
                    player = player == 'X' ? 'O' : 'X';
                    repaint();
                }
            }
        }

        @Override
        public void mousePressed(MouseEvent e) {
            int tileWidth = (int) Math.floor(BOARD_WIDTH / cells.length);
            int tileHeight = (int) Math.floor(BOARD_HEIGHT / cells[0].length);
            int col = (int) Math.floor(e.getX() / tileWidth);
            int row = (int) Math.floor(e.getY() / tileHeight);

            setPlayer(row, col);
        }

        @Override
        public void mouseClicked(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseEntered(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent e) {
            // TODO Auto-generated method stub

        }
    }
}