简体   繁体   English

将int数组作为参数传递给方法[Java]

[英]Passing int arrays as arguments in methods [Java]

I am building a Connect 4 game and I was wondering if it is possible to build int arrays into methods. 我正在构建一个Connect 4游戏,我想知道是否有可能将int数组构建到方法中。 It would be easier for me to just show the code, so I've shown the method below (and dependencies). 仅显示代码对我来说会更容易,所以我在下面显示了方法(和依赖项)。 The commented out bit is how it works at the moment 评论出来的地方是目前的运作方式

    //Defend 4 horizontal to the right (xxx-)
    for(int y=0;y<board.get_ysize();y++){
        boolean breakfor=false;
        for(int x=0;x<(board.get_xsize()-3);x++){
            if(board.get_state(x,y)==1
                    && board.areSame(board, {(x+1),y}, {x,y})
                    && board.areSame(board, {(x+2),y}, {x,y})
                    && board.areSame(board, {(x+3),y}, {0})

// How the code works at the moment:                        
//                      board.get_state((x+1),y)==board.get_state(x,y)
//                      && board.get_state((x+2),y)==board.get_state(x,y)
//                      && board.get_state((x+3),y)==0)
                    {
                if(y==0){
                    return (x+3);
                }
                else if((y-1)==0){
                    return (x+3);
                }
                else{
                    breakfor=true;
                    break;
                }       
            }
        }
        if(breakfor){
            break;
        }
    }

get_state(): get_state():

public int get_state(int x, int y) {
        return layout[x][y];
    }

(layout[x][y] returns a 0, 1 or 2 for the position. 0 is empty and 1 and 2 are player counters) (layout [x] [y]返回该位置的0、1或2。0为空,而1和2为玩家计数器)

areSame(): areSame():

public boolean areSame(GameBoard board, int[] initialspace, int[] comparespace){
    if(board.get_state(initialspace[0],initialspace[1])==board.get_state(comparespace[0], comparespace[1])){
        return true;
    }
    return false;
}

It's not necessary as the code works the old way, but I was just wondering if there is a way to make this array method work? 由于代码按旧方法工作是不必要的,但是我只是想知道是否有一种方法可以使此数组方法工作?

Thanks 谢谢

You may want to check for a defense against a vertical and horizontal threat to win on next move by using a check like (for horizontal shown below, make an analog for vertical threat) 您可能希望通过使用类似的检查来检查针对垂直和水平威胁的防御措施,以便在下一步行动中获胜(如下所示的水平,对垂直威胁进行模拟)

boolean threat;
int playX = -1;
int playY;

for (int y=0; y < board.get_ysize; y++) {
    for (int x=0, x < board.get_xsize; x++) {
        int count1;
        int count2;
        for (int i=0; i < 4; i++) {
            switch (board.get_state(x+i, y)) {
            case 0:
                if (playX == -1) {playX = x+i; playY = y;}
                break;
            case 1:
                count1++;
                break;
            case 2:
                count2++;
                break;
            default:
              // handle error
              break;
           }
        }
        if (count1 == 3 && count2 == 0) {
          // should be able to defend
          // play player 2 at playX, playY
        break; // could go on to see if imminent loss anyway
    }
}

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

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