简体   繁体   English

如何获得按钮的数组#/使此运行更有效

[英]How can I get the Array # of a button / Make this run more efficiently

I am making a complex tic tac toe program that has a variable grid size and player amount. 我正在制作一个复杂的tic tac toe程序,它具有可变的网格大小和玩家数量。 But one of my friends commented that it is a little slow respond after they make a move in a 64 by 64 space. 但是我的一位朋友评论说,他们在64乘64的空间中移动后反应有点慢。 I looked into it and I have found the issue, my program checks every button in the grid to see if it was clicked, then it checks every player to see who made the move. 我查看了它,我发现了问题,我的程序检查网格中的每个按钮,看它是否被点击,然后它检查每个玩家,看看谁做了移动。 Once it has found both out, it moves on with life :D. 一旦它找到了两个,它继续生活:D。 But that can take some time when working with a larger grid. 但是,使用更大的网格可能需要一些时间。 so I made an attempt at fixing it by putting in some "breaks;", but they didn't help to find it faster, only to stop looking faster. 所以我试图通过加入一些“休息”来修复它,但是他们没有帮助更快地找到它,只是为了停止看起来更快。

public void actionPerformed(ActionEvent gridButtonClicked) {
        for (int i = 0; i < gridButton.length; i++) {
            if (gridButtonClicked.getSource() == gridButton[i]) {
                for(int a = 0;a < amountOfPlayers;a++){
                    if(turn == a) {
                        gridButtonOwner[i] = a + 1;
                        gridButton[i].setBackground(playerColors[a]);
                        gridButton[i].setEnabled(false);
                        System.out.println("Grid " + i + ": " + gridButtonOwner[i]);
                        break;
                    }
                }
                break;
            }
        }
    }

What I want to know is that if I can get the array number of the button clicked. 我想知道的是,如果我可以点击按钮的数组编号。 Like if gridButtonClicked = gridButton[1], it would return the number 1, or if it equaled gridButton[2] it would return 2, etc. 就像gridButtonClicked = gridButton [1]一样,它将返回数字1,或者如果它等于gridButton [2],它将返回2,等等。

  • gridButtonOwner is a int array, gridButtonOwner是一个int数组,
  • gridButton is a jbutton array. gridButton是一个jbutton数组。

I must say i don't understand the need for the loops. 我必须说我不理解循环的必要性。 In the first for-loop you're getting the grid-button - but you know what this is because it's the event source... You can just store a map of GridButton onto Integer to get the place in your array. 在第一个for循环中,你得到了网格按钮 - 但是你知道这是什么,因为它是事件源...你可以将GridButton的地图存储到Integer中以获得数组中的位置。 Why loop to find it? 为什么循环找到它? In the second loop you're looping until a == turn ... Which means you already know what a is because it == turn . 在第二循环中,您会循环,直到a == turn ......这意味着你已经知道什么a是因为它== turn You should be able to remove the loops completely: 您应该能够完全删除循环:

// earlier on: myMap = new HashMap<GridButton, Integer>();
public void actionPerformed(ActionEvent gridButtonClicked) {
    GridButton gridButton = gridButtonClicked.getSource();
    int i = myMap.get(gridButton);
    gridButtonOwner[i] = turn + 1;
    gridButton.setBackground(playerColors[turn]);
    gridButton.setEnabled(false);
    System.out.println("Grid " + i + ": " + gridButtonOwner[i]);
}

You can find the index 'i' in a single lookup (without looping) if you use a HashMap. 如果使用HashMap,则可以在单个查找中找到索引“i”(无需循环)。 You'll have to use the proper type (eg JButton, I didn't know which you're using in place of GridButton). 你必须使用正确的类型(例如JButton,我不知道你使用哪个代替GridButton)。

HashMap<GridButton, Integer> buttonIndices = new HashMap<GridButton, Integer>();

'Integer' is the object version if 'int' as collections can't store primitive types. 'Integer'是对象版本,如果'int',因为集合不能存储基本类型。

Then populate the map with each GridButton and index value (fill in the ...): 然后使用每个GridButton和索引值填充地图(填写...):

for (i...) {
  buttonIndices.put(gridButton[i], i); // the 'i' is auto-converted to Integer
}

To lookup the 'i' for a gridButton: 要为gridButton查找'i':

Integer index = buttonIndices(gridButton);
if (index == null) {
  // error, not found
  error handling stuff...
}

i = index; // converts Integer to int type of 'i'

This should get you moving in the right direction. 这应该让你朝着正确的方向前进。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM