简体   繁体   English

我如何用 int 数组编写我的获胜方法

[英]How can i write my winning method with an int array

public class Logik extends JFrame implements ActionListener {

    private static final int[] WINCASE = { 0, 1, 2 };

    JButton[] buttons = new JButton[9];

    public int counter = 0;

    public Logik() {

        JFrame f = new JFrame("TicTacToe");
        f.setVisible(true);
        f.setSize(400, 420);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false);
        JPanel p = new JPanel();
        f.add(p);

        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new JButton(String.valueOf(i));
            buttons[i].setPreferredSize(new Dimension(120, 120));
            buttons[i].addActionListener(this);
            buttons[i].setText("");
            p.add(buttons[i]);
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {

        if (e.getSource() instanceof JButton) {
            JButton b = (JButton) e.getSource();
            if (counter % 2 == 0) {
                b.setText("X");
                b.setEnabled(false);
                counter++;
            } else {
                b.setText("O");
                counter++;
                b.setEnabled(false);
            }

        }
    }
}

If you can see I made an int Array and I have 9 buttons and I want to know who won by writing all winning cases in that array like: {4,5,6}{7,8,9}.如果你能看到我制作了一个 int Array ,我有 9 个按钮,我想通过在该数组中写入所有获胜案例来知道谁获胜,例如: {4,5,6}{7,8,9}. but I don't know how to use my array for Jbutton to make it work, can someone give me tips?但我不知道如何将我的数组用于 Jbutton 使其工作,有人可以给我提示吗?

You can do that in many ways of course, for eg you can check the entire "board" in actionPerformed to see if you have a winner.当然,您可以通过多种方式做到这一点,例如,您可以在 actionPerformed 中检查整个“板”,看看您是否有赢家。

But I think you program could benefit from an abstraction that reminds of the game you are building;但是我认为你的程序可以从一个抽象的东西中受益,它提醒你正在构建的游戏; place the buttons in an matrix (instead of the array) and why not create your custom JButton by extending the java class - in that class you can add the coordination the button have in the matrix.将按钮放在矩阵(而不是数组)中,为什么不通过扩展 java 类来创建自定义 JButton - 在该类中,您可以添加按钮在矩阵中的协调。 Then it will be much easier for you to check for a winner.这样您就可以更轻松地检查获胜者。

With a matrix I mean two dimensional array like JButton[][] buttons = new JButton[3][3] Here is one example on how you can extends the JButton class How to create a custom JButton class in Java?对于矩阵,我的意思是二维数组,如JButton[][] buttons = new JButton[3][3]下面是一个关于如何扩展JButton类的示例 如何在 Java 中创建自定义 JButton 类? Of course you need to add logic related to your program in the CustomButton class, and maybe give a name meaningful in that context.当然,您需要在 CustomButton 类中添加与您的程序相关的逻辑,并可能给出一个在该上下文中有意义的名称。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当按下一个按钮时,如何编写一种从按钮数组返回int的方法? - How can I write a method that return an int from an array of buttons when one is pressed? 如何使用数组列表中的索引并转换为 int? - How can I use the index in my array list and convert to int? 怎么写呢? 我的nextWord方法需要特定的帮助 - How can write this? I Need help specifially with my nextWord method 为什么我不能从另一个方法访问我在 main 方法中创建的 int 数组值? (初学者Java编码器) - Why can't I access my int array values that I made in my main method from another method? (beginner Java coder) 如何在 JAVA 中输出 6 个随机乐透中的 2 个中奖号码? - How can I output 2 winning numbers in a 6 random lotto in JAVA? 如何将这个.txt文件写入2D int数组,而没有收到NumberFormatException? - How can I write this .txt file into a 2D int array and not receive a NumberFormatException? write(byte [],int,int)方法如何工作? - How is the write(byte[],int,int) method working? 我想修复我的方法来反转int数组 - i want fix my method to reverse int array 你好,我怎样才能将 hexString 转换为 int。 我有“F3A4”,我想编写一个方法或使用 Java_API 来获取 int。 请问我如何编码? - Hallo , how can I convert a hexString to int . I have "F3A4" and i want to write a method or use Java_API to get the int. How code I do it please? 如何在每种方法中使用对象数组? - How can I use my object array at every method?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM