[英]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.