簡體   English   中英

井字游戲鼠標監聽器

[英]Tic Tac Toe mouseListener

我試圖在此代碼中實現井字游戲邏輯。 我不想要任何超級花哨的東西。 它只需要成為玩家對玩家,並且必須顯示獲勝者並詢問您是否想再次玩。 我一直在努力使這項工作有一段時間了,但沒有成功。

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

說誰贏了,問你是否想再玩一次。

顯然,游戲邏輯是由您編寫的,但您可以使用幾個循環輕松繪制棋盤。

我會嘗試獲取您單擊的單元格索引並將玩家數據存儲到矩陣中。 通過這種方式,您可以在需要繪畫時簡單地調用這些值。 這也增加了關於誰擁有哪個單元格的驗證層。

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

        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM