简体   繁体   中英

Sample code not doing as expected

Here is the algorithm (not working) Please let me know where the error is

Thanks

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

It looks like the locations that got rotated back to the other player are the exact same as those rotated during the first move. I would guess that the array being populated by addToPiecesToTurn is perhaps not being cleared out between each move, so all the previous locations are still in there.

If you are storing the pieces to be turned in an ArrayList , you can use the clear() method to erase the contents of the collection between each turn.


Another possible problem is that you are checking for the opposite player, and then instantly beginning to populate addToPiecesToTurn . However, the pieces in that direction are not necessarily valid to be rotated unless they are "sandwiched" in by a second location containing the current player's piece. I don't think your code is properly checking for that case; when that happens, you'll want to somehow skip flipping those pieces to the other player, such as clearing out the array of piecesToTurn .


Edit : Looking at your current solution where you are implementing every direction separately, you are going to have a lot of duplicated code. If you think about what it means to walk along a certain direction, you can think of it as adjusting the x/y value by a "step" amount. The step amount could be -1 for backwards, 0 for no move, or 1 for forwards. Then you could create a single method that handles all directions without duplicating the logic:

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

You would need to implement isValid to check that the location is valid, ie, in the board. Then you could call this method for each direction:

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

This is the sort of problem that is ripe for some unit testing. You could very easily set up a board, play a move, and validate the answer, and the test results would give plenty of insight into where your expectations and reality diverge.

why didn't you use a 2d array ?

each cell would contain an enum : EMPTY, PLAYER_1, PLAYER_2 .

then, in order to go over the cells, you simply use loops for each direction.

for example, upon clicking on a cell , checking to the right would be:

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

checking from top to bottom would be:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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