簡體   English   中英

示例代碼未按預期執行

[英]Sample code not doing as expected

這是算法(無效),請讓我知道錯誤在哪里

謝謝

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

看起來輪換回另一位玩家的位置與第一次移動時輪換的位置完全相同。 我猜想在每次移動之間可能沒有清除由addToPiecesToTurn填充的數組,因此所有先前的位置仍在其中。

如果將要翻轉的零件存儲在ArrayList ,則可以使用clear()方法在每次翻轉之間擦除集合的內容。


另一個可能的問題是,您正在檢查對方球員,然后立即開始填充addToPiecesToTurn 但是,沿該方向的棋子不一定有效地旋轉,除非它們被包含當前玩家棋子的第二個位置“夾在”其中。 我認為您的代碼無法正確檢查這種情況; 發生這種情況時,您將需要以某種方式跳過將這些片段翻轉給其他玩家的操作,例如清除piecesToTurn數組。


編輯 :查看當前解決方案,在其中分別實現每個方向,您將有很多重復的代碼。 如果考慮沿某個方向行走意味着什么,則可以將其視為按“步長”量調整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

對於某些單元測試來說,這種問題已經成熟。 您可以很容易地設置一個電路板,采取行動並驗證答案,而測試結果將使您對期望和現實之間的差異有很多了解。

為什么不使用二維數組?

每個單元格將包含一個枚舉:EMPTY,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;
  }

暫無
暫無

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

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