Java 示例代码未按预期执行

Java 示例代码未按预期执行,java,Java,这是算法(不工作),请告诉我错误在哪里 谢谢 private void checkSouth(Location point, int player) { //Loop through everything south boolean isthereAnOppositePlayer=false; int oppositePlayer=0; //Set opposite player if (player==1) {

这是算法(不工作),请告诉我错误在哪里

谢谢

private void checkSouth(Location point, int player) {
        //Loop through everything south
        boolean isthereAnOppositePlayer=false;

        int oppositePlayer=0;
        //Set opposite player
        if (player==1) {
            oppositePlayer=2;
        }else{
            oppositePlayer=1;
        }

        for (int i = point.getVertical(); i < 8; i++) {

            //Create a location point with the current location being compared
            MyLocation locationBeingChecked= new MyLocation();
            locationBeingChecked.setHorizontal(point.getHorizontal());
            locationBeingChecked.setVertical(i);

            int value = board[locationBeingChecked.getVertical()][locationBeingChecked.getHorizontal()];

            //If the first checked is the opposite player
            if (value==oppositePlayer) {
                //Then potential to evaluate more
                isthereAnOppositePlayer=true;
            }
            //If it isn't an opposite player, then break
            if(!isthereAnOppositePlayer && value!=0){
                break;
            }

            //If another of the player's piece found or 0, then end
            if (isthereAnOppositePlayer && value==player || isthereAnOppositePlayer && value==0) {
                break;
                //end   
            }

            //Add to number of players to flip
            if(isthereAnOppositePlayer && value==oppositePlayer && value!=0){
                //add to array
                addToPiecesToTurn(locationBeingChecked);
            }
        }
    }
private void checkSouth(位置点,int播放器){
//环游南方
布尔值isthereAnOppositePlayer=false;
int-oppositePlayer=0;
//设置对方球员
如果(玩家==1){
对方玩家=2;
}否则{
对方玩家=1;
}
对于(int i=point.getVertical();i<8;i++){
//创建一个与正在比较的当前位置相比较的位置点
MyLocation Location BeingChecked=新的MyLocation();
locationBeingChecked.setHorizontal(point.getHorizontal());
检查位置。设置垂直(i);
int value=board[locationBeingChecked.getVertical()][locationBeingChecked.getHorizontal()];
//如果第一个选中的是对方玩家
如果(值==对手玩家){
//然后是评估更多的潜力
是否有一个对手玩家=正确;
}
//如果不是对方球员,则破发
如果(!isthereAnOppositePlayer&&value!=0){
打破
}
//如果另一个玩家的棋子找到或为0,则结束
如果(isthereAnOppositePlayer&&value==player | | isthereAnOppositePlayer&&value==0){
打破
//结束
}
//添加要翻转的玩家数
如果(isthereAnOppositePlayer&&value==oppositePlayer&&value!=0){
//添加到数组
addToPiecesToTurn(正在检查位置);
}
}
}

看起来旋转回到另一位玩家的位置与第一次移动时旋转的位置完全相同。我猜由
addToPiecesToTurn
填充的数组在每次移动之间可能没有被清除,所以之前的所有位置都仍然在那里

如果要在
ArrayList
中存储要翻转的工件,可以使用该方法在每次翻转之间擦除集合的内容


另一个可能的问题是,您正在检查对方玩家,然后立即开始填充
addToPiecesToTurn
。但是,该方向的棋子不一定可以旋转,除非它们被包含当前玩家棋子的第二个位置“夹住”。我认为您的代码没有正确地检查这种情况;当这种情况发生时,您可能希望以某种方式跳过将这些片段翻转给其他玩家的操作,例如清除
片段的数组以翻转


编辑:查看当前的解决方案,您将分别实现每个方向,您将有大量重复的代码。如果您考虑沿某个方向行走意味着什么,可以将其视为按“步长”量调整x/y值。向后的步进量可以是
-1
,不移动的步进量可以是
0
,向前的步进量可以是
1
。然后,您可以创建一个处理所有方向的方法,而无需复制逻辑:

private void checkDirection(Location point, int player, int yStep, int xStep) {
    int x = point.getHorizontal() + xStep;
    int y = point.getVertical() + yStep;

    MyLocation locationBeingChecked = new MyLocation();
    locationBeingChecked.setHorizontal(x);
    locationBeingChecked.setVertical(y);

    while (isValid(locationBeingChecked)) {
        // do the logic here

        x += xStep;
        y += yStep;

        locationBeingChecked = new MyLocation();
        locationBeingChecked.setHorizontal(x);
        locationBeingChecked.setVertical(y);
    }
}
您需要实现
isValid
,以检查位置是否有效,即在电路板中。然后可以为每个方向调用此方法:

// north
checkDirection(curPoint, curPlayer, -1, 0);
// north-east
checkDirection(curPoint, curPlayer, -1, 1);
// east
checkDirection(curPoint, curPlayer, 0, 1);
// etc

看起来旋转回到另一个玩家的位置与第一步中旋转的位置完全相同。我猜由
addToPiecesToTurn
填充的数组在每次移动之间可能没有被清除,所以之前的所有位置都仍然在那里

如果要在
ArrayList
中存储要翻转的工件,可以使用该方法在每次翻转之间擦除集合的内容


另一个可能的问题是,您正在检查对方玩家,然后立即开始填充
addToPiecesToTurn
。但是,该方向的棋子不一定可以旋转,除非它们被包含当前玩家棋子的第二个位置“夹住”。我认为您的代码没有正确地检查这种情况;当这种情况发生时,您可能希望以某种方式跳过将这些片段翻转给其他玩家的操作,例如清除
片段的数组以翻转


编辑:查看当前的解决方案,您将分别实现每个方向,您将有大量重复的代码。如果您考虑沿某个方向行走意味着什么,可以将其视为按“步长”量调整x/y值。向后的步进量可以是
-1
,不移动的步进量可以是
0
,向前的步进量可以是
1
。然后,您可以创建一个处理所有方向的方法,而无需复制逻辑:

private void checkDirection(Location point, int player, int yStep, int xStep) {
    int x = point.getHorizontal() + xStep;
    int y = point.getVertical() + yStep;

    MyLocation locationBeingChecked = new MyLocation();
    locationBeingChecked.setHorizontal(x);
    locationBeingChecked.setVertical(y);

    while (isValid(locationBeingChecked)) {
        // do the logic here

        x += xStep;
        y += yStep;

        locationBeingChecked = new MyLocation();
        locationBeingChecked.setHorizontal(x);
        locationBeingChecked.setVertical(y);
    }
}
您需要实现
isValid
,以检查位置是否有效,即在电路板中。然后可以为每个方向调用此方法:

// north
checkDirection(curPoint, curPlayer, -1, 0);
// north-east
checkDirection(curPoint, curPlayer, -1, 1);
// east
checkDirection(curPoint, curPlayer, 0, 1);
// etc

对于某些单元测试来说,这类问题已经成熟。您可以很容易地设置一个板,进行一次移动,并验证答案,测试结果将让您充分了解您的期望和现实的分歧。

对于某些单元测试来说,这类问题已经成熟。你可以很容易地设置一个棋盘,进行一个移动,并验证答案,测试结果将让你充分了解你的期望和现实的分歧。

为什么不使用2d阵列

每个单元格将包含一个枚举:空、PLAYER_1、PLAYER_2

然后,为了遍历单元格,只需对每个方向使用循环

例如,单击一个单元格时,右边的复选框为:

for(int x=pressedLocation.x+1;x<cells[pressedLocation.y].length;++x)
  {
  Cell cell=cells[pressedLocation.y][x];
  if(cell==EMPTY||cell==currentPlayerCell)
    break;
  cells[pressedLocation.y][x]=currentPlayerCell;
  }
for(int y=pressedLocation.y+1;y<cells.length;++y)
  {
  Cell cell=cells[y][pressedLocation.x];
  if(cell==EMPTY||cell==currentPlayerCell)
    break;
  cells[y][pressedLocation.x]=currentPlayerCell;
  }

for(int x=pressedLocation.x+1;x为什么不使用2d数组

每个单元格将包含一个en